SpringDataJPA笔记(7)-Specification

短命女 2022-02-05 04:55 485阅读 0赞

SpringDataJPA-Specification

使用Specification可以构建动态查询

原生的使用起来有点复杂,这里推介一个别人封装好的工具包

这里是github的地址

https://github.com/wenhao/jpa-spec/blob/master/README_CN.md

  1. <!-- https://mvnrepository.com/artifact/com.github.wenhao/jpa-spec -->
  2. <dependency>
  3. <groupId>com.github.wenhao</groupId>
  4. <artifactId>jpa-spec</artifactId>
  5. <version>3.2.4</version>
  6. </dependency>

具体用法


































方法 含义
gt/ge greater than/greater equal ,大于/大于等于
lt/le less than/less equal,小于/小于等于
eq/ne equal/not equal, 等于/不等于
in/notIn 包含/不包含
like/notLike like/notLike
between between

基础查询均支持三个参数或者两个参数的用法

  1. condition: 如果为true(默认),应用此条件查询。
  2. property: 字段名称。
  3. values: 具体查询的值,eq/ne/like 支持多个值。

不同的查询条件之间可以用and或者or来连接

具体代码使用可参考下面的实例

  1. @Slf4j
  2. @RestController
  3. @RequestMapping("/chapter/seven")
  4. public class ChapterSevenController {
  5. @Autowired
  6. private CatRepository catRepository;
  7. @ApiOperation(value = "and条件查询", httpMethod = "POST")
  8. @PostMapping("/search/cat/and")
  9. public List<CatEntity> searchCatAnd(@RequestBody ChapterSevenDTO chapterSevenDTO) {
  10. Specification<CatEntity> specification = Specifications.<CatEntity>and()
  11. .gt(null != chapterSevenDTO.getAge(), "age", chapterSevenDTO.getAge())
  12. .lt(null != chapterSevenDTO.getHeight(), "height", chapterSevenDTO.getHeight())
  13. .eq(null != chapterSevenDTO.getWeight(), "weight", chapterSevenDTO.getWeight()).build();
  14. return catRepository.findAll(specification);
  15. }
  16. @ApiOperation(value = "or条件查询", httpMethod = "POST")
  17. @PostMapping("/search/cat/or")
  18. public List<CatEntity> searchCatOr(@RequestBody ChapterSevenDTO chapterSevenDTO) {
  19. Specification<CatEntity> specification = Specifications.<CatEntity>or()
  20. .gt(null != chapterSevenDTO.getAge(), "age", chapterSevenDTO.getAge())
  21. .lt(null != chapterSevenDTO.getHeight(), "height", chapterSevenDTO.getHeight())
  22. .eq(null != chapterSevenDTO.getWeight(), "weight", chapterSevenDTO.getWeight()).build();
  23. return catRepository.findAll(specification);
  24. }
  25. @ApiOperation(value = "复杂条件查询", httpMethod = "POST")
  26. @PostMapping("/search/cat")
  27. public List<CatEntity> searchCat(@RequestBody ChapterSevenDTO chapterSevenDTO) {
  28. Specification<CatEntity> specification = Specifications.<CatEntity>or()
  29. .gt(null != chapterSevenDTO.getAge(), "age", chapterSevenDTO.getAge())
  30. .lt(null != chapterSevenDTO.getHeight(), "height", chapterSevenDTO.getHeight()).build();
  31. Specification<CatEntity> specification1 = Specifications.<CatEntity>and()
  32. .eq(null != chapterSevenDTO.getSex(), "sex", chapterSevenDTO.getSex())
  33. .eq(null != chapterSevenDTO.getName(), "name", chapterSevenDTO.getName()).build();
  34. Specification<CatEntity> specification2 = Specifications.<CatEntity>and().predicate(specification).predicate(specification1)
  35. .lt(null != chapterSevenDTO.getWeight(), "weight", chapterSevenDTO.getWeight()).build();
  36. return catRepository.findAll(specification2);
  37. }
  38. }

源码参考 GITHUB
欢迎关注微信交流
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,485人围观)

还没有评论,来说两句吧...

相关阅读