添加依赖

  1. <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
  2. <dependency>
  3. <groupId>com.github.pagehelper</groupId>
  4. <artifactId>pagehelper</artifactId>
  5. <version>5.2.0</version>
  6. </dependency>

配置分页插件

在mybatis-config.xml里面配置分页插件,注意标签顺序

  1. <plugins>
  2. <!--设置分页插件-->
  3. <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  4. </plugins>

分页插件的使用

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

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

      1. @Test
      2. public void testQBC() throws IOException {
      3. InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
      4. SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
      5. SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
      6. SqlSession sqlSession = sqlSessionFactory.openSession(true);
      7. EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
      8. //访问第2页,每页5条数据
      9. PageHelper.startPage(2, 5);
      10. List<Emp> emps = mapper.selectByExample(null);
      11. emps.forEach(System.out::println);
      12. }

      image.png

      在查询之前得到的page对象, 和 在查询获取list集合之后的pageInfo对象, 都能得到关于分页的数据

  1. @Test
  2. public void testQBC() throws IOException {
  3. InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
  4. SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
  5. SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
  6. SqlSession sqlSession = sqlSessionFactory.openSession(true);
  7. EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
  8. //访问第2页,每页5条数据
  9. //PageHelper.startPage(2, 5);//可以直接分页,也可以用Page<Object>接收
  10. //page包含一些关于分页的信息,比如总数,开始行数等....
  11. Page<Object> page = PageHelper.startPage(2, 5);
  12. List<Emp> emps = mapper.selectByExample(null);
  13. //pageInfo比page包含更多的信息,参数1:分页之后的数据,参数2:页码数
  14. PageInfo<Emp> pageInfo = new PageInfo<>(emps,2);
  15. System.out.println(page);
  16. System.out.println(pageInfo);
  17. emps.forEach(System.out::println);
  18. }
  1. Page{count=true, pageNum=2, pageSize=5, startRow=5, endRow=10, total=10, pages=2, reasonable=false, pageSizeZero=false}[Emp{eid=6, empName='发的', age=43, sex='男', email='123@qq.com', did=3}, Emp{eid=7, empName='是', age=23, sex='女', email='123@qq.com', did=1}, Emp{eid=8, empName='水电费', age=32, sex='男', email='123@qq.com', did=3}, Emp{eid=9, empName='官方', age=22, sex='女', email='123@qq.com', did=2}, Emp{eid=10, empName='几节课', age=33, sex='男', email='123@qq.com', did=1}]
  1. PageInfo{pageNum=2, pageSize=5, size=5, startRow=6, endRow=10, total=10, pages=2, list=Page{count=true, pageNum=2, pageSize=5, startRow=5, endRow=10, total=10, pages=2, reasonable=false, pageSizeZero=false}[Emp{eid=6, empName='发的', age=43, sex='男', email='123@qq.com', did=3}, Emp{eid=7, empName='是', age=23, sex='女', email='123@qq.com', did=1}, Emp{eid=8, empName='水电费', age=32, sex='男', email='123@qq.com', did=3}, Emp{eid=9, empName='官方', age=22, sex='女', email='123@qq.com', did=2}, Emp{eid=10, empName='几节课', age=33, sex='男', email='123@qq.com', did=1}], prePage=1, nextPage=0, isFirstPage=false, isLastPage=true, hasPreviousPage=true, hasNextPage=false, navigatePages=2, navigateFirstPage=1, navigateLastPage=2, navigatepageNums=[1, 2]}

常用的PageInfo属性

  • pageNum:当前页的页码
  • pageSize:每页显示的条数
  • size:当前页显示的真实条数
  • total:总记录数
  • pages:总页数
  • prePage:上一页的页码
  • nextPage:下一页的页码
  • isFirstPage/isLastPage:是否为第一页/最后一页
  • hasPreviousPage/hasNextPage:是否存在上一页/下一页
  • navigatePages:导航分页的页码数
  • navigatepageNums:导航分页的页码,[1,2,3,4,5]