SSS项目专题(一):分页查询

本文主要介绍了SSS项目中分页查询的实现,包括需求分析、分页查询的四个关键功能:分页、左右翻页、页面大小调整和跳转翻页。通过Specification和PageRequest进行条件封装与排序设置,Controller层使用PageUtils工具类,前端展示页码。同时,详细阐述了左右翻页和页面大小调整的实现细节,以及如何实现跳转翻页功能。
摘要由CSDN通过智能技术生成

1.需求分析

[外链图片转存失败(img-aZgAHKuA-1565268401747)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1564995573616.png)]

简单的分页查询需要实现以上四个功能:①分页②左右翻页③自定义页面大小④跳转翻页

2.分页查询

①分页

public interface DepartDao extends JpaRepository<Depart,Long>,JpaSpecificationExecutor<Depart>{
   
}
//继承的JpaSpecificationExecutor接口具有分页功能

利用Specification类的对象来封装查询条件

//接口 匿名内部类 专门负责封装查询条件的
Specification specification= new Specification() {
   
            // root 要查询实体类
            // criteriaQuery 条件
            // CriteriaBuilder 对象. 用于创建 Criteria 相关对象的工厂. 当然可以从中获取到
            // Predicate 类型, 代表一个查询条件
            @Override
      public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder cb) {
   
             Predicate p1 = cb.notEqual(root.get("id"), 15);     //条件1   id!=15
             Predicate p2 = cb.like(root.get("email"), "%163.com");   // 条件2 email like '%163.com'
                return cb.or(p1,p2);
            }
        };

PageRequest可以选择排序规则

Sort.Order order1=new Sort.Order(Sort.Direction.DESC, "id");  // 按id降序
Sort.Order order2=new Sort.Order(Sort.Direction.ASC, "age"); // 按age升序

Sort sort = new Sort(order1, order2); //Sort封装了排序规则

//分页条件对象
Pageable pageable = new PageRequest(pageIndex-1, pageSize,sort);

//specification查询条件封装的对象
//pageable分页条件
Page<Depart> page = userDao.findAll(specification,pageable);//带条件分页
//或取到当页数据集合
List<Depart> list = page.getContent();

Controller层,此处利用到了封装的分页工具类PageUtils

@RequestMapping("/list/{pageIndex}/{pageSize}")
public String getAllDepart(@PathVariable("pageIndex") int pageIndex,@PathVariable("pageSize") 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值