当前位置: 移动技术网 > IT编程>开发语言>Java > 详解Spring Data JPA动态条件查询的写法

详解Spring Data JPA动态条件查询的写法

2019年07月22日  | 移动技术网IT编程  | 我要评论

我们在使用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上看到的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网