開始用到SpringData分頁,在跟着資料寫項目的時候,看到分頁的數量和返回的不一致。
一.使用Page.getSize
@RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
public Result findSearch(@RequestBody Map searchMap,@PathVariable int page,@PathVariable int size) {
Page<Label> pageList = labelService.findSearch(searchMap, page, size);
PageResult pageResult = new PageResult<Label>(pageList.getTotalElements(), pageList.getContent());// 資料中使用的是這種,Page.getTotalElements()
return new Result(true, StatusCode.OK, "查詢成功", pageResult);
}
分頁結果:主要是total不同,這裏的total不等於當前的rows的大小。
{
"flag": true,
"code": 20000,
"message": "查詢成功",
"data": {
"total": 9,
"rows": [
{
"id": "3",
"labelname": "C++",
"state": "1",
"count": null,
"recommend": "2",
"fans": null
},
{
"id": "4",
"labelname": "python",
"state": "1",
"count": null,
"recommend": "2",
"fans": null
}
]
}
}
二.使用Page.getTotalElements
@RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
public Result findSearch(@RequestBody Map searchMap,@PathVariable int page,@PathVariable int size) {
Page<Label> pageList = labelService.findSearch(searchMap, page, size);
PageResult pageResult = new PageResult<Label>(Long.valueOf(pageList.getSize()), pageList.getContent());// 我自己修改後的,使用Page.getSize()
return new Result(true, StatusCode.OK, "查詢成功", pageResult);
}
我的分頁結果:total的值等於rows的大小。
{
"flag": true,
"code": 20000,
"message": "查詢成功",
"data": {
"total": 2,
"rows": [
{
"id": "3",
"labelname": "C++",
"state": "1",
"count": null,
"recommend": "2",
"fans": null
},
{
"id": "4",
"labelname": "python",
"state": "1",
"count": null,
"recommend": "2",
"fans": null
}
]
}
}
暫時先記錄一下,兩者的區別等項目完成後,看源碼找答案。