Solution that PageHelper does not work in MyBatis

  • 2021-09-20 20:22:47
  • OfStack

PageHelper in MyBatis does not work

Using pageHelper today, we found that PageHelper. startPage (page, pageSize) was set up. pageSize is set to 10, but the results are not paged, and all the data are investigated;

Problem solving:

The reason is the dependent version of mybatis, which was previously configured with version 1.0. 0, and this version does not support paging interception


<dependency>
 <groupId>org.mybatis.spring.boot</groupId>
 <artifactId>mybatis-spring-boot-starter</artifactId>
 <version>1.0.0</version>
</dependency>

So modify it to version 1.1. 1 to solve the problem


<!-- mybatis -->
<dependency>
 <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>1.1.1</version>
 </dependency>
 <!--  Paging plug-in  -->
<dependency>
 <groupId>com.github.pagehelper</groupId>
 <artifactId>pagehelper</artifactId>
 <version>4.1.6</version>
</dependency>

MyBatis PageHelper Setting the pages property manually does not work

Environment: springboot+mybatis with pagehelp plug-in

Dependencies are as follows:


        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis.spring.boot</groupId>
                    <artifactId>mybatis-spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Problem description:

Manually build the Page object, and the set pages attribute does not take effect. For example, the setting value is 3, and it is still 0 when getting it;

Reason:

The constructed Page object does not have the pageSize property set. When the setTotal () method of the page object is called, the value of the pages property is internally reset based on the pageSize property.

The source code of the setTotal method for the Page object is as follows:


public void setTotal(long total) {
        this.total = total;
        if (total == -1) {
            pages = 1;
            return;
        }
        //  If pageSize<=0,  Is set directly pages=0; pageSize Default to 0
        if (pageSize > 0) {
            pages = (int) (total / pageSize + ((total % pageSize == 0) ? 0 : 1));
        } else {
            pages = 0;
        }
        // Paging rationalization, automatic processing for unreasonable page numbers 
        if ((reasonable != null && reasonable) && pageNum > pages) {
            pageNum = pages;
            calculateStartAndEndRow();
        }
    }

The code for manually constructing an Page object is as follows:


//  Return value page Object 
Page<StudyRecordVo> pageResult = new Page<>();
//  Do not set pageSize,setPages Invalid method call , setTotal Method overrides the pageResult Adj. pages Attribute 
//  Solutions : 1.  Settings pageSize Property is invoked when the setTotal Method before ; 2. setTotal After the method is called, the pages Attribute 
pageResult.setPageSize(pageDto.getPageSize());
pageResult.setPages(totalPages);
pageResult.setTotal(totalElements);
pageResult.addAll(studyRecordVoList);

Related articles: