SoFunction
Updated on 2025-03-08

Detailed explanation of Mybatis' pagination plugin

1. Overview

Mybatis is a very popular persistence layer framework that helps us easily implement database operations and data access. In Mybatis, how to paginate data is a very common problem. Now, we can paginate data by using Mybatis' paging plugin.

2. Installation and configuration

To use Mybatis' paging plugin, we need to install and configure it first. The installation process is very simple, just run the following command in the project:

npm install mybatis-paginate -g 

After the installation is complete, we need to specify the configuration of the paging plugin in the configuration file. We can add the following code to our file:

<configuration>  
    <typeAliases>  
        <package name=""/>  
    </typeAliases>  
    <environments default="development">  
        <environment >  
            <transactionManager type="JDBC"/>  
            <dataSource type="POOLED">  
                <property name="driver" value="${}"/>  
                <property name="url" value="${}"/>  
                <property name="username" value="${}"/>  
                <property name="password" value="${}"/>  
            </dataSource>  
        </environment>  
    </environments>  
    <mappers>  
        <mapper class=""/>  
    </mappers>  
    <plugins>  
        <plugin artifactId="mybatis-paginate">  
            <version>3.0.1</version>  
            <configuration>  
                <pagesize>10</pagesize>  
                <total-results-size>20</total-results-size>  
                <max-results-size>50</max-results-size>  
                <results-per-page-config>  
                    <config>  
                        <index>0</index>  
                        <label>pageNumber</label>  
                        <value>pageNum</value>  
                    </config>  
                    <config>  
                        <index>1</index>  
                        <label>pageSize</label>  
                        <value>pageSize</value>  
                    </config>  
                </results-per-page-config>  
            </configuration>  
        </plugin>  
    </plugins>  
</configuration>  

In the above code, we specify some configurations of the paging plugin, including the paging size, total result size, maximum result size, and the amount of data displayed per page.

3. Use the paging plugin

Now that we have installed and configured the Mybatis paging plugin, we can use it to paginate our data. Here is an example of using a paging plugin:

@Mapper  
public interface MyClassMapper {  
    @Select("SELECT * FROM my_table WHERE condition")  
    List<MyClass> findMyClasses();  
}

In the above code, we use the @Select annotation to specify the query statement, and the @Page annotation to specify the configuration of the pagination plugin.

@Mapper  
public interface MyClassMapper {  
    @Pageable(pagesize = 10, direction = . DESC, filter = "condition")  
    List<MyClass> findMyClasses(@Param("condition") String condition);  
}

In the above code, we use the @Pageable annotation to specify the configuration of the paging plugin, and use the @Param annotation to specify the query conditions.

Now, we can get the paged data by calling the findMyClasses method.

Commonly used data:

  1. pageNum: The page number of the current page
  2. pageSize: Number of pieces displayed per page
  3. size: the number of real numbers displayed on the current page
  4. Total: Total records
  5. pages: Total page count
  6. prePage: page number of the previous page
  7. nextPage: The page number of the next page
  8. isFirstPage/isLastPage: Is it the first page/last page
  9. hasPreviousPage/hasNextPage: Does the previous page/next page exist?
  10. navigatePages: the number of pages for navigation pages
  11. navigatepageNums: Navigate page number, [1,2,3,4,5]

4. Summary

Through this article, we can learn how to use Mybatis' paging plugin to implement pagination of data. Installing and configuring the paging plugin is very simple, you only need to make some simple configuration in the configuration file. Using the paging plug-in allows us to more conveniently implement data paging and improve the readability and maintenance of code.

This is the end of this article about explaining Mybatis' paging plug-in in detail. For more related contents of Mybatis paging plug-in, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!