10.1 添加分页插件的依赖
<!-- pagehelper分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
10.2 配置插件
在mybatis的主配置文件mybatis-config.xml
中通过plugins
标签进行配置
<!--plugins标签,用于配置MyBatis插件(分页插件)-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
10.3 分页实例
对学生信息进行分页查询
@Test
public void testListStudentsByPage() {
StudentDAO studentDAO = MyBatisUtil.getMapper(StudentDAO.class); //sqlSession
PageHelper.startPage(2,4);
List<Student> students = studentDAO.listStudents();
PageInfo<Student> pageInfo = new PageInfo<Student>(students);
//pageInfo中就包含了数据及分页信息
}
带条件分页
@Test
public void testListStudentsByPage() {
StudentDAO studentDAO = MyBatisUtil.getMapper(StudentDAO.class); //sqlSession
PageHelper.startPage(2,4);
//List<Student> students = studentDAO.listStudents();
List<Student> list = studentDAO.listStudentsByGender("女");
PageInfo<Student> pageInfo = new PageInfo<Student>(list);
//pageInfo中就包含了数据及分页信息
}