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查詢。

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