我之所以会发现这个PageHelper这个东东 是因为公司在使用 ,刚开始我也没太注意这个插件,感觉不就是个分页插件吗?也就那样,直到一天,我在网上找了个代码生成器,用来构建代码,因为它是针对mysql的,我们公司使用的是pgsql,它们的分页是不同的。我下载的代码生成器的是针对mysql的。所以我又修改了它的分页语句。忽然想到了这个PageHelper、感觉这个东西还是很方便的,不用再去改sql了
引入依赖
这里我直接那我之前用来整合mybatis的项目简单演示下
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
application.yml
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
修改controller
@GetMapping(value = "/users")
public PageInfo<UserEntity> getUsers() {
PageHelper.startPage(1, 10);
List<UserEntity> users=userMapper.getAll();
PageInfo<UserEntity> pageInfo = new PageInfo<UserEntity>(users);
return pageInfo;
}
访问验证
{
"pageNum": 1,
"pageSize": 10,
"size": 2,
"startRow": 1,
"endRow": 2,
"total": 2,
"pages": 1,
"list": [
{
"id": 28,
"userName": "毛毛",
"passWord": "1234",
"userSex": "MAN",
"nickName": "324"
},
{
"id": 29,
"userName": "12",
"passWord": "12",
"userSex": "MAN",
"nickName": null
}
],
"prePage": 0,
"nextPage": 0,
"isFirstPage": true,
"isLastPage": true,
"hasPreviousPage": false,
"hasNextPage": false,
"navigatePages": 8,
"navigatepageNums": [
1
],
"navigateFirstPage": 1,
"navigateLastPage": 1,
"lastPage": 1,
"firstPage": 1
}
当然,这只是个简单入门整合,但是已经足够大家进行使用了,好了玩的开心!