specification jpa 复杂查询

目录

前言

一、specification是什么?

二、使用方法

1.repository继承JpaSpecificationExecutor接口

2.service层构造specification

2.1 各成员含义解析 

2.2 sql对比

3. 构造完成之后的使用

总结



前言

本文主要介绍我对于jpa中specification相关概念的理解和简单的使用介绍。欢迎大家探讨。


提示:以下是本篇文章正文内容,下面案例可供参考

一、specification是什么?

我的理解就是specification主要是用来构造复杂查询条件的东西。

二、使用方法

1.repository继承JpaSpecificationExecutor接口

代码如下(示例):JpaSpecificationExecutor<T> 这个泛型一般就是表的实体类

@Repository
public interface StudentRepository extends JpaRepository<StudentEntity, Long>, JpaSpecificationExecutor<StudentEntity> {
}

2.service层构造specification

一般来说使用过程如图

@Service
public class StudentService {

    @Resource
    private StudentRepository studentRepository;

    /**
     * 根据给出的条件查询
     *
     * @param req 查询请求
     * @return 查询结果
     */
    public List<StudentEntity> getStudentByConditions(QueryStudentReq req) {
        // 通过给出的条件构造查询语句specification
        Specification<StudentEntity> specification = buildSpecification(req);
        // 通过构造的specification来进行查询
        return studentRepository.findAll(specification);
    }

    /**
     * 构造specification的类
     *
     * @param req 查询请求
     * @return 可以理解为根据查询请求构造的sql
     */
    Specification<StudentEntity> buildSpecification(QueryStudentReq req) {
        // 通过给出的条件构造查询语句specification
        Specification<StudentEntity> specification = new Specification<StudentEntity>() {
            // 重写构造方法
            @Override
            public Predicate toPredicate(Root<StudentEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                /*
                root : 当前所用的表,与Root<T> root中的T对应的表
                query: 最后构造查询语句
                cb :查询条件的构造器
                 */
                List<Predicate> condition = new ArrayList<>();
                // 查询所有姓名中包含关键字的学生信息
                condition.add(cb.like(root.get("name"), "%"+req.getNameKeyword()+"%"));
                // todo 根据条件构造查询语句
                Predicate[] predicates = new Predicate[condition.size()];\
                // 使用cb构造查询语句
                query.where(cb.and(condition.toArray(predicates)));
                return query.getRestriction();
            }
        };
        return specification;
    }
}

2.1 各成员含义解析 

其中最重要的就是那个重写的public Predicate toPredicate方法,

Predicate   可以理解为where中的一个查询条件

cb   是帮助我们生成条件构造器

root   就是当前

2.2 sql对比

上述代码对应的sql为:

select * from student s where s.name like "%"+查询关键字+"%"

root.get("name") 这个就相当于 s.name,把这个字段给拿出来操作,ike由cb.like来完成。

之所以说这么多是因为我踩过的一个巨坑:

select * from student s where s.name in (xxx,xxx)

我想的sql是这样子的,但是在cb中找了半天没有in啊,有的那个感觉也不会用,最后反应过来,可以通过

root.get("name").in(xxx)来操作!

使用的过程中请一定注意查询条件之间是and链接还是or链接,不要最后一个cb.and()把条件全部and链接了(捂脸)

cb中的方法非常之多,几乎涵盖了sql中where查询的方方面面,官方文档为:https://docs.oracle.com/javaee/6/api/javax/persistence/criteria/CriteriaBuilder.html

3. 构造完成之后的使用

直接使用repository.find或findAll来查询

    public List<StudentEntity> getStudentByConditions(QueryStudentReq req) {
        // 通过给出的条件构造查询语句specification
        Specification<StudentEntity> specification = buildSpecification(req);
        // 通过构造的specification来进行查询
        return studentRepository.findAll(specification);
    }

分页查询的话就传入自己构造的pageable,记得返回类型Page<T>的对象哦 

     return studentRepository.findAll(specification, pageable);


总结

本文主要记录分享一下specification的基本使用方法,如果错误还请批评指正,也欢迎大家一起探讨交流!转载请注明出处。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值