このプラグインは、Mybatis フレームワークから独立したサードパーティのプラグインです。
ページングプラグインの依存関係を追加する
ファイルpom.xml
に追加
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
プラグインの設定
mybatis
メイン設定ファイルのタグmybatis-config.xml
を通じてplugins
設定します。
<!-- plugins 配置myBatis插件 -->
<plugins>
<!-- 分页插件拦截器 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
ページネーションの例
テストクラス
@Test
public void queryStudentListByPluginPage() {
StudentDao studentDao = MyBatisUtil.getMapper(StudentDao.class); //sqlSession
// 在sql查询之前先设置分页信息,分页信息设置给sqlSession
// 设置之后,也就创建好了一个pageHelper的拦截器
PageHelper.startPage(2,4);
// mybatis在操作的时候就会检查有没有拦截器,
// 若有,在查询的时候就会把对应的分页信息设置到List<Student>中
List<Student> students = studentDao.queryStudentList();
// 通过new PageInfo 方法,获取和分页有关的信息值
PageInfo<Student> pageInfo = new PageInfo(students);
List<Student> list = pageInfo.getList();
for (Student student : list) {
System.out.println(student);
}
}