一、添加依赖
-
-
-
com.github.pagehelper -
pagehelper-spring-boot-starter -
二、使用
Paperhelper框架完成分页的功能原理是sql语句后自动添加limit。
- @Select("select *from order_tbl")
- List
findAllOrders();
- //分页查询所有订单信息的方法
- //参数page是页码,pageSize是每页条数
- public PageInfo
getAllOrdersByPage(Integer page, Integer pageSize) { - //pagehepler框架实现分页最核心代码,是要编写在执行查询数据代码之前
- PageHelper.startPage(page, pageSize);
- //上面设置好分页执行条件,下面的查询在执行时,SQL会自动追加limit关键字
- List
list = orderMapper.findAllOrders(); - return new PageInfo<>(list);
- }
三、调用
- @GetMapping("/page")
- @ApiOperation("分查询所有订单")
- @ApiImplicitParams({
- @ApiImplicitParam(value = "页码", name = "page", example = "1"),
- @ApiImplicitParam(value = "每页条数", name = "pageSize", example = "10")
- })
- public JsonResult
> pageOrder(Integer page, Integer pageSize) { - PageInfo
allOrdersByPage = orderService.getAllOrdersByPage(page, pageSize); - return JsonResult.ok("查询完成", allOrdersByPage);
- }
返回结果不只是数据还包括分页信息
四、返回结果,利用在线文档接受服务器返回的page信息
{
"state": 200,
"message": null,
"data": {
"total": 22,
"list": [
{
"id": 44,
"userId": "UU100",
"commodityCode": "PC100",
"count": 100,
"money": 20
},
{
"id": 45,
"userId": "UU100",
"commodityCode": "PC100",
"count": 100,
"money": 20
}
],
"pageNum": 3,
"pageSize": 10,
"size": 2,
"startRow": 21,
"endRow": 22,
"pages": 3,
"prePage": 2,
"nextPage": 0,
"isFirstPage": false,
"isLastPage": true,
"hasPreviousPage": true,
"hasNextPage": false,
"navigatePages": 8,
"navigatepageNums": [
1,
2,
3
],
"navigateFirstPage": 1, //前端默认首页
"navigateLastPage": 3 //前端默认最后一页
}
}