1.依赖
引入pagehelper依赖后
需要排除Mybatis的依赖, 否则会启动报错
注:如果是多模块, 此依赖需要放入到父pom里,不然项目启动不了
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
2.配置文件
yml里配置
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
page-size-zero: true
params: count=countSql
3.代码实现
设定 startPage
查询数据
返回PageInfo
@GetMapping("/getAllIp")
public PageInfo<IpBean> getAllIp() throws IOException {
//分页
PageHelper.startPage(1 ,10);
List<IpBean> IpBeanList=null;
List<IpBean> ipBeanList = ipBeanService.list();
PageInfo<IpBean> pageInfo = new PageInfo(ipBeanList);
return pageInfo;
}
4 结果
{
"endRow": 1,
"firstPage": 1,
"hasNextPage": true,
"hasPreviousPage": false,
"isFirstPage": true,
"isLastPage": false,
"lastPage": 2,
"list": [
{
"createId": "",
"createName": "",
"createTime": "2020-09-03 19:07:24",
"goodsCategory": "",
"goodsDesc": "3333",
"goodsExpressType": "",
"goodsHeadImg": "",
"goodsId": "",
"goodsName": "1111",
"goodsSwipersImg": "",
"id": "3934e9510594211e3a9b5e2ad0d5365d",
"lastUpdateId": "",
"lastUpdateName": "",
"oemId": "",
"spare1": "",
"spare2": "",
"spare3": "",
"spare4": "",
"spare5": ""
}
],
"navigateFirstPage": 1,
"navigateLastPage": 2,
"navigatePages": 8,
"navigatepageNums": [
1,
2
],
"nextPage": 2,
"pageNum": 1,
"pageSize": 1,
"pages": 2,
"prePage": 0,
"size": 1,
"startRow": 1,
"total": 2
}
5 PageHelper分页插件上的PageInfo类属性详解
private int pageNum;//当前页码
private int pageSize;//设置每页多少条数据
private int size;//当前页有多少条数据
private int startRow;//当前页码第一条数据的
private int endRow;//当前页码的开始条
private int pages;//当前页码结束条
private int prePage;//上一页(页面链接使用)
private int nextPage;//下一页(页面链接使用)
private boolean isFirstPage;//是否为第一页
private boolean isLastPage;//是否为最后一页
private boolean hasPreviousPage;//是否有前一页
private boolean hasNextPage;//是否有下一页
private int navigatePages;//导航页码数(就是总共有多少页)
private int[] navigatepageNums;//导航页码数(就是总共有多少页),可以用来遍历
private int navigateFirstPage;//首页号
private int navigateLastPage;//尾页号