实现步骤:
插件地址:https://github.com/pagehelper/Mybatis-PageHelper
(1) maven 坐标
<!--pagehelper分页坐标--><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.1.10</version></dependency>
(2)在主配置文件 mybatisConfig.xml 中加入 plugin 配置。
在
<environments default="mysql">之前加入<plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor" /> </plugins>加入后: ```xml <?xml version=”1.0” encoding=”UTF-8” ?> <!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
**(3) PageHelper 对象**<br />查询语句之前调用 `PageHelper.startPage` 静态方法。
除了 PageHelper.startPage 方法外,还提供了类似用法的 PageHelper.offsetPage 方法。
在你需要进行分页的 MyBatis 查询方法前调用 PageHelper.startPage 静态方法即可,紧跟在这个方法后的第一个 **MyBatis 查询方法**会被进行分页。
```java
@Test
public void testSelect() throws IOException {
public void queryStudentList2() throws IOException {
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
ArrayList<Integer> list = new ArrayList<>();
Collections.addAll(list,1,2,3);
//获取第 1 页,2 条内容,
PageHelper.startPage(1,2);这方法下的sql执行方法会分页。
List<Student> students = studentDao.queryStudentList2(list);
students.forEach(student -> System.out.println(student));
}
}
