Spring Boot之PageHelper入门教程
1、首先导入PageHelper依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
2、application.properties中加入pageHelper配置
#分页插件
pagehelper.helper-dialect=mysql
pagehelper.params=count=countSql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
3、然后再代码中直接只用即可
@GetMapping("queryAll")
public PageInfo<User> selectAll(){
Page<User> page = PageHelper.startPage(2, 2);
List<User> users = userService.queryAllUser();
PageInfo<User> pageInfo = new PageInfo<User>(users);
pageInfo.getList().stream().forEach(System.out::println);
int pages = pageInfo.getPages();
System.out.println(pages);
users.stream().forEach(System.out::println);
return pageInfo;
}
返回结果如下
{"total":5,"list":[{"id":3,"name":"wangwu","pwd":"12345"},{"id":4,"name":"曹功德","pwd":"1321"}],
"pageNum":2,"pageSize":2,"size":2,"startRow":3,"endRow":4,"pages":3,"prePage":1,"nextPage":3,"isFirstPage":false,"isLastPage":false,
"hasPreviousPage":true,"hasNextPage":true,
"navigatePages":8,"navigatepageNums":[1,2,3],"navigateFirstPage":1,"navigateLastPage":3,"firstPage":1,"lastPage":3}