springboot整合pagehelper插件失效

简介: springboot整合pagehelper插件失效

1.前言


因为我之前用的是SSM框架写的我们的项目,但是只有因为技术主管让我们做成微服务的架构,所以现在是又用springboot把之前的项目重新编写了一遍,之前自己在SSM框架里面编写分页查询是通过pagehelper这个插件实现的,但是之后将项目迁移到springboot上面的时候却出现所有的分页查询都不能使用了.


当时前端的同事告诉我这个消息的时候,我瞬间就觉得是不是他们的问题,


因为我这边的逻辑什么的都没有改,但是在swagger里面测试了之后,页面上没有报错,是能够正常运行的,但就是无法正常的分页.我就觉得可能还是我这边后台的原因,毕竟他们能够正常读取到数据,说明他们那边肯定是通了的,于是自己查了一下发现分页失效的确是我这边的问题,沃土了,又是我这边错了


20201024104124713.png


但是谁让我是打不死的小强呢,有bug就解决呗,实在解决不了就删库跑路呗.


哈哈哈,说着玩的,查完资料之后发现,主要就是下面这两个原因.希望能够对你们有所帮助.


2.注意点


2.1缺少依赖


这个是我遇到的问题.


在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>


2.2查询语句的顺序


pagehelper插件的具体使用我在我的这篇博客:前后端分离使用pagehelper里面详细说过,有兴趣的朋友可以去看看.

使用pagehelper插件的时候,逻辑一般是如下图所示:


2020102410590081.png

再通过代码来举个小栗子给大家看一下:

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.我们来看一下查询出来的数据


20201024113913646.png

显然数据是没有进行过分页操作的.

但是我自己之后又在另一个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,我们来看反馈的结果:


20201024142918906.png


所以说这种bug是 有时出现,有时候又不出现 ,所以我们需要注意一点就是 最好要将我们需要进行分页查询的操作紧跟在PageHelper后面 ,否则你不知道他到底能不能正常的进行分页操作.但是你跟在PageHelper后面肯定是可以实现分页操作的.


相关文章
|
5月前
SpringBoot+Mybatis-Plus+PageHelper分页+多条件查询
SpringBoot+Mybatis-Plus+PageHelper分页+多条件查询
146 0
|
5月前
|
SQL Java 数据库连接
【mybatis】第一篇,Springboot中使用插件PageHelper不生效解决方案
【mybatis】第一篇,Springboot中使用插件PageHelper不生效解决方案
|
10天前
|
SQL JSON Java
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和PageHelper进行分页操作,并且集成Swagger2来生成API文档,同时定义了统一的数据返回格式和请求模块。
22 1
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
|
9天前
|
监控 Java Maven
springboot学习二:springboot 初创建 web 项目、修改banner、热部署插件、切换运行环境、springboot参数配置,打包项目并测试成功
这篇文章介绍了如何快速创建Spring Boot项目,包括项目的初始化、结构、打包部署、修改启动Banner、热部署、环境切换和参数配置等基础操作。
44 0
|
5月前
|
Java Maven
SpringBoot项目的用maven插件打包报Test错误
SpringBoot项目的用maven插件打包报Test错误
104 1
|
1月前
|
SQL XML Java
springboot整合mybatis-plus及mybatis-plus分页插件的使用
这篇文章介绍了如何在Spring Boot项目中整合MyBatis-Plus及其分页插件,包括依赖引入、配置文件编写、SQL表创建、Mapper层、Service层、Controller层的创建,以及分页插件的使用和数据展示HTML页面的编写。
springboot整合mybatis-plus及mybatis-plus分页插件的使用
|
2月前
|
JavaScript 前端开发 Java
SpringBoot 引入 smart-doc 接口文档管理插件,以及统一接口返回,最后推送到 Torna,进行统一管理
本文介绍了如何在SpringBoot项目中整合smart-doc接口文档管理插件,实现接口文档的生成和统一管理,并展示了如何将文档推送到Torna接口文档管理系统进行进一步的集中管理。
174 0
SpringBoot 引入 smart-doc 接口文档管理插件,以及统一接口返回,最后推送到 Torna,进行统一管理
|
3月前
|
Java 测试技术 开发工具
Spring Boot中的开发工具与插件推荐
Spring Boot中的开发工具与插件推荐
|
4月前
|
Java 测试技术 开发工具
Spring Boot中的开发工具与插件推荐
Spring Boot中的开发工具与插件推荐
|
4月前
springboot2.4.5使用pagehelper分页插件
springboot2.4.5使用pagehelper分页插件
134 0