前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringDataJPA笔记(7)-Specification

SpringDataJPA笔记(7)-Specification

作者头像
yingzi_code
发布2019-08-31 12:23:00
1.6K0
发布2019-08-31 12:23:00
举报

SpringDataJPA-Specification

使用Specification可以构建动态查询

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

这里是github的地址

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

代码语言:javascript
复制
<!-- https://mvnrepository.com/artifact/com.github.wenhao/jpa-spec -->
<dependency>
    <groupId>com.github.wenhao</groupId>
    <artifactId>jpa-spec</artifactId>
    <version>3.2.4</version>
</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来连接

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

代码语言:javascript
复制
@Slf4j
@RestController
@RequestMapping("/chapter/seven")
public class ChapterSevenController {

    @Autowired
    private CatRepository catRepository;


    @ApiOperation(value = "and条件查询", httpMethod = "POST")
    @PostMapping("/search/cat/and")
    public List<CatEntity> searchCatAnd(@RequestBody ChapterSevenDTO chapterSevenDTO) {
        Specification<CatEntity> specification = Specifications.<CatEntity>and()
                .gt(null != chapterSevenDTO.getAge(), "age", chapterSevenDTO.getAge())
                .lt(null != chapterSevenDTO.getHeight(), "height", chapterSevenDTO.getHeight())
                .eq(null != chapterSevenDTO.getWeight(), "weight", chapterSevenDTO.getWeight()).build();
        return catRepository.findAll(specification);
    }

    @ApiOperation(value = "or条件查询", httpMethod = "POST")
    @PostMapping("/search/cat/or")
    public List<CatEntity> searchCatOr(@RequestBody ChapterSevenDTO chapterSevenDTO) {
        Specification<CatEntity> specification = Specifications.<CatEntity>or()
                .gt(null != chapterSevenDTO.getAge(), "age", chapterSevenDTO.getAge())
                .lt(null != chapterSevenDTO.getHeight(), "height", chapterSevenDTO.getHeight())
                .eq(null != chapterSevenDTO.getWeight(), "weight", chapterSevenDTO.getWeight()).build();
        return catRepository.findAll(specification);
    }

    @ApiOperation(value = "复杂条件查询", httpMethod = "POST")
    @PostMapping("/search/cat")
    public List<CatEntity> searchCat(@RequestBody ChapterSevenDTO chapterSevenDTO) {
        Specification<CatEntity> specification = Specifications.<CatEntity>or()
                .gt(null != chapterSevenDTO.getAge(), "age", chapterSevenDTO.getAge())
                .lt(null != chapterSevenDTO.getHeight(), "height", chapterSevenDTO.getHeight()).build();
        Specification<CatEntity> specification1 = Specifications.<CatEntity>and()
                .eq(null != chapterSevenDTO.getSex(), "sex", chapterSevenDTO.getSex())
                .eq(null != chapterSevenDTO.getName(), "name", chapterSevenDTO.getName()).build();
        Specification<CatEntity> specification2 = Specifications.<CatEntity>and().predicate(specification).predicate(specification1)
                .lt(null != chapterSevenDTO.getWeight(), "weight", chapterSevenDTO.getWeight()).build();

        return catRepository.findAll(specification2);
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年05月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档