詳解SpringDataJPA動(dòng)態(tài)條件查詢的寫(xiě)法

我們?cè)谑褂肧pringData JPA框架時(shí),進(jìn)行條件查詢,如果是固定條件的查詢,我們可以使用符合框架規(guī)則的自定義方法以及@Query注解實(shí)現(xiàn)。

成都創(chuàng)新互聯(lián)公司是一家專(zhuān)業(yè)提供潯陽(yáng)企業(yè)網(wǎng)站建設(shè),專(zhuān)注與網(wǎng)站制作、成都做網(wǎng)站H5響應(yīng)式網(wǎng)站、小程序制作等業(yè)務(wù)。10年已為潯陽(yáng)眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。

如果是查詢條件是動(dòng)態(tài)的,框架也提供了查詢接口。

JpaSpecificationExecutor

 和其他接口使用方式一樣,只需要在你的Dao接口繼承即可(官網(wǎng)代碼)。

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

就可以查找出符合條件的所有數(shù)據(jù),如果你的框架使用的是前段分頁(yè)的技術(shù),那么這個(gè)方法就挺簡(jiǎn)便的。

那么這個(gè)方法該如何使用呢?我們看到它需要的參數(shù)是一個(gè)

org.springframework.data.jpa.domain.Specification

對(duì)象。那我們就創(chuàng)建這個(gè)對(duì)象先看看。

Specification specification = new Specification() {
   @Override
   public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
    return null;
   }
  }

IDE自動(dòng)生成了要重寫(xiě)的方法toPredicate。

root參數(shù)是我們用來(lái)對(duì)應(yīng)實(shí)體的信息的。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&#060;Boolean&#062;</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對(duì)象里有很多條件方法,比如制定條件:某條數(shù)據(jù)的創(chuàng)建日期小于今天。

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

該方法返回的對(duì)象類(lèi)型是Predicate。正是toPredicate需要返回的值。

如果有多個(gè)條件,我們就可以創(chuàng)建一個(gè)Predicate集合,最后用CriteriaBuilder的and和or方法進(jìn)行組合,得到最后的Predicate對(duì)象。

/**
  * 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到一起的話所有條件就是且關(guān)系,or就是或關(guān)系了。

其實(shí)也是在Stack Overflow上看到的。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當(dāng)前名稱(chēng):詳解SpringDataJPA動(dòng)態(tài)條件查詢的寫(xiě)法
URL地址:http://bm7419.com/article10/jdedgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站內(nèi)鏈、關(guān)鍵詞優(yōu)化軟件開(kāi)發(fā)、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管