其他分享
首页 > 其他分享> > Spring Data JPA Projections -- JPA分页查询

Spring Data JPA Projections -- JPA分页查询

作者:互联网

一、单表:分页 + 排序 + 动态查询

  1. repository继承JpaSpecificationExecutor<T>接口;
    • 最终调用Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable)实现分页功能
  2. 构造Specification<T> spec;
    • 搜索Specification能得到很多相关网页
      interface Specification<T> {
          Predicate toPredicate(Root<T> root, CriteriaQuery query, CriteriaBuilder cb);
      }
  3. 构造Pageable pageable,
    • Pageable是可以带Sort的
      Pageable sortedByName = 
        PageRequest.of(0, 3, Sort.by("name"));
       
      Pageable sortedByPriceDesc = 
        PageRequest.of(0, 3, Sort.by("price").descending());
       
      Pageable sortedByPriceDescNameAsc = 
        PageRequest.of(0, 5, Sort.by("price").descending().and(Sort.by("name")));
  4. 等待补充

二、多表:分页 + 排序 + 动态查询

  1. 选择主表
    • 一般选择带“排序字段”的表作为主表
  2. 辅表操作进行动态查询
    • 当做单表动态查询进行处理,获取逻辑外键的Set集合;
  3. 主表操作

标签:Sort,Pageable,PageRequest,JPA,Spring,Specification,查询,Projections,主表
来源: https://www.cnblogs.com/echo1937/p/13151837.html