1.在Maven配置PageHelper插件
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
2.在mybatis核心配置文件中配置PageHelper插件
- params:为了支持startPage(Object params)方法,增加了该参数来配置参数映射,用于从对象中根据属性名取值, 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默认值, 默认值为pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero。
<!--
plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
properties?, settings?,
typeAliases?, typeHandlers?,
objectFactory?,objectWrapperFactory?,
plugins?,
environments?, databaseIdProvider?, mappers?
-->
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
<property name="param1" value="value1"/>
</plugin>
</plugins>
3.使用
//设置分页参数
//PageHelper.startPage(1,2);,当前页数,每页数据数量
//在数据库连接的service类中
public PageInfo<Student> part(int currentPage, int pageSize) {
PageHelper.startPage(currentPage, pageSize);
//正常查询,则会根据上面,进行注入,按页查询
List<Student> all = dao.all();
//返回的是 PageInfo<Student>,是student类
PageInfo<Student> pageInfo = new PageInfo(all);
return pageInfo;
}
//使用PageHelper进行查询分页数据
PageHelper.startPage(currentPage, pageSize);
PageInfo<Student> part = server.part(currentPage, pageSize);
// let data = rz.data.list;
//将数据赋值给 tableData
//tb.tableData = data;
//tb.pagination.total= rz.data.total;