在jpa查询中,自定义条件查询对象,进行条件查询:
@PostMapping("/findAll")
@ApiOperation(value = "查询分页数据",notes = "jap根据条件查询查询分页数据")
public Page<Paper> findAll(@RequestBody PaperVo param) {
Pageable pageParam = new PageRequest(param.getCurrent() - 1, param.getSize());
return paperService.findAll(param,pageParam);
}
@Override
public Page<Paper> findAll(PaperVo param, Pageable pageable) {
ExampleMatcher matcher = ExampleMatcher.matching()
.withMatcher("name", math -> math.contains());//设置name为模糊匹配规则
Paper paper = new Paper();
BeanUtils.copyProperties(param, paper);
Example example = Example.of(paper, matcher);
Page<Paper> Pages = paperRepository.findAll(example, pageable);
return Pages;
}
math还有其他的匹配规则:
ExampleMatcher能满足大部分的条件查询
弊端:时间字段不支持两个时间条件between查询。