一、分页插件配置步骤
1.1 添加依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
1.2 配置分页插件
- 在MyBatis的核心配置文件(mybatis-config.xml)中配置插件
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
二、分页插件的使用步骤
2.1 开启分页功能
- 在查询功能之前使用
PageHelper.startPage(int pageNum, int pageSize) 开启分页功能
- pageNum:当前页的页码
- pageSize:每页显示的条数
@Test
public void testPageHelper() 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);
PageHelper.startPage(1,4);
List<Emp> emps = mapper.selectByExample(null);
emps.forEach(System.out::println);
}
输出:
DEBUG 04-20 19:18:15,001 Cache Hit Ratio [SQL_CACHE]: 0.0 (LoggingCache.java:60)
DEBUG 04-20 19:18:15,117 ==> Preparing: SELECT count(0) FROM t_emp (BaseJdbcLogger.java:137)
DEBUG 04-20 19:18:15,186 ==> Parameters: (BaseJdbcLogger.java:137)
DEBUG 04-20 19:18:15,256 <== Total: 1 (BaseJdbcLogger.java:137)
DEBUG 04-20 19:18:15,258 ==> Preparing: select eid, emp_name, age, sex, email, did from t_emp LIMIT ? (BaseJdbcLogger.java:137)
DEBUG 04-20 19:18:15,259 ==> Parameters: 4(Integer) (BaseJdbcLogger.java:137)
DEBUG 04-20 19:18:15,262 <== Total: 4 (BaseJdbcLogger.java:137)
Emp{eid=1, empName='admin', age=22, sex='null', email='456@qq.com', did=3}
Emp{eid=2, empName='admin2', age=22, sex='女', email='456@qq.com', did=3}
Emp{eid=3, empName='王五', age=13, sex='男', email='14@qq.com', did=3}
Emp{eid=4, empName='赵六', age=25, sex='女', email='32@qq.com', did=1}
2.2 分页相关数据
2.2.1 方法一:直接输出
@Test
public void testPageHelper() 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);
Page<Object> page = PageHelper.startPage(1, 4);
List<Emp> emps = mapper.selectByExample(null);
System.out.println(page);
}
输出数据:
Page{count=true, pageNum=1, pageSize=4, startRow=0, endRow=4,
total=8, pages=2, reasonable=false, pageSizeZero=false}[
Emp{eid=1, empName='admin', age=22, sex='null', email='456@qq.com', did=3},
Emp{eid=2, empName='admin2', age=22, sex='女', email='456@qq.com', did=3},
Emp{eid=3, empName='王五', age=13, sex='男', email='14@qq.com', did=3},
Emp{eid=4, empName='赵六', age=25, sex='女', email='32@qq.com', did=1}]
2.2.2 方法二:使用PageInfo
@Test
public void testPageHelper() 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);
PageHelper.startPage(1, 4);
List<Emp> emps = mapper.selectByExample(null);
PageInfo<Emp> page = new PageInfo<>(emps,5);
System.out.println(page);
}
输出:
PageInfo{pageNum=1, pageSize=4, size=4, startRow=1, endRow=4, total=8, pages=2,
list=Page{count=true, pageNum=1, pageSize=4, startRow=0, endRow=4, total=8, pages=2, reasonable=false, pageSizeZero=false}[
Emp{eid=1, empName='admin', age=22, sex='null', email='456@qq.com', did=3}, Emp{eid=2, empName='admin2', age=22, sex='女', email='456@qq.com', did=3},
Emp{eid=3, empName='王五', age=13, sex='男', email='14@qq.com', did=3},
Emp{eid=4, empName='赵六', age=25, sex='女', email='32@qq.com', did=1}],
prePage=0, nextPage=2, isFirstPage=true, isLastPage=false, hasPreviousPage=false, hasNextPage=true, navigatePages=5, navigateFirstPage=1, navigateLastPage=2, navigatepageNums=[1, 2]}
2.2.3 分页常用数据
- pageNum:当前页的页码
- pageSize:每页显示的条数
- size:当前页显示的真实条数
- total:总记录数
- pages:总页数
- prePage:上一页的页码
- nextPage:下一页的页码
- isFirstPage/isLastPage:是否为第一页/最后一页
- hasPreviousPage/hasNextPage:是否存在上一页/下一页
- navigatePages:导航分页的页码数
- navigatepageNums:导航分页的页码,[1,2,3,4,5]
本文主要参考: 【尚硅谷】2022版MyBatis教程(细致全面,快速上手)
|