Mybatis 分页插件

一、分页插件使用步骤

1.1 添加依赖

<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper</artifactId>
  <version>5.2.0</version>
</dependency>

1.2 配置分页插件

<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>

二、分页插件的使用

1、在查询功能之前使用PageHelper.startPage(int pageNum, int pageSize)开启分页功能

  • pageNum:当前页的页码
  • pageSize:每页显示的条数

测试方法:

@Test
public void test07() throws IOException {
    SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try{
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        Page<Object> page = PageHelper.startPage(1, 2); // 返回 Page 对象
        List<Employee> allEmp = mapper.findAllEmp();
        for (Employee emp: allEmp) {
            System.out.println(emp);
        }
        System.out.println(page.getPageNum());  // 当前页数
        System.out.println(page.getPages());   // 总页数
        System.out.println(page.getPageSize());  // 每页显示几条数据
    } finally {
        sqlSession.close();
    }
}

控制台打印结果:

[main] [com.example.mapper.EmployeeMapper.findAllEmp_COUNT]-[DEBUG] ==>  Preparing: SELECT count(0) FROM tbl_employee 
[main] [com.example.mapper.EmployeeMapper.findAllEmp_COUNT]-[DEBUG] ==> Parameters: 
[main] [com.example.mapper.EmployeeMapper.findAllEmp_COUNT]-[DEBUG] <==      Total: 1
[main] [com.example.mapper.EmployeeMapper.findAllEmp]-[DEBUG] ==>  Preparing: select * from tbl_employee LIMIT ? 
[main] [com.example.mapper.EmployeeMapper.findAllEmp]-[DEBUG] ==> Parameters: 2(Integer)
[main] [com.example.mapper.EmployeeMapper.findAllEmp]-[DEBUG] <==      Total: 2
Employee{id=1, lastName='tomcat', email='tom@123.com', gender='0', department=null}
Employee{id=2, lastName='jerry', email='jerry@123.com', gender='0', department=null}
1
4
2

2、在查询获取list集合之后,使用PageInfo pageInfo = new PageInfo<>(List list, int navigatePages)获取分页相关数据

  • list:分页之后的数据
  • navigatePages:导航分页的页码数

测试代码:

@Test
public void test07() throws IOException {
    SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try{
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        Page<Object> page = PageHelper.startPage(1, 2); // 返回 Page 对象
        List<Employee> allEmp = mapper.findAllEmp();

        PageInfo<Employee> pageInfo = new PageInfo<>(allEmp, 5);
        System.out.println(pageInfo.toString());
        System.out.println(pageInfo.getNextPage());
    } finally {
        sqlSession.close();
    }
}

控制台输出结果:

[main] [com.example.mapper.EmployeeMapper.findAllEmp_COUNT]-[DEBUG] ==>  Preparing: SELECT count(0) FROM tbl_employee 
[main] [com.example.mapper.EmployeeMapper.findAllEmp_COUNT]-[DEBUG] ==> Parameters: 
[main] [com.example.mapper.EmployeeMapper.findAllEmp_COUNT]-[DEBUG] <==      Total: 1
[main] [com.example.mapper.EmployeeMapper.findAllEmp]-[DEBUG] ==>  Preparing: select * from tbl_employee LIMIT ? 
[main] [com.example.mapper.EmployeeMapper.findAllEmp]-[DEBUG] ==> Parameters: 2(Integer)
[main] [com.example.mapper.EmployeeMapper.findAllEmp]-[DEBUG] <==      Total: 2
PageInfo{pageNum=1, pageSize=2, size=2, startRow=1, endRow=2, total=8, pages=4, 
list=Page{count=true, pageNum=1, pageSize=2, startRow=0, endRow=2, total=8, pages=4, reasonable=false, pageSizeZero=false}
[Employee{id=1, lastName='tomcat', email='tom@123.com', gender='0', department=null}, 
Employee{id=2, lastName='jerry', email='jerry@123.com', gender='0', department=null}], 
prePage=0, nextPage=2, isFirstPage=true, isLastPage=false, hasPreviousPage=false, hasNextPage=true, navigatePages=5, navigateFirstPage=1, navigateLastPage=4, navigatepageNums=[1, 2, 3, 4]}
2

常用数据:

  • pageNum:当前页的页码
  • pageSize:每页显示的条数
  • size:当前页显示的真实条数
  • total:总记录数
  • pages:总页数
  • prePage:上一页的页码
  • nextPage:下一页的页码
  • isFirstPage/isLastPage:是否为第一页/最后一页
  • hasPreviousPage/hasNextPage:是否存在上一页/下一页
  • navigatePages:导航分页的页码数
  • navigatepageNums:导航分页的页码,[1,2,3,4,5]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值