Spring Data JPA Complex and multi conditional composite paging queries

  • 2020-06-19 10:16:35
  • OfStack

Without further ado, see the code:


public Map<String, Object> getWeeklyBySearch(final Map<String, String> serArgs,
      String pageNum, String pageSize) throws Exception {
    // TODO Auto-generated method stub
    Map<String,Object> resultMap=new HashMap<String, Object>();
    //  Determine paging conditions 
    pageNum = TextUtils.isNotBlank(pageNum) ? pageNum : "1";
    pageSize = TextUtils.isNotBlank(pageSize) ? pageSize : "10";
    //  The total number of pages paged, the number of pages per page, sort mode, sort fields 
    Pageable StuPageable = PageUtils.buildPageRequest(Integer.valueOf(pageNum),Integer.valueOf(pageSize), new Sort(Direction.DESC, new String[] { "xmzbsj","lstProinfo.proId"}));
    //  Paging queries by condition, according to StuPageable Paging mode of 
     Page<Weekly> StuPage = proWeeklyDao.findAll(new Specification<Weekly>() {
      public Predicate toPredicate(Root<Weekly> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
        List<Predicate> lstPredicates = new ArrayList<Predicate>();
        if (TextUtils.isNotBlank(serArgs.get("xmmc"))) {
          lstPredicates.add(cb.like(root.get("lstProinfo").get("xmmc").as(String.class), "%" + serArgs.get("xmmc") + "%"));
        }
        if (TextUtils.isNotBlank(serArgs.get("xmzbqssj"))) {
          lstPredicates.add(cb.greaterThanOrEqualTo(root.get("xmzbsj").as(String.class),serArgs.get("xmzbqssj")));
        }
        if (TextUtils.isNotBlank(serArgs.get("xmzbjzsj"))) {
          lstPredicates.add(cb.lessThanOrEqualTo(root.get("xmzbsj").as(String.class),serArgs.get("xmzbjzsj")));
        }
        Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
        return cb.and(lstPredicates.toArray(arrayPredicates));
      }
    }, StuPageable);
     //  Paging queries by condition 
    resultMap = PageUtils.getPageMap(StuPage);
    return resultMap;
  }

The buildPageRequest() method, the imported package, is the self-written method below


import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

* @param pageNum  The current page 
   * @param pageSize  Number each page 
   * @param sortType  Sort field 
   * @param direction  Sort direction 
   */
  public static PageRequest buildPageRequest(int pageNum, int pageSize, String sortType, String direction) {
    Sort sort = null;

    if (!TextUtils.isNotBlank(sortType)) {
      return new PageRequest(pageNum - 1, pageSize);
    } else if (TextUtils.isNotBlank(direction)) {
      if (Direction.ASC.equals(direction)) {
        sort = new Sort(Direction.ASC, sortType);
      } else {
        sort = new Sort(Direction.DESC, sortType);
      }
      return new PageRequest(pageNum - 1, pageSize, sort);
    } else {
      sort = new Sort(Direction.ASC, sortType);
      return new PageRequest(pageNum - 1, pageSize, sort);
    }
  }
   public static PageRequest buildPageRequest(int pageNum, int pageSize, String sortType) {
     return buildPageRequest(pageNum, pageSize, sortType, null);
   }

getPageMap () method:

The Page of JPA is also the set, gets the values from the Page set, and finally gets these (key,value)


/**
   *  Encapsulates paging data to Map In the. 
   */
  public static Map<String, Object> getPageMap(Page<?> objPage) {
    Map<String, Object> resultMap = new HashMap<String, Object>();
    resultMap.put(Constants.PAGE_RESULT_LIST, objPage.getContent()); //  A collection of data, all recorded data that meets the query criteria 
    resultMap.put(Constants.PAGE_TOTAL_NUM, objPage.getTotalElements()); //  The total number of records 
    resultMap.put(Constants.PAGE_TOTAL_PAGE, objPage.getTotalPages()); //  Total number of pages 
    resultMap.put(Constants.PAGE_NUM, objPage.getNumber()); //  The current page number 
    resultMap.put(Constants.PAGE_SIZE, objPage.getSize()); //  Number of pages displayed 
    return resultMap;
  }

Related articles: