-
pom.xml文件添加引用,注意此种分页不是基于原生PageHelper分页
<!--pageHelper--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency>
- 属性文件,添加如下配置,注意此配置可不添加,添加作用是为了处理特殊情况
pagehelper.helperDialect=mysql pagehelper.reasonable=true #为了使用输入页数为负或者超出最大页时候使页数为最小或最大值 pagehelper.supportMethodsArguments=true pagehelper.params=count=countSql
- 分页关键代码
@ApiOperation("查询列表") @PostMapping("/selectList") @JwtIgnore public Result selectList(@RequestBody PageRequest request) { Page page = PageHelper.startPage(request.pageNum,request.pageSize); //这行是重点(放在方法第一行)表示从pageNum页开始,每页pageSize条数据 List<User_info> _list= userService.selectList();//返回集合和分页之前不要有其它代码,这两行上下要紧靠近 PageInfo pageInfo = new PageInfo<>(_list); return Result.SUCCESS( pageInfo); }
@Override public List<User_info> selectList() { return uMapper.selectList(); }
<!---查询列表--> <select id="selectList" resultType="User_info"> select * from User_info </select>
使用postman接口测试工具,调用效果
{ "code": 1, "message": "操作成功!", "data": { "pageNum": 1, "pageSize": 3, "size": 3, "startRow": 1, "endRow": 3, "total": 5, "pages": 2, "list": [ { "id": "43591639-e5fb-4369-b4d6-aab12a982ebf", "create_time": "2020-08-19T08:06:57.000+00:00", "name": "string1" }, { "id": "66bafe88-b716-48d5-8cfb-b91a881a355a", "create_time": "2020-08-19T07:25:50.000+00:00", "name": "string1" }, { "id": "6afb9f4c-7a95-45fb-881b-543e2dcc2c72", "create_time": "2020-08-19T07:38:52.000+00:00", "name": "string1" } ], "prePage": 0, "nextPage": 2, "isFirstPage": true, "isLastPage": false, "hasPreviousPage": false, "hasNextPage": true, "navigatePages": 8, "navigatepageNums": [ 1, 2 ], "navigateFirstPage": 1, "navigateLastPage": 2, "firstPage": 1, "lastPage": 2 } }