暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

SpringDataJPA笔记(7)-Specification

生活不止眼前的代码 2019-05-11
528

网页浏览地址

https://blog.csdn.net/yingziisme/article/details/89819308


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/gegreater than/greater equal ,大于/大于等于
lt/leless than/less equal,小于/小于等于
eq/neequal/not equal, 等于/不等于
in/notIn包含/不包含
like/notLikelike/notLike
betweenbetween

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

  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. }

源码参考 https://github.com/yingziisme/spring-boot-jpa-demo


文章转载自生活不止眼前的代码,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论