条件查询的数据类型一定要与表中的数据类型一致,否则会返回服务器异常,

  1. {
  2. "code": 9999,
  3. "message": "服务器异常",
  4. "request": "GET /v1/spu/id/2/simplify"
  5. }

并发出警告

2021-03-08 10:03:19.981 WARN 15636 —- [nio-8081-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [2] did not match expected type [java.lang.Long (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [2] did not match expected type [java.lang.Long (n/a)]]

表的model层
1.首先将表的model映射到JAVA中。
表的增删改查方法的Repository层
2.使用Repository的方法完成对数据表的查询。
表的sevice层完成增删改查方法的实现
3.使用service类来完成Repository层的方法的实现。
表的vo层则可以将多余的数据筛出,避免返回前端的数据过于的臃肿
4.VO层可以用于筛选不必要的数据,并且返回到前端。

vo层代码:

  1. import lombok.Getter;
  2. import lombok.Setter;
  3. @Getter
  4. @Setter
  5. public class SpuSimplifyVO {
  6. private Integer id;
  7. private String title;
  8. private String subtitle;
  9. private String img;
  10. private String price;
  11. private Integer sketchSpecId;
  12. private String forThemeImg;
  13. private String discountPrice;
  14. private String description;
  15. private String tags;
  16. }


. 方法过于笨重 不便捷。**

  1. @GetMapping("id/{id}/detail")
  2. public SpuSimplifyVO getSimplifySpu(@PathVariable @Positive Integer id){
  3. Spu spu = this.spuService.getSpu(id);
  4. SpuSimplifyVO vo = new SpuSimplifyVO();
  5. vo.setTitle(spu.getTitle());
  6. return vo;
  7. }



②.使用BeanUntils中的copyProperties方法将源spu传入到目标vo中,相对于第一种方法较为简便

  1. @GetMapping("id/{id}/simplify")
  2. public SpuSimplifyVO getSimplifySpu(@PathVariable @Positive Integer id){
  3. Spu spu = this.spuService.getSpu(id);
  4. SpuSimplifyVO vo = new SpuSimplifyVO();
  5. BeanUtils.copyProperties(spu,vo);
  6. return vo;
  7. }



③.DozerMapper
使用DozerMapper拷贝属性并精简。

  1. @GetMapping("/latest")
  2. public List<SpuSimplifyVO> getLatestSpuList(){
  3. Mapper mapper = DozerBeanMapperBuilder.buildDefault();
  4. List<Spu> spuList = this.spuService.getLatestPaging();
  5. List<SpuSimplifyVO> vos = new ArrayList<>();
  6. spuList.forEach(s->{
  7. SpuSimplifyVO vo = mapper.map(s,SpuSimplifyVO.class);
  8. vos.add(vo);
  9. });
  10. return vos;
  11. }

分页:

分页一般在数据库进行,如果从数据库中将数据取出并在服务器进行分页操作会占用大量的内存,这是不被提倡的,所以在数据被取出之前就要完成分页操作。

  1. @GetMapping("/latest?start= & count=")
  2. public List<SpuSimplifyVO> getLatestSpuList(@RequestParam(defaultValue = "0") Integer start,
  3. @RequestParam(defaultValue = "10") Integer count){
  4. Mapper mapper = DozerBeanMapperBuilder.buildDefault();
  5. List<Spu> spuList = this.spuService.getLatestPaging();
  6. List<SpuSimplifyVO> vos = new ArrayList<>();
  7. spuList.forEach(s->{
  8. SpuSimplifyVO vo = mapper.map(s,SpuSimplifyVO.class);
  9. vos.add(vo);
  10. });
  11. return vos;
  12. }

如代码所示:在接口地址后加上开始的页数以及每次加载的页数,在参数列表加入@RequestParm注解(defaultValue表示如果不输入时它的默认值)

  1. public List<Spu> getLatestPaging(Integer pageNum,Integer size){
  2. Pageable pageable = PageRequest.of(pageNum,size, Sort.by("creaTime").descending());
  3. return this.spuRepository.findAll();
  4. }

在取到数据的方法的参数列表中 定义pageNum以及size,定义一个pageable用来存储用pageRequest.of方法取出来的数据,PageRequest.of(pageNum,size)这是一个最基本的重载方法,由于需要进行倒序排序,所以需要传递一个sort对象进行倒序排序,descending表示倒序排序,by(“creaTime”)表示排序的字段。

bo对象以及分页参数转换:

新建bo包以及util包
在util包中创建一个用于参数转换的类:

  1. public class CommonUtil {
  2. public static PageConunter convertToPageParmeter(Integer start, Integer count){
  3. int pageNum = start / count;
  4. PageConunter pageConunter = PageConunter.builder()
  5. .page(pageNum)
  6. .count(count)
  7. .build();
  8. return pageConunter;
  9. }
  10. }

在bo包中设置转换后的参数,并且使用builder构造器使代码更加优美

  1. @Getter
  2. @Setter
  3. @Builder
  4. public class PageConunter {
  5. public Integer page;
  6. public Integer count;
  7. }

在SpuController中使用

  1. @GetMapping("/latest?start= & count=")
  2. public List<SpuSimplifyVO> getLatestSpuList(@RequestParam(defaultValue = "0") Integer start,
  3. @RequestParam(defaultValue = "10") Integer count){
  4. PageConunter pageConunter = CommonUtil.convertToPageParmeter(start, count);
  5. Page<Spu> spuList = (Page<Spu>) this.spuService.getLatestPaging(pageConunter.getPage(), pageConunter.getCount());
  6. Mapper mapper = DozerBeanMapperBuilder.buildDefault();
  7. List<SpuSimplifyVO> vos = new ArrayList<>();
  8. spuList.forEach(s->{
  9. SpuSimplifyVO vo = mapper.map(s,SpuSimplifyVO.class);
  10. vos.add(vo);
  11. });
  12. return vos;
  13. }

分页数据的返回:

1.构建一个分页的对象:

  1. package come.lin.missyou.vo;
  2. import lombok.Getter;
  3. import lombok.NoArgsConstructor;
  4. import lombok.Setter;
  5. import org.springframework.data.domain.Page;
  6. import java.util.List;
  7. @Setter
  8. @Getter
  9. @NoArgsConstructor
  10. public class Paging<T> {
  11. private Long total;
  12. private Integer count;
  13. private Integer page;
  14. private Integer totalpage;
  15. private List<T> items;
  16. public Paging(Page<T> pageT){
  17. this.initPageParmeters(pageT);
  18. this.items = pageT.getContent();
  19. }
  20. void initPageParmeters(Page<T> pageT){
  21. this.total = pageT.getTotalElements();
  22. this.count = pageT.getSize();
  23. this.page = pageT.getNumber();
  24. this.totalpage = pageT.getTotalPages();
  25. }
  26. }

2.封装一个类 继承paging这个基础的分页对象,变成一个可以复制属性的paging对象,

  1. package come.lin.missyou.vo;
  2. import com.github.dozermapper.core.DozerBeanMapperBuilder;
  3. import com.github.dozermapper.core.Mapper;
  4. import org.springframework.data.domain.Page;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. public class PagingDozer<T,K> extends Paging {
  8. public PagingDozer(Page<T> pageT,Class<K> classK){
  9. this.initPageParmeters(pageT);
  10. List<T> tList = pageT.getContent();
  11. Mapper mapper = DozerBeanMapperBuilder.buildDefault();
  12. List<K> voList = new ArrayList<>();
  13. tList.forEach(t->{
  14. K vo = mapper.map(t,classK);
  15. voList.add(vo);
  16. });
  17. this.setItems(voList);
  18. }
  19. }

3.使用封装的类来简化代码:

  1. @GetMapping("/latest")
  2. public PagingDozer<Spu, SpuSimplifyVO> getLatestSpuList(@RequestParam(defaultValue = "0") Integer start,
  3. @RequestParam(defaultValue = "10") Integer count){
  4. PageConunter pageConunter = CommonUtil.convertToPageParmeter(start, count);
  5. Page<Spu> pageSpu = (Page<Spu>) this.spuService.getLatestPaging(pageConunter.getPage(), pageConunter.getCount());
  6. return new PagingDozer<Spu,SpuSimplifyVO>(pageSpu,SpuSimplifyVO.class);
  7. }