在自己引入使用PageHelper依赖,使用时发生了错误,后来发现PageHelper 在spring项目和springboot项目中用法是不一样的

springboot是有自带的版本的,所以只需要导入boot自带的就可以了。
springboot内置pagehelper,可以直接引用
1.pom.xml直接引用
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>2.在application.properties 进行配置
pagehelper.helper-dialect=mysql
#启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页
pagehelper.reasonable=true3.Controller层调用pagehelper,并返回PageInfo对象
@RequestMapping({"","/"})
public JsonResult<PageInfo<CartVO>> getCart(@RequestParam(value = "pn",defaultValue = "1")Integer pn,HttpSession session){
//pn表示当前页码,后者表示当前页面需要显示的数量
PageHelper.startPage(pn,3);
List<CartVO> cart = cartService.getCartByUid(getUidFromSession(session));
//传入查询到的数据,并设置显示页码的数量
PageInfo<CartVO> page = new PageInfo<>(cart,3);
return new JsonResult<>(OK,page);
}成功显示分页数据,返回结果如下:
{
"state": 200,
"msg": null,
"data": {
"total": 7,
"list": [
{
"cid": 7,
"uid": 14,
"pid": 10000016,
"price": 4602,
"num": 1,
"title": "戴尔(DELL)XPS13-9360-R1609 13.3高配版金色",
"realPrice": 4602,
"image": "/images/portal/12(DELL)XPS13gold/"
},
{
"cid": 6,
"uid": 14,
"pid": 10000013,
"price": 41,
"num": 2,
"title": "齐心(COMIX)C5902 A5优品商务笔记本子记事本日记本122张",
"realPrice": 41,
"image": "/images/portal/02COMIXC5902A5122blue/"
},
{
"cid": 5,
"uid": 14,
"pid": 10000022,
"price": 5119,
"num": 8,
"title": "联想(Lenovo)IdeaPad310经典版黑色",
"realPrice": 5119,
"image": "/images/portal/13LenovoIdeaPad310_black/"
}
],
"pageNum": 1,
"pageSize": 3,
"size": 3,
"startRow": 1,
"endRow": 3,
"pages": 3,
"prePage": 0,
"nextPage": 2,
"isFirstPage": true,
"isLastPage": false,
"hasPreviousPage": false,
"hasNextPage": true,
"navigatePages": 2,
"navigatepageNums": [
1,
2
],
"navigateFirstPage": 1,
"navigateLastPage": 2
}
}
至此,分页产生错误的问题已基本解决。

版权声明:本文为qq_53686534原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。