发表于: 2020-06-18 23:34:48
1 1630
今天完成的事情:添加了动态查询到作品表中,添加了Aspectj相关日志,异常捕捉,完结了所有的Controller.
明天计划的事情:明天添加Nginx对图片的代理,以及查看响应时间,分析下时耗分布图。然后提交任务三。
遇到的问题:今天有一个问题org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException,由于修改属性的类型int为Intege,对应的set和get没有改回来,就报错了。重写set和get就行了。
收获:
了解VO ,DTO ,DO,PO
VO(View Object):视图对象,用于展示层,它的作用是把某个指定页面(或组件)的所有数据封装起来。
DTO(Data Transfer Object):数据传输对象,这个概念来源于J2EE的设计模式,原来的目的是为了EJB的分布式应用提供粗粒度的数据实体,以减少分布式调用的次数,从而提高分布式调用的性能和降低网络负载,但在这里,我泛指用于展示层与服务层之间的数据传输对象。
DO(Domain Object):领域对象,就是从现实世界中抽象出来的有形或无形的业务实体。
PO(Persistent Object):持久化对象,它跟持久层(通常是关系型数据库)的数据结构形成一一对应的映射关系,如果持久层是关系型数据库,那么,数据表中的每个字段(或若干个)就对应PO的一个(或若干个)属性。'
由于自己碰见的业务比较少,不能完全用到和深刻理解各层架设的意义。但是在对分页那块我自己封装的一个pagaInfoDTO对象,用来实现DTO到VO的传输。
https://www.cnblogs.com/qixuejia/p/4390086.html
分页处理 service和Controller
封装PageInfoDTO对象,还要添加相应的get和set,toString方法
public class PageInfoDTO implements Serializable {
//当前页数
private Integer pageNum;
//总条数
private Long total;
//总页数
private Integer pages;
List<Picture> listPic;
@Override
public PageInfoDTO findAllPicture(Picture pictureLog, int pageNum, int pageSize) {
PageHelper.startPage(pageNum,pageSize);
List listPicture = pictureDao.findAllPicture(pictureLog,(pageNum-1)*pageSize,pageSize);
PageInfo pageInfo =new PageInfo(listPicture);
System.out.println(listPicture);
PageInfoDTO pageInfoDTO = new PageInfoDTO();
pageInfoDTO.setPageNum(pageNum);
pageInfoDTO.setPages(pageInfo.getPages());
pageInfoDTO.setTotal(pageInfo.getTotal());
pageInfoDTO.setListPic(pageInfo.getList());
return pageInfoDTO;
}
控制层
@Controller
@RequestMapping("/picture")
public class PictureController {
@Resource
private PictureService pictureService;
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public Result addPicture(Picture picture){
int num = pictureService.addPicture(picture);
return Result.back(num);
}
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
@ResponseBody
public Result findPicture(@PathVariable int id){
Picture picture = pictureService.findPicture(id);
return Result.back(picture);
}
@RequestMapping(value = "/all",method = RequestMethod.GET)
@ResponseBody
public PageInfoDTO findAllPicture(Picture picture,@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum,@RequestParam(value = "pageSize",required = false,defaultValue = "10") int pageSize){
PageInfoDTO pageInfoDTO = pictureService.findAllPicture(picture,pageNum,pageSize);
System.out.println(pageInfoDTO);
return pageInfoDTO;
}
@RequestMapping(method = RequestMethod.PUT)
@ResponseBody
public Result updatePicture(@RequestBody Picture picture){
int num = pictureService.updatePicture(picture);
return Result.back(num);
}
@RequestMapping(value = "{id}",method = RequestMethod.DELETE)
@ResponseBody
public Result deletePicture(@PathVariable int id){
int num = pictureService.deletePicture(id);
return Result.back(num);
}
}
mapper中添加动态查询
<select id="findPicture" resultMap="forPicture">
select id,name,catalog_id,author_id,intro,priority,image,update_at,create_at,create_by,update_by
from picture where id=#{id}
</select>
<!--关联留言表查询-->
<select id="getMessage" resultType="Message">
select * from message where picture_id=#{id}
</select>
<select id="findAllPicture" resultType="Picture">
select id,name,author_id,catalog_id,intro,priority,image,update_at,create_at,create_by,update_by
from picture
<where>
<!--根据作者id查询-->
<if test="pictureLog.authorId != null">
and author_id = #{pictureLog.authorId}
</if>
<!--根据类目id查询-->
<if test="pictureLog.catalogId != null">
and catalog_id = #{pictureLog.catalogId}
</if>
<!--根据作品名字模糊查询-->
<if test="pictureLog.name != null">
and name like '%${pictureLog.name}%'
</if>
<!--根据优先级查询-->
<if test="pictureLog.priority != null">
and priority = #{pictureLog.priority}
</if>
</where>
order by priority desc
limit #{rowIndex},#{pageSize}
</select>
评论