条件查询的数据类型一定要与表中的数据类型一致,否则会返回服务器异常,
{
"code": 9999,
"message": "服务器异常",
"request": "GET /v1/spu/id/2/simplify"
}
并发出警告
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层代码:
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class SpuSimplifyVO {
private Integer id;
private String title;
private String subtitle;
private String img;
private String price;
private Integer sketchSpecId;
private String forThemeImg;
private String discountPrice;
private String description;
private String tags;
}
①. 方法过于笨重 不便捷。**
@GetMapping("id/{id}/detail")
public SpuSimplifyVO getSimplifySpu(@PathVariable @Positive Integer id){
Spu spu = this.spuService.getSpu(id);
SpuSimplifyVO vo = new SpuSimplifyVO();
vo.setTitle(spu.getTitle());
return vo;
}
②.使用BeanUntils中的copyProperties方法将源spu传入到目标vo中,相对于第一种方法较为简便
@GetMapping("id/{id}/simplify")
public SpuSimplifyVO getSimplifySpu(@PathVariable @Positive Integer id){
Spu spu = this.spuService.getSpu(id);
SpuSimplifyVO vo = new SpuSimplifyVO();
BeanUtils.copyProperties(spu,vo);
return vo;
}
③.DozerMapper
使用DozerMapper拷贝属性并精简。
@GetMapping("/latest")
public List<SpuSimplifyVO> getLatestSpuList(){
Mapper mapper = DozerBeanMapperBuilder.buildDefault();
List<Spu> spuList = this.spuService.getLatestPaging();
List<SpuSimplifyVO> vos = new ArrayList<>();
spuList.forEach(s->{
SpuSimplifyVO vo = mapper.map(s,SpuSimplifyVO.class);
vos.add(vo);
});
return vos;
}
分页:
分页一般在数据库进行,如果从数据库中将数据取出并在服务器进行分页操作会占用大量的内存,这是不被提倡的,所以在数据被取出之前就要完成分页操作。
@GetMapping("/latest?start= & count=")
public List<SpuSimplifyVO> getLatestSpuList(@RequestParam(defaultValue = "0") Integer start,
@RequestParam(defaultValue = "10") Integer count){
Mapper mapper = DozerBeanMapperBuilder.buildDefault();
List<Spu> spuList = this.spuService.getLatestPaging();
List<SpuSimplifyVO> vos = new ArrayList<>();
spuList.forEach(s->{
SpuSimplifyVO vo = mapper.map(s,SpuSimplifyVO.class);
vos.add(vo);
});
return vos;
}
如代码所示:在接口地址后加上开始的页数以及每次加载的页数,在参数列表加入@RequestParm注解(defaultValue表示如果不输入时它的默认值)
public List<Spu> getLatestPaging(Integer pageNum,Integer size){
Pageable pageable = PageRequest.of(pageNum,size, Sort.by("creaTime").descending());
return this.spuRepository.findAll();
}
在取到数据的方法的参数列表中 定义pageNum以及size,定义一个pageable用来存储用pageRequest.of方法取出来的数据,PageRequest.of(pageNum,size)这是一个最基本的重载方法,由于需要进行倒序排序,所以需要传递一个sort对象进行倒序排序,descending表示倒序排序,by(“creaTime”)表示排序的字段。
bo对象以及分页参数转换:
新建bo包以及util包
在util包中创建一个用于参数转换的类:
public class CommonUtil {
public static PageConunter convertToPageParmeter(Integer start, Integer count){
int pageNum = start / count;
PageConunter pageConunter = PageConunter.builder()
.page(pageNum)
.count(count)
.build();
return pageConunter;
}
}
在bo包中设置转换后的参数,并且使用builder构造器使代码更加优美
@Getter
@Setter
@Builder
public class PageConunter {
public Integer page;
public Integer count;
}
在SpuController中使用
@GetMapping("/latest?start= & count=")
public List<SpuSimplifyVO> getLatestSpuList(@RequestParam(defaultValue = "0") Integer start,
@RequestParam(defaultValue = "10") Integer count){
PageConunter pageConunter = CommonUtil.convertToPageParmeter(start, count);
Page<Spu> spuList = (Page<Spu>) this.spuService.getLatestPaging(pageConunter.getPage(), pageConunter.getCount());
Mapper mapper = DozerBeanMapperBuilder.buildDefault();
List<SpuSimplifyVO> vos = new ArrayList<>();
spuList.forEach(s->{
SpuSimplifyVO vo = mapper.map(s,SpuSimplifyVO.class);
vos.add(vo);
});
return vos;
}
分页数据的返回:
1.构建一个分页的对象:
package come.lin.missyou.vo;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.domain.Page;
import java.util.List;
@Setter
@Getter
@NoArgsConstructor
public class Paging<T> {
private Long total;
private Integer count;
private Integer page;
private Integer totalpage;
private List<T> items;
public Paging(Page<T> pageT){
this.initPageParmeters(pageT);
this.items = pageT.getContent();
}
void initPageParmeters(Page<T> pageT){
this.total = pageT.getTotalElements();
this.count = pageT.getSize();
this.page = pageT.getNumber();
this.totalpage = pageT.getTotalPages();
}
}
2.封装一个类 继承paging这个基础的分页对象,变成一个可以复制属性的paging对象,
package come.lin.missyou.vo;
import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import org.springframework.data.domain.Page;
import java.util.ArrayList;
import java.util.List;
public class PagingDozer<T,K> extends Paging {
public PagingDozer(Page<T> pageT,Class<K> classK){
this.initPageParmeters(pageT);
List<T> tList = pageT.getContent();
Mapper mapper = DozerBeanMapperBuilder.buildDefault();
List<K> voList = new ArrayList<>();
tList.forEach(t->{
K vo = mapper.map(t,classK);
voList.add(vo);
});
this.setItems(voList);
}
}
3.使用封装的类来简化代码:
@GetMapping("/latest")
public PagingDozer<Spu, SpuSimplifyVO> getLatestSpuList(@RequestParam(defaultValue = "0") Integer start,
@RequestParam(defaultValue = "10") Integer count){
PageConunter pageConunter = CommonUtil.convertToPageParmeter(start, count);
Page<Spu> pageSpu = (Page<Spu>) this.spuService.getLatestPaging(pageConunter.getPage(), pageConunter.getCount());
return new PagingDozer<Spu,SpuSimplifyVO>(pageSpu,SpuSimplifyVO.class);
}