Skip to content

Pagination Plugin

The MyBatis-Plus pagination plugin PaginationInnerInterceptor provides powerful pagination capabilities, supports multiple databases, and makes paginated queries simple and efficient.

Supported Databases

PaginationInnerInterceptor supports a wide range of databases, including but not limited to:

If the database you need to use is not on the list, you can request its addition via a Pull Request.

Configuration

In a Spring Boot project, you can add the pagination plugin via Java configuration:

@Configuration
@MapperScan("scan.your.mapper.package")
public class MybatisPlusConfig {
/**
* Add pagination plugin
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // If configuring multiple plugins, remember to add pagination last
// If you have multiple data sources, you can omit the specific type; otherwise, it's recommended to specify the concrete DbType
return interceptor;
}
}

Properties

PaginationInnerInterceptor provides the following properties to customize pagination behavior:

Property NameTypeDefault ValueDescription
overflowbooleanfalseWhether to handle overflow beyond the total page count
maxLimitLongLimit for the number of records per page
dbTypeDbTypeDatabase type
dialectIDialectDialect implementation class

It is recommended to set dbType for single database type configurations.

Using Pagination in Custom Mapper Methods

You can use pagination in your Mapper methods in the following ways:

IPage<UserVo> selectPageVo(IPage<?> page, Integer state);
// Or use a custom page class
MyPage selectPageVo(MyPage page);
// Or return List
List<UserVo> selectPageVo(IPage<UserVo> page, Integer state);

Corresponding XML configuration:

<select id="selectPageVo" resultType="xxx.xxx.xxx.UserVo">
SELECT id,name FROM user WHERE state=#{state}
</select>

If the return type is IPage, the input IPage parameter cannot be null. To temporarily disable pagination, initialize the IPage with a size parameter less than 0. If the return type is List, the input IPage parameter can be null, but you need to manually set IPage.setRecords(returned List). If the XML needs to retrieve values from the page object, use page.property syntax.

Other Considerations

  • When generating the countSql, if a table in a left join does not participate in the where condition, it will be optimized (removed). It is recommended to always use aliases for tables and fields in any SQL involving left join.
  • When using multiple plugins, place the pagination plugin last in the plugin execution chain to avoid inaccurate COUNT SQL execution.

Page Class

The Page class extends the IPage class, implementing a simple pagination model. If you need to implement your own pagination model, you can extend the Page class or implement the IPage interface.

Property NameTypeDefault ValueDescription
recordsList<T>emptyListList of query data records
totalLong0Total number of records for the query list
sizeLong10Number of records displayed per page, defaults to 10
currentLong1Current page number
ordersList<OrderItem>emptyListSorting field information
optimizeCountSqlbooleantrueAutomatically optimize COUNT SQL
optimizeJoinOfCountSqlbooleantrueWhether to automatically remove the join part when optimizing COUNT SQL
searchCountbooleantrueWhether to perform a count query
maxLimitLongLimit for the number of records per page
countIdStringXML custom count query statementId

With these configurations and usage methods, you can easily implement paginated queries in MyBatis-Plus, improving your application’s performance and user experience.

Baomidou

© 2016-2025 Baomidou™. All Rights Reserved.

Power by Astro Starlight | Sponsored by JetBrains

渝ICP备2021000141号-1 | 渝公网安备50011302222097