SoFunction
Updated on 2025-03-04

Springboot integrates mybatis-plus with pageHelper for pagination (steps)

PageHelper usage steps are analyzed.

When developing web applications, pagination display of database data is often involved. PageHelper is a very practical MyBatis paging plug-in. It can easily implement the paging function of database query results, greatly improving development efficiency. The following will briefly introduce the steps to use PageHelper.

1. Introduce dependencies

Maven Project

If your project is built on Maven, it needs to beAdd PageHelper dependencies to the file.

<!--pagehelperPagination plugin-->
<dependency>
  <groupId></groupId>
     <artifactId>pagehelper-spring-boot-starter</artifactId>
     <version>1.4.0</version>
</dependency>

Gradle Project 

For Gradle projects,Add the following dependencies to the file:

implementation ':pagehelper-spring-boot-starter:1.4.0'

2. Configure PageHelper

  • If it is a Spring Boot project,orConfigure in the file. byAs an example:
pagehelper:
  helper-dialect: mysql # Specify the database dialect, here I take MySQL as an example  reasonable: true # Paging rationalization, if pageNum<1, it will query the first page, if pageNum>pages, it will query the last page  support-methods-arguments: true # Support passing paging parameters through Mapper interface parameters  params: count=countSql # Used to take values ​​from objects based on attribute names,Configure herecountofSQL

The detailed explanation of these parameters is as follows:

  • helper-dialect: Specify the database dialect of the paging plugin. PageHelper will automatically detect the current database link and automatically select the appropriate paging method. If you are using MySQL, you can specify it asmysql
  • reasonable: Whether to enable paging rationalization. If enabled,pageNum<1When the data on the first page will be automatically queried, whenpageNum>pagesWhen , the last page of data is automatically queried; if not enabled, the above two cases will return empty data.
  • support-methods-arguments: Whether it supports passing paging parameters through Mapper interface parameters, default valuefalse. Set astrueWhen pageHelper will automatically select the value from the parameter values ​​of the query method according to the configured field and perform pagination.
  • params: Used to select values ​​from an object according to the attribute name, which can be configuredpageNum, pageSize, count, pageSizeZero, reasonableetc. Herecount=countSqlIt means that when executing paging queries, it will be usedcountSqlSQL as the calculation of the total number.

3. Use PageHelper in the code

1. Simple pagination query

Suppose we have a user table (user), you need to query and display user information on pages. First, define the query method in the corresponding Mapper interface:

import ;
import ;
public interface UserMapper {
    @Select("SELECT * FROM user")
    List<User> selectAllUsers();
}

Then, use PageHelper to perform pagination query at the Service layer or where the Mapper is called. The sample code is as follows:

import ;
import ;
import ;
import ;
import ;
import ;
import ;
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public PageInfo&lt;User&gt; getUserList(int pageNum, int pageSize) {
        // Set the paging parameters, pageNum is the current page number, and pageSize is the number of records displayed per page        (pageNum, pageSize);
        // Execute query        List&lt;User&gt; userList = ();
        // Use PageInfo to wrap the query results and return the object containing the paging information        return new PageInfo&lt;&gt;(userList);
    }
}

In the above code,(pageNum, pageSize)The method is used to set the paging parameters and tell PageHelper to query which page and how many records to display on each page. Then execute the query operation, PageHelper will automatically paginate the query statement, and finally wrap the query results intoPageInfoObject returns.PageInfoThe object contains various information related to paging, such as total number of records, total number of pages, current page number, number of records per page, etc., which facilitates paging display and related logical processing on the front-end.

2. Pagination query with conditions

If the query needs to be conditional, for example, fuzzy query and pagination based on the user's name. First, define a query method with conditions in the Mapper interface:

import ;
import ;
import ;
import ;
import ;
@Mapper
public interface UserMapper {
    @Select("SELECT * FROM user WHERE user_name LIKE #{name}")
    List<User> selectUsersByName(@Param("name") String name);
}

Implement paging query with conditions in the Service layer, the code is as follows:

import ;
import ;
import ;
import ;
import ;
import ;
import ;
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public PageInfo&lt;User&gt; getUserListByName(String name, int pageNum, int pageSize) {
        // Set paging parameters        (pageNum, pageSize);
        // Execute a conditional query        List&lt;User&gt; userList = ("%" + name + "%");
        // Return to the paging information object        return new PageInfo&lt;&gt;(userList);
    }
}

In this way, we can simply use the pageHelper paging function. For detailed use, please refer to the document.How to use the paging plugin

This is the article about Springboot integrating mybatis-plus for pagination using pageHelper. For more related content related to Springboot integrating mybatis-plus, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!