官网
https://pagehelper.github.io/docs/howtouse/
引入步骤
第1步:引入依赖
<!--分页插件-->
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.3.2</version>
</dependency>
第2步:配置拦截器插件
@Configuration
public class AppConfig {@Beanpublic PageInterceptor pageInterceptor() {PageInterceptor pageInterceptor = new PageInterceptor();Properties properties = new Properties();// 分页合理化,true开启,如果分页参数不合理会自动修正。默认false不启用properties.setProperty("reasonable", "true");pageInterceptor.setProperties(properties);return pageInterceptor;}}
[Ref] 整合PageHelper实现分页
第3步:使用
前提: 业务提供 查询所有学生的方法
// com.zhangziwa.practisesvr.mapper.UserMapper#listStudents
<select id="listStudents" resultMap="StudentMap">SELECT * FROM students
</select>// com.zhangziwa.practisesvr.mapper.UserMapper
List<Student> listStudents();
不使用分页插件 会查出全部
List<Student> students = userMapper.listStudents();
获取分页信息 方式1:
public List<Student> listStudents2(Integer pageNum, Integer PageSize) {PageHelper.startPage(1, 10, true);PageHelper.orderBy("age asc");List<Student> students = userMapper.listStudents();PageInfo<Student> studentPageInfo = PageInfo.of(students);return students;
}
获取分页信息 方式2:
public List<Student> listStudents2(Integer pageNum, Integer PageSize) {PageHelper.startPage(1, 10, true);PageHelper.orderBy("age asc");Page<Student> students = (Page<Student>) userMapper.listStudents();return students;
}
常见问题
[Q&A] 在系统中发现了多个分页插件