jpa中使用ExampleMatcher进行动态条件组装查询

在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查询。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章