一、分页插件使用步骤
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);
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);
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]
|