SoFunction
Updated on 2025-04-13

MyBatis-Plus paging plug-in configuration two ways

Pagination queries are a very common requirement when using MyBatis-Plus for database operations. MyBatis-Plus provides convenient paging plugins to simplify this process. This article will introduce in detail how to correctly configure MyBatis-Plus paging plugin (includingMybatisPlusInterceptorandPaginationInterceptor)。

1. File configuration

In order to use MyBatis-Plus paging plugin, you need to make sure that you are in your Maven projectThe necessary dependencies are correctly added to the file: (Usually, just add the starting dependency of mybatis-plus-boot-starter for mybatis-plus)

Notice: Replace ${} with the version number of your project's mybatis-plus, or manage version information uniformly in the <properties> tag

<!-- MyBatis-Plus Boot Starter -->
    <dependency>
        <groupId></groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>${}</version>
    </dependency>
<!-- MyBatis-Plus Extension for PaginationInterceptor or MybatisPlusInterceptor -->                                                                            
    <dependency>
        <groupId></groupId>
        <artifactId>mybatis-plus-extension</artifactId>
        <version>${}</version>
    </dependency>

2. Use PaginationInterceptor

  • Applicable to MyBatis-Plus versions prior to 3.4.0

If you are using MyBatis-Plus before 3.4.0, then you need to use PaginationInterceptor, which is an interceptor specially used to handle paging logic. In earlier versions, it provided paging functionality as a separate plug-in. Under the Springboot framework, it should be configured in the following way:

import ;
import ;
import ;

/**
  * mybatis-plus configuration
  */@ConfigurationpublicclassMybatisPlusConfig {

    /**
      * Add a paging plugin
      */@Beanpublic PaginationInterceptor paginationInterceptor() {
        returnnewPaginationInterceptor();
    }

}

3. Use MybatisPlusInterceptor

  • Applicable to versions after MyBatis-Plus 3.4.0

Starting from the MyBatis-Plus version 3.4.0, it is officially recommended to useMybatisPlusInterceptorTo replace the old versionPaginationInterceptorMybatisPlusInterceptor is an interceptor that integrates multiple functional functions.It supports a variety of plug-ins, such as paging plug-ins, performance analysis plug-ins, etc., and the interceptor has various functions by calling the addInnerInterceptor() method. The following is the official documentation for pagination plug-in configuration under the SpringBoot framework

import ;
import ;
import ;
import ;
import ;

/**
  * mybatis-plus configuration
  */@ConfigurationpublicclassMybatisPlusConfig {

    /**
      * Add a paging plugin
      */@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptorinterceptor=newMybatisPlusInterceptor();
        // Add paging plug-ins. If you configure multiple plug-ins, remember to add paging at the end        (newPaginationInnerInterceptor());
        // If there are multiple data sources, you can not match specific types, otherwise it is recommended to match specific DbTypereturn interceptor;    }

4. Comparison of two interceptors

1. PaginationInterceptor:

  • Mainly focused on the paging function, suitable for application scenarios where only simple paging logic is required

2. MybatisPlusInterceptor:

  • Paging plug-in: implementing paging logic
  • Performance analysis plug-in: Helps monitor SQL execution time.
  • SQL Injection Protection Plug-in: Improve application security.
  • Multi-tenant plug-in: supports data isolation under a multi-tenant architecture.
  • Optimistic lock plug-in: simplifies concurrent control.

V. Summary and Suggestions

If you are developing or maintaining a project based on MyBatis-Plus 3.4.0 before and your needs are simply pagination functions, thenPaginationInterceptorIt's enough.

For MyBatis-Plus 3.4.0 and later versions, the official recommendation isMybatisPlusInterceptor, because it provides more flexibility and more built-in functions, allowing you to easily expand the functions of your application according to actual needs.

This is the end of this article about the implementation of the MyBatis-Plus paging plug-in configuration. For more related MyBatis-Plus paging content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!