目录
1.repository继承JpaSpecificationExecutor接口
前言
本文主要介绍我对于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的基本使用方法,如果错误还请批评指正,也欢迎大家一起探讨交流!转载请注明出处。