1. 依赖jar包(https://blog.csdn.net/qq_16517483/article/details/72803043)
使用 PageHelper 你只需要在 classpath 中包含 pagehelper-x.x.x.jar 和 jsqlparser-0.9.5.jar。
2.mybatis-config.xml(https://blog.csdn.net/s592652578/article/details/78179998?locationNum=4&fps=1)
4.0.0版本之前:
- <plugins>
- <--
- <plugin interceptor="com.github.pagehelper.PageHelper">
- <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
- <property name="dialect" value="Oracle"/>
- </plugin>
- </plugins>
- <plugins>
- <plugin interceptor="com.github.pagehelper.PageInterceptor">
- <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库, 4.0.0以后版本支持自动识别数据库-->
- <!-- <property name="dialect" value="Oracle"/>-->
- </plugin>
- </plugins>
3.PageHandler
public void testPageHelper() {
//这句一定要写在sql执行之前,只对紧跟的第一条语句起作用
PageHelper.startPage(1, 5,"id desc");
List<Dept> deptList = session.selectList("DEPT.getAllDept");
PageInfo<Dept> page = new PageInfo<>(deptList);
System.out.println("总页数:" + page.getPages());
System.out.println("页码:" + page.getPageNum());
System.out.println("总条数:"+page.getTotal());
System.out.println(deptList.size());
for (Dept dept : deptList) {
System.out.println(dept.getName());
}
}