SpringBoot使用pageHelper
2018-04-19 本文已影响723人
lbcBoy
pom文件增加依赖(只service需要)
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
<!--pageHelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
<exclusions>
<exclusion>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
**注意解决依赖冲突**
application.proprieties
pagehelper.helperDialect=mysql
pagehelper.reasonable=true #为了使用输入页数为负或者超出最大页时候使页数为最小或最大值
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
代码范例
service
public PageInfo findAllUser(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> users = userMapper.selectAllUser();
List<UserDto> userDtos = new ArrayList<>();
users.forEach(user -> {
userDtos.add(dozerBeanMapper.map(user, UserDto.class));
});
// 用PageInfo对结果进行包装
PageInfo pageInfo = new PageInfo(users);
pageInfo.setList(userDtos);
return pageInfo;
}
controller
@ResponseBody
@RequestMapping(value = "/all/{pageNum}/{pageSize}", produces = {"application/json;charset=UTF-8"})
public PageInfo findAllUser(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) {
logger.info(String.valueOf(userService.findAllUser(pageNum, pageSize)));
return userService.findAllUser(pageNum, pageSize);
}
请求及返回结果
{
"pageNum": 0,
"pageSize": 5,
"size": 5,
"startRow": 1,
"endRow": 5,
"total": 14,
"pages": 3,
"list": [
{
"userId": 1000,
"userName": "姓名",
"password": "123456",
"phone": "18123568978"
},
{
"userId": 1001,
"userName": "姓名",
"password": "123456",
"phone": "18123568978"
},
{
"userId": 1002,
"userName": "姓名",
"password": "123456",
"phone": "18123568978"
},
{
"userId": 1003,
"userName": "姓名",
"password": "123456",
"phone": "18123568978"
},
{
"userId": 1004,
"userName": "姓名",
"password": "123456",
"phone": "18123568978"
}
],
"prePage": 0,
"nextPage": 1,
"isFirstPage": false,
"isLastPage": false,
"hasPreviousPage": false,
"hasNextPage": true,
"navigatePages": 8,
"navigatepageNums": [
1,
2,
3
],
"navigateFirstPage": 1,
"navigateLastPage": 3,
"firstPage": 1,
"lastPage": 3
}