因为我之前用的是SSM框架写的我们的项目,但是只有因为技术主管让我们做成微服务的架构,所以现在是又用springboot把之前的项目重新编写了一遍,之前自己在SSM框架里面编写分页查询是通过pagehelper这个插件实现的,但是之后将项目迁移到springboot上面的时候却出现所有的分页查询都不能使用了.
当时前端的同事告诉我这个消息的时候,我瞬间就觉得是不是他们的问题,
因为我这边的逻辑什么的都没有改,但是在swagger里面测试了之后,页面上没有报错,是能够正常运行的,但就是无法正常的分页.我就觉得可能还是我这边后台的原因,毕竟他们能够正常读取到数据,说明他们那边肯定是通了的,于是自己查了一下发现分页失效的确是我这边的问题,沃土了,又是我这边错了
但是谁让我是打不死的小强呢,有bug就解决呗,实在解决不了就删库跑路呗.
哈哈哈,说着玩的,查完资料之后发现,主要就是下面这两个原因.希望能够对你们有所帮助.
这个是我遇到的问题.
在SSM框架中使用pagehelper插件,只需要导入这一个依赖就能够正常实现分页查询的功能
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
但是在springboot中使用pagehelper则需要导入一下三个依赖才行
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
pagehelper插件的具体使用我在我的这篇博客:前后端分离使用pagehelper
里面详细说过,有兴趣的朋友可以去看看.
使用pagehelper插件的时候,逻辑一般是如下图所示:
再通过代码来举个小栗子给大家看一下:
public void selectAllByPage(Map map){
Integer page=(Integer)map.get("page");
Integer size=(Integer)map.get("size");
PageHelper.startPage(page,size);
List<RoleDao> roleDaoList = roleService.selectAllByPage(page,size);
PageInfo<RoleDao> pageInfo=new PageInfo<>(roleDaoList);
}
这样我们就能够实现分页查询了,这里面要 注意的就是查询语句顺序的问题 ,PageHelper默认是将紧跟在他后面的查询语句做分页查询的
,所以如果你在需要做分页查询的语句之前又添加了另外一句查询语句,那么PageHelper就会自动将之前的查询语句进行分页的操作.
linux之父经典名言-----talk is cheap show me the code
还是通过下面的小栗子来帮助大家理解.
public RestResult selectAllByPage(@RequestBody @ApiParam("包含page,size参数即可")Map map){
RestResult restResult=new RestResult();
Integer page=(Integer)map.get("page");
Integer size=(Integer)map.get("size");
PageHelper.startPage(page,size);
List<ArchTypeDao> archTypeDaoList=archTypeService.selectAllByPage(page,size);
List<ArchTypeDao> archTypeDaoList1=archTypeService.selectAll();
PageInfo<ArchTypeDao>pageInfo=new PageInfo<>(archTypeDaoList);
int pageNum=pageInfo.getPageNum();
int pages=pageInfo.getPages();
Map<String,Object>map1=new HashMap<>();
map1.put("list",archTypeDaoList);
map1.put("total",archTypeDaoList1.size());
map1.put("pageNum",pageNum);
map1.put("pages",pages);
if(archTypeDaoList!=null) {
restResult.success(map1);
}
else{
restResult.fail("查询失败!");
}
return restResult;
}
我们的返回信息是selectAllByPage方法查询出来的数据,这次我们是先进行selectAllByPage方法,再进行selectAll.我们来看一下查询出来的数据
我们的确是正常分页查询查询出来的数据
接下来我们再看看交换两个查询语句的代码
public RestResult selectAllByPage(@RequestBody @ApiParam("包含page,size参数即可")Map map){
RestResult restResult=new RestResult();
Integer page=(Integer)map.get("page");
Integer size=(Integer)map.get("size");
PageHelper.startPage(page,size);
List<ArchTypeDao> archTypeDaoList1=archTypeService.selectAll();
List<ArchTypeDao> archTypeDaoList=archTypeService.selectAllByPage(page,size);
PageInfo<ArchTypeDao>pageInfo=new PageInfo<>(archTypeDaoList);
int pageNum=pageInfo.getPageNum();
int pages=pageInfo.getPages();
Map<String,Object>map1=new HashMap<>();
map1.put("list",archTypeDaoList);
map1.put("total",archTypeDaoList1.size());
map1.put("pageNum",pageNum);
map1.put("pages",pages);
if(archTypeDaoList!=null) {
restResult.success(map1);
}
else{
restResult.fail("查询失败!");
}
return restResult;
}
我们的返回信息仍然是selectAllByPage方法查询出来的数据,这次我们是先进行selectAll方法,再进行selectAllByPage.我们来看一下查询出来的数据
显然数据是没有进行过分页操作的.
但是我自己之后又在另一个controller测试了一下,但是结果与我想象的有不太一样
这是代码
public RestResult selectAllByPage(@RequestBody Map map){
RestResult restResult=new RestResult();
Integer page=(Integer)map.get("page");
Integer size=(Integer)map.get("size");
PageHelper.startPage(page,size);
List<AreaDao> areaDaoList1=areaService.selectAll();
List<AreaDao> areaDaoList=areaService.selectAllByPage(page,size);
PageInfo<AreaDao> pageInfo=new PageInfo<>(areaDaoList);
int pageNum=pageInfo.getPageNum();
int pages=pageInfo.getPages();
Map<String,Object>map1=new HashMap<>();
map1.put("list",areaDaoList);
map1.put("total",areaDaoList1.size());
map1.put("pageNum",pageNum);
map1.put("pages",pages);
if(areaDaoList!=null) {
restResult.success(map1);
}
else{
restResult.fail("查询失败!");
}
return restResult;
}
返回的信息仍然是selectAllByPage方法返回的数据,但是我们是先进行selectAll方法,再进行selectAllByPage,我们来看反馈的结果:
所以说这种bug是 有时出现,有时候又不出现 ,所以我们需要注意一点就是 最好要将我们需要进行分页查询的操作紧跟在PageHelper后面
,否则你不知道他到底能不能正常的进行分页操作.但是你跟在PageHelper后面肯定是可以实现分页操作的.