添加依赖
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
配置分页插件
在mybatis-config.xml里面配置分页插件,注意标签顺序
<plugins>
<!--设置分页插件-->
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
分页插件的使用
在查询功能之前使用
PageHelper.startPage(int pageNum, int pageSize)
开启分页功能- pageNum:当前页的页码
pageSize:每页显示的条数
@Test
public void testQBC() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
//访问第2页,每页5条数据
PageHelper.startPage(2, 5);
List<Emp> emps = mapper.selectByExample(null);
emps.forEach(System.out::println);
}
在查询之前得到的page对象, 和 在查询获取list集合之后的pageInfo对象, 都能得到关于分页的数据
@Test
public void testQBC() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
//访问第2页,每页5条数据
//PageHelper.startPage(2, 5);//可以直接分页,也可以用Page<Object>接收
//page包含一些关于分页的信息,比如总数,开始行数等....
Page<Object> page = PageHelper.startPage(2, 5);
List<Emp> emps = mapper.selectByExample(null);
//pageInfo比page包含更多的信息,参数1:分页之后的数据,参数2:页码数
PageInfo<Emp> pageInfo = new PageInfo<>(emps,2);
System.out.println(page);
System.out.println(pageInfo);
emps.forEach(System.out::println);
}
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}]
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]