详解Spring Data JPA动态条件查询的写法
作者:Derry.1990 时间:2022-03-17 18:26:06
我们在使用SpringData JPA框架时,进行条件查询,如果是固定条件的查询,我们可以使用符合框架规则的自定义方法以及@Query注解实现。
如果是查询条件是动态的,框架也提供了查询接口。
JpaSpecificationExecutor
和其他接口使用方式一样,只需要在你的Dao接口继承即可(官网代码)。
public interface CustomerRepository extends CrudRepository<Customer, Long>, JpaSpecificationExecutor {
…
}
JpaSpecificationExecutor提供很多条件查询方法。
public interface JpaSpecificationExecutor<T> {
T findOne(Specification<T> var1);
List<T> findAll(Specification<T> var1);
Page<T> findAll(Specification<T> var1, Pageable var2);
List<T> findAll(Specification<T> var1, Sort var2);
long count(Specification<T> var1);
}
比如方法:
List<T> findAll(Specification<T> var1);
就可以查找出符合条件的所有数据,如果你的框架使用的是前段分页的技术,那么这个方法就挺简便的。
那么这个方法该如何使用呢?我们看到它需要的参数是一个
org.springframework.data.jpa.domain.Specification
对象。那我们就创建这个对象先看看。
Specification specification = new Specification() {
@Override
public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
return null;
}
}
IDE自动生成了要重写的方法toPredicate。
root参数是我们用来对应实体的信息的。criteriaBuilder可以帮助我们制作查询信息。
/**
* A root type in the from clause.
* Query roots always reference entities.
*
* @param <X> the entity type referenced by the root
* @since Java Persistence 2.0
*/
public interface Root<X> extends From<X, X> {...}
/**
* Used to construct criteria queries, compound selections,
* expressions, predicates, orderings.
*
* <p> Note that <code>Predicate</code> is used instead of <code>Expression<Boolean></code>
* in this API in order to work around the fact that Java
* generics are not compatible with varags.
*
* @since Java Persistence 2.0
*/
public interface CriteriaBuilder {...}
CriteriaBuilder对象里有很多条件方法,比如制定条件:某条数据的创建日期小于今天。
criteriaBuilder.lessThan(root.get("createDate"), today)
该方法返回的对象类型是Predicate。正是toPredicate需要返回的值。
如果有多个条件,我们就可以创建一个Predicate集合,最后用CriteriaBuilder的and和or方法进行组合,得到最后的Predicate对象。
/**
* Create a conjunction of the given restriction predicates.
* A conjunction of zero predicates is true.
*
* @param restrictions zero or more restriction predicates
*
* @return and predicate
*/
Predicate and(Predicate... restrictions);
/**
* Create a disjunction of the given restriction predicates.
* A disjunction of zero predicates is false.
*
* @param restrictions zero or more restriction predicates
*
* @return or predicate
*/
Predicate or(Predicate... restrictions);
示例:
public List<WeChatGzUserInfoEntity> findByCondition(Date minDate, Date maxDate, String nickname){
List<WeChatGzUserInfoEntity> resultList = null;
Specification querySpecifi = new Specification<WeChatGzUserInfoEntity>() {
@Override
public Predicate toPredicate(Root<WeChatGzUserInfoEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicates = new ArrayList<>();
if(null != minDate){
predicates.add(criteriaBuilder.greaterThan(root.get("subscribeTime"), minDate));
}
if(null != maxDate){
predicates.add(criteriaBuilder.lessThan(root.get("subscribeTime"), maxDate));
}
if(null != nickname){
predicates.add(criteriaBuilder.like(root.get("nickname"), "%"+nickname+"%"));
}
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
}
};
resultList = this.weChatGzUserInfoRepository.findAll(querySpecifi);
return resultList;
}
and到一起的话所有条件就是且关系,or就是或关系了。
其实也是在Stack Overflow上看到的。
来源:http://www.cnblogs.com/derry9005/p/6282571.html
标签:Spring,Data,JPA,动态,查询
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java里的static在Kotlin里如何实现
2023-08-24 20:25:33
java Spring的启动原理详解
2022-09-02 04:39:59
![](https://img.aspxhome.com/file/2023/4/66244_0s.jpg)
java虚拟机钩子关闭函数addShutdownHook的操作
2021-10-18 00:58:25
![](https://img.aspxhome.com/file/2023/6/61576_0s.jpg)
c# 生成随机时间的小例子
2023-10-04 02:44:29
mybatis主从表关联查询,返回对象带有集合属性解析
2023-06-15 16:10:59
![](https://img.aspxhome.com/file/2023/6/79236_0s.jpg)
Windows下Java环境配置的超详细教程
2021-12-18 19:19:16
![](https://img.aspxhome.com/file/2023/4/62154_0s.jpg)
Java线程的五种状态介绍
2023-06-16 01:22:15
![](https://img.aspxhome.com/file/2023/5/80275_0s.png)
java实现上传图片并压缩图片大小功能
2023-06-14 22:32:35
Zookeeper连接超时问题与拒绝连接的解决方案
2023-11-20 03:41:29
![](https://img.aspxhome.com/file/2023/3/78863_0s.png)
java常用工具类 Random随机数、MD5加密工具类
2023-02-14 17:55:08
解析JavaSE的继承和多态
2023-11-24 16:13:48
深入理解MyBatis中的一级缓存与二级缓存
2022-05-25 09:41:34
![](https://img.aspxhome.com/file/2023/7/66057_0s.png)
Flutter应用集成极光推送的实现示例
2023-06-24 03:51:04
![](https://img.aspxhome.com/file/2023/2/90172_0s.jpg)
java使用jdbc链接Oracle示例类分享
2022-06-25 19:18:58
Java中常用缓存Cache机制的实现
2023-05-27 23:32:22
Java中的main函数的详细介绍
2021-10-03 16:09:16
Java编程swing组件JLabel详解以及使用示例
2023-12-15 22:28:50
Java 高并发三:Java内存模型和线程安全详解
2021-10-24 07:04:13
![](https://img.aspxhome.com/file/2023/6/59966_0s.png)
Java迭代器与Collection接口超详细讲解
2022-07-14 05:10:49
![](https://img.aspxhome.com/file/2023/9/66069_0s.png)
Unity的IPostBuildPlayerScriptDLLs实用案例深入解析
2022-12-23 00:12:38