我們在使用SpringData JPA框架時,進行條件查詢,如果是固定條件的查詢,我們可以使用符合框架規則的自定義方法以及@Query注解實現。
如果是查詢條件是動態的,框架也提供了查詢接口。
1
|
JpaSpecificationExecutor |
和其他接口使用方式一樣,只需要在你的Dao接口繼承即可(官網代碼)。
1
2
3
|
public interface CustomerRepository extends CrudRepository<Customer, Long>, JpaSpecificationExecutor { … } |
JpaSpecificationExecutor提供很多條件查詢方法。
1
2
3
4
5
6
7
8
9
10
11
|
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); } |
比如方法:
1
|
List<T> findAll(Specification<T> 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 <X> the entity type referenced by the root * @since Java Persistence 2.0 */ public interface Root<X> extends From<X, X> {...} |
1
2
3
4
5
6
7
8
9
10
11
|
/** * 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對象里有很多條件方法,比如制定條件:某條數據的創建日期小于今天。
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); |
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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