用Spring Data Jpa的SpecificationExecutor接口做复杂查询

JpaSpecificationExecutor接口

Dao接口继承(官网代码示例)


public interface CustomerRepository extends CrudRepository, JpaSpecificationExecutor {
 …
}

 JpaSpecificationExecutor提供很多条件查询方法

public interface JpaSpecificationExecutor {

	/**
	 * Returns a single entity matching the given {@link Specification}.
	 * 
	 * @param spec
	 * @return
	 */
	T findOne(Specification spec);

	/**
	 * Returns all entities matching the given {@link Specification}.
	 * 
	 * @param spec
	 * @return
	 */
	List findAll(Specification spec);

	/**
	 * Returns a {@link Page} of entities matching the given {@link Specification}.
	 * 
	 * @param spec
	 * @param pageable
	 * @return
	 */
	Page findAll(Specification spec, Pageable pageable);

	/**
	 * Returns all entities matching the given {@link Specification} and {@link Sort}.
	 * 
	 * @param spec
	 * @param sort
	 * @return
	 */
	List findAll(Specification spec, Sort sort);

	/**
	 * Returns the number of instances that the given {@link Specification} will return.
	 * 
	 * @param spec the {@link Specification} to count instances for
	 * @return the number of instances
	 */
	long count(Specification spec);
}

比如方法:

 

1

List findAll(Specification var1);

就可以查找出符合条件的所有数据,如果你的框架使用的是前段分页的技术,那么这个方法就挺简便的。

那么这个方法该如何使用呢?我们看到它需要的参数是一个

 

1

org.springframework.data.jpa.domain.Specification

对象。那我们就创建这个对象先看看。

 

1

2

3

4

5

6

Specification specification = new Specification() {

   @Override

   public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {

    return null;

   }

  }

IDE自动生成了要重写的方法toPredicate。

root参数是我们用来对应实体的信息的。criteriaBuilder可以帮助我们制作查询信息。

 

1

2

3

4

5

6

7

8

/**

 * A root type in the from clause.

 * Query roots always reference entities.

 *

 * @param the entity type referenced by the root

 * @since Java Persistence 2.0

 */

public interface Root extends From {...}

 

1

2

3

4

5

6

7

8

9

10

11

/**

 * Used to construct criteria queries, compound selections,

 * expressions, predicates, orderings.

 *

 *

Note that Predicate is used instead of Expression

 * 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对象里有很多条件方法,比如制定条件:某条数据的创建日期小于今天。

 

1

criteriaBuilder.lessThan(root.get("createDate"), today)

该方法返回的对象类型是Predicate。正是toPredicate需要返回的值。

如果有多个条件,我们就可以创建一个Predicate集合,最后用CriteriaBuilder的and和or方法进行组合,得到最后的Predicate对象

 

1

2

3

4

5

6

7

8

9

/**

  * 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);

1

2

3

4

5

6

7

8

9

/**

  * 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 findByCondition(Date minDate, Date maxDate, String nickname){
  List resultList = null;
  Specification querySpecifi = new Specification() {
   @Override
   public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
 
    List 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;
 }

 

你可能感兴趣的:(Java,spring,boot学习笔记)