SoFunction
Updated on 2025-03-08

MyBatis-Plus Page problem of pagination not taking effect

1. Possible Causes

1) The version is too low

2) MybatisPlusInterceptor configuration needs to be added

2. Processing

1) Pull the package, use version 3.4.0

  <dependency>
            <groupId></groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>

2) Configuration, you can change DbType by yourself according to the database type used

@Configuration
public class MyBatisPlusConfig {

    /**
      * Plug-in configuration
      *
      * @return
      */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // Adding a paging interceptor to the filter chain of MyBatis-Plus requires setting the database type (mainly used in paging dialects)        // Use pg        //(new PaginationInnerInterceptor(DbType.POSTGRE_SQL));
        // MySQL use        (new PaginationInnerInterceptor());
        // Add an optimistic lock interceptor        (new OptimisticLockerInnerInterceptor());
        return interceptor;
    }
}

Supplement: Other reasons

1. The paging plugin is not configured:

Make sure you have added a paging plugin to the MyBatis-Plus configuration. If you are using Spring Boot, you usually add a bean to the MybatisPlusConfig class to add a paginated plugin.

@Bean  
public PaginationInterceptor paginationInterceptor() {  
    return new PaginationInterceptor();  
}

Method issues in SQL mapped files or Mapper interface:
If you write custom SQL in the XML mapping file, make sure you are not writing LIMIT and OFFSET manually. The paging plugin will automatically handle these for you.

Here we also need to pay special attention to the above configuration method in version 3.4. You can try the following configuration method

@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    (new PaginationInnerInterceptor());
    return interceptor;
}

2. The Page object is not set correctly when calling the method:

Before calling the paging method, you need to create a Page object and set the number of records displayed on the current page and each page.

Page&lt;User&gt; page = new Page&lt;&gt;(1, 10); // The first page, 10 records per pageIPage&lt;User&gt; userPage = (page, null); // Assume there is no query conditionList&lt;User&gt; userList = (); // Get pagination results

3. Version conflict or configuration issues:

Make sure that the version of MyBatis-Plus you are using does not conflict with other dependencies and that your project is configured correctly.

4. Check the database and tables:

Make sure your databases and tables support paging queries (for example, MySQL supports LIMIT and OFFSET).

View the log:
Turn on the logging function of MyBatis to see if the generated SQL statement contains LIMIT and OFFSET. This can help you confirm that the paging plugin has been called correctly.

Transaction management:
If you are using a transaction, make sure the paging query is executed correctly in the context of the transaction.

5. Other plug-ins or interceptor conflicts:

If you use other MyBatis plug-ins or interceptors in your project, make sure there is no conflict between them.

This is the article about solving the problem of MyBatis-Plus Page paging not taking effect. For more related contents of MyBatis-Plus Page paging not taking effect, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!