Springboot integration pagehelper plugin fails

Develop a habit, like first and then watch!!!

1 Introduction

Because I used the SSM framework to write our project before, but only because the technical director asked us to make a microservice architecture, so now I use springboot to rewrite the previous project, and I wrote it in the SSM framework before. The paging query is implemented through the pagehelper plugin, but when the project is later migrated to springboot, all paging queries can no longer be used.

When the front-end colleagues told me the news, I instantly felt that it was their problem.
Insert picture description here

Because the logic on my side has not changed anything, but after testing in swagger, there is no error on the page, and it can run normally, but it is not normal pagination. I think it may be due to my background. After all, they can read the data normally, indicating that they must be connected, so I checked it and found that the paging failure is indeed a problem on my side, fertile soil, and I am wrong here
Insert picture description here
but who made me Xiaoqiang, who can’t be beaten to death, can be solved if there is a bug, if it can’t be solved, just delete the library and run away.
Insert picture description here
Hahaha, for fun, after checking the information, I found that the main reasons are the following two reasons. I hope I can do something for you. help.

2. Precautions

2.1 Lack of dependencies

This is the problem I encountered.
Using the pagehelper plugin in the SSM framework, you only need to import this dependency to normally realize the function of paging query

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.2</version>
</dependency>

But to use pagehelper in springboot, you need to import three dependencies.

 <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 The order of query statements

The specific use of the pagehelper plugin is in my blog: I 前后端分离使用pagehelpersaid in detail in it, friends who are interested can check it out.
When using the pagehelper plugin, the logic is generally as shown in the following figure:
Insert picture description here
Then use the code to give a little chestnut Let's take a look:

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);
    }

So that we can implement paging queries, and there should be noted that inquiries sentence order , PageHelper默认是将紧跟在他后面的查询语句做分页查询的so if you need to do before pagination query statement and add another one query, the query before it will automatically PageHelper Statements are used for paging operations.
The classic famous saying of the father of Linux-----talk is cheap show me the code
is to help everyone understand through the following little chestnuts.

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;
    }

Our return information is the data queried by the selectAllByPage method. This time we will perform the selectAllByPage method first, and then selectAll. Let’s take a look at the queried data
Insert picture description here

We are indeed the data queried by normal paging query

Next, let’s look at the code for exchanging two query statements

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;
    }

Our return information is still the data queried by the selectAllByPage method. This time we first perform the selectAll method, and then selectAllByPage. Let’s take a look at the queried data.
Insert picture description here
Obviously the data has not been paged.
But I later Another controller tested it, but the result was different from what I expected.
This is the code

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;
    }

The information returned is still the data returned by the selectAllByPage method, but we first perform the selectAll method, and then selectAllByPage, let's look at the feedback result:

Insert picture description here

So this bug is sometimes, sometimes not appear , so we need to note is that 最好要将我们需要进行分页查询的操作紧跟在PageHelper后面, or you do not know him in the end can not be normal paging operation. But you certainly can be achieved with paging operations behind PageHelper.

I have seen it here. If you think it is helpful to you, you can pay attention to my public account. The newcomer up needs your support!!!
Insert picture description here

Guess you like

Origin blog.csdn.net/lovely__RR/article/details/109263534