第一步,导入依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.3</version>
</dependency>
第二步,在 mybatis-config.xml
核心配置文件中声明 PageInterceptor
<!--声明PageInterceptor-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"/>
</plugins>
第三步,测试
@Test
public void testPage() {
PageHelper.startPage(1,4); // 每页4条记录,查询第1页
List<Employee> employeeList = empMapper1.selectAll();
for (Employee employee : employeeList) {
log.info(employee.toString());
}
}
selectAll() 方法是查询数据库中所有数据。只要在调用此方法前,添加 PageHelper.startPage(1,4);
,返回的结果employeeList,就是分页查询的结果。