mybatis 整合pageHelper
1.引入依赖,注意引入springboot整合的依赖
如果不引入的不是springboot整合的依赖,需maven 引入插件
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
2 配置application.proterties
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql
3. 写sql语句
<select id="findAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_user
</select>
4 service实现分页
public PageInfo<User> findAll(int currentPage, int pageSize) {
PageHelper.startPage(currentPage,pageSize);
List<User> list = userMapper.findAll();
return new PageInfo<User>(list);
}
5.controller调用
@RequestMapping("page")
public PageInfo<User> page(int currentPage, int pageSize){
return userService.findAll(currentPage, pageSize);
}
6.调用结果显示
// 20190622090951
// http://127.0.0.1:8082/page?currentPage=3&pageSize=3
{
"pageNum": 3,
"pageSize": 3,
"size": 3,
"startRow": 7,
"endRow": 9,
"total": 10,
"pages": 4,
"list": [
{
"id": 9,
"username": "bajie",
"password": "123456",
"createTime": "2019-06-21T16:00:00.000+0000",
"trueName": "猪八戒",
"remarks": null,
"bz": null
},
{
"id": 10,
"username": "wukong",
"password": "123456",
"createTime": "2019-06-21T16:00:00.000+0000",
"trueName": "孙悟空",
"remarks": null,
"bz": null
},
{
"id": 11,
"username": "shasheng",
"password": "123456",
"createTime": "2019-06-21T16:00:00.000+0000",
"trueName": "沙悟净",
"remarks": null,
"bz": null
}
],
"prePage": 2,
"nextPage": 4,
"isFirstPage": false,
"isLastPage": false,
"hasPreviousPage": true,
"hasNextPage": true,
"navigatePages": 8,
"navigatepageNums": [
1,
2,
3,
4
],
"navigateFirstPage": 1,
"navigateLastPage": 4,
"firstPage": 1,
"lastPage": 4
}