<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Spring Data JPA:解析CriteriaQuery

      CriteriaQuery 

      源碼定義

      CriteriaQuery定義在包路徑javax.persistence.criteria下,其定義如下:

      /**
       * The <code>CriteriaQuery</code> interface defines functionality that is specific 
       * to top-level queries.
       *
       * @param <T>  the type of the defined result
       *
       * @since 2.0
       */
      public interface CriteriaQuery<T> extends AbstractQuery<T> {

       

      類圖

      CriteriaQuery對(duì)應(yīng)的類圖如下:

        

      方法定義

      此處聚焦CriteriaQuery繼承體系中定義了哪些方法,請(qǐng)參見(jiàn)下圖:

      解讀:

      根據(jù)上述方法的返回值可知,AbstractQuery、CriteriaQuery接口中的方法大部分是返回其本身類型變量,可以理解為流式API的寫法。

       

      實(shí)際應(yīng)用

      回顧一下Specification中toPredicate方法的定義,代碼如下:

          /**
           * Creates a WHERE clause for a query of the referenced entity in form of a {@link Predicate} for the given
           * {@link Root} and {@link CriteriaQuery}.
           *
           * @param root must not be {@literal null}.
           * @param query must not be {@literal null}.
           * @param criteriaBuilder must not be {@literal null}.
           * @return a {@link Predicate}, may be {@literal null}.
           */
          @Nullable
          Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder);

      解讀:

      上述方法的第二個(gè)參數(shù)為CriteriaQuery類型,所以在構(gòu)建Specification的實(shí)例(實(shí)現(xiàn)其toPredicate方法)時(shí)可以借助CriteriaQuery的能力,案例如下:

        public Page<User> getUsers(Integer id, Integer pageNum, Integer pageSize) {
          Sort sort = Sort.by(Sort.Direction.DESC, "id");
          Pageable pageable = PageRequest.of(pageNum, pageSize, sort);
      
          Specification<User> specification = new Specification<>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
              Path<Integer> idPath = root.get("id");
      
              query.where(cb.lt(idPath, id));
              query.orderBy(cb.asc(idPath));
      
              return query.getRestriction();
            }
          };
          return userRepository.findAll(specification, pageable);
        }

      解讀:

      上述案例調(diào)用了CriteriaQuery的where以及orderBy方法以指定具體查詢條件,在return語(yǔ)句中調(diào)用了CriteriaQuery的getRestriction方法。

       

      Note:

      從前面類圖可知,getRestriction方法實(shí)際上是定義在CommonAbstractCriteria接口中,代碼如下:

      /**
       * The <code>CommonAbstractCriteria</code> interface defines functionality 
       * that is common to both top-level criteria queries and subqueries as 
       * well as to update and delete criteria operations.
       * It is not intended to be used directly in query construction.
       *
       * <p> Note that criteria queries and criteria update and delete operations
       * are typed differently.
       * Criteria queries are typed according to the query result type.
       * Update and delete operations are typed according to the target of the
       * update or delete.
       *
       * @since 2.1
       */
      public interface CommonAbstractCriteria {
      
          /**
           * Create a subquery of the query. 
           * @param type  the subquery result type
           * @return subquery 
           */
          <U> Subquery<U> subquery(Class<U> type);
      
          /**
           * Return the predicate that corresponds to the where clause
           * restriction(s), or null if no restrictions have been
           * specified.
           * @return where clause predicate
           */
          Predicate getRestriction();
       
      }

       


       

      CriteriaQuery與EntityManager

      EntityManager定義在包路徑javax.persistence下,其中的一些方法如下圖所示:

      解讀:

      EntityManager提供了眾多createQuery方法,其中一個(gè)createQuery方法可以接受CriteriaQuery類型的參數(shù),該方法的定義如下:

          /**
           * Create an instance of <code>TypedQuery</code> for executing a
           * criteria query.
           * @param criteriaQuery  a criteria query object
           * @return the new query instance
           * @throws IllegalArgumentException if the criteria query is
           *         found to be invalid
           * @since 2.0
           */
          public <T> TypedQuery<T> createQuery(CriteriaQuery<T> criteriaQuery); 

       

      示例

        private void getUserList(String specialEmail) {
          CriteriaBuilder cb = entityManager.getCriteriaBuilder();
          CriteriaQuery<User> query = cb.createQuery(User.class);
      
          Root<User> root = query.from(User.class);
      
          Path<User> email = root.get("email");
          Predicate predicateEmail = cb.equal(email, specialEmail);
          query.where(predicateEmail);
      
          TypedQuery<User> q = entityManager.createQuery(query);
          List<User> result = q.getResultList();
          for (User user : result) {
            //打印查詢結(jié)果
            System.out.println(user.toString());
          }
        }

      解讀:

      上述代碼通過(guò)EntityManager的getCriteriaBuilder方法獲取了CriteriaBuilder類型的變量,進(jìn)而構(gòu)建了CriteriaQuery類型的變量,然后進(jìn)一步利用CriteriaQuery中的from、where等方法指定查詢條件。

       

      擴(kuò)展閱讀

      官方文檔[地址]

      其它[地址]

      posted @ 2021-08-30 08:07  時(shí)空穿越者  閱讀(4407)  評(píng)論(0)    收藏  舉報(bào)
      主站蜘蛛池模板: 中文字幕精品亚洲无线码二区| 国产视频 视频一区二区| 中文字幕日韩区二区三区| 久久亚洲国产精品五月天| 国产成人最新三级在线视频| 久久久www成人免费精品| 人妻少妇无码精品专区| 精品久久久久中文字幕日本| 少妇伦子伦情品无吗| 青春草在线视频观看| 国产精品VA尤物在线观看| 最新亚洲人成网站在线影院 | 久久久久免费看成人影片| 无码熟妇人妻av影音先锋| 麻豆国产成人av高清在线| 日本大片免A费观看视频三区| 延边| 三上悠亚精品一区二区久久| 日韩免费无码一区二区三区| 日韩中文字幕人妻一区| 久久久久国产精品人妻| 韩国av无码| 国产精品久久久久无码网站| 老色鬼在线精品视频在线观看 | 岛国一区二区三区高清视频| 亚洲精品www久久久久久| 内射囯产旡码丰满少妇| 亚洲精品日韩久久精品| 龙门县| 国产午夜福利精品视频| 色综合天天综合天天更新| 久久精品女人的天堂av| 国产成人精品午夜2022| 人妻少妇| 国产成人精品无码免费看| 中文字幕午夜福利片午夜福利片97| 91亚洲一线产区二线产区| 色AV专区无码影音先锋| 国产午夜精品福利视频| 国产精品白嫩极品在线看| 又大又粗又爽18禁免费看|