Skip to content

Pagination Plugin

The PaginationInnerInterceptor plugin in MyBatis-Plus provides powerful pagination capabilities, supporting multiple databases and making paginated queries simple and efficient.

Supported Databases

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

If your required database is not listed, you can request its addition via a Pull Request.

Configuration

In a Spring Boot project, you can add the pagination plugin through 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, ensure pagination is added last
// For multi-data sources, you can omit the specific DbType. Otherwise, it's recommended to specify it.
return interceptor;
}
}

Properties

PaginationInnerInterceptor offers the following properties to customize pagination behavior:

PropertyTypeDefaultDescription
overflowbooleanfalseWhether to handle overflow beyond total pages
maxLimitLongMaximum number of items per page
dbTypeDbTypeDatabase type
dialectIDialectDialect implementation class

It is recommended to set dbType for single-database scenarios.

Using Pagination in Custom Mapper Methods

You can use pagination in Mapper methods as follows:

IPage<UserVo> selectPageVo(IPage<?> page, Integer state);
// Or use a custom pagination 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 cannot be null. To temporarily disable pagination, set the size parameter to a value less than 0 when initializing IPage. If the return type is List, the input IPage can be null, but you need to manually set IPage.setRecords(returned List). If XML needs to retrieve values from page, use page.property syntax.

Additional Notes

  • When generating countSql, tables in left join that do not participate in where conditions will be optimized out. It is recommended to always use aliases for tables and fields in SQL with left join.
  • When using multiple plugins, place the pagination plugin last in the execution chain to avoid inaccurate COUNT SQL execution.

Page Class

The Page class extends IPage and implements a simple pagination model. If you need a custom pagination model, you can extend Page or implement IPage.

PropertyTypeDefaultDescription
recordsList<T>emptyListQuery result list
totalLong0Total number of records
sizeLong10Items per page (default: 10)
currentLong1Current page
ordersList<OrderItem>emptyListSorting field information
optimizeCountSqlbooleantrueAutomatically optimize COUNT SQL
optimizeJoinOfCountSqlbooleantrueWhether to remove join queries in COUNT SQL optimization
searchCountbooleantrueWhether to perform count query
maxLimitLongMaximum number of items per page
countIdStringCustom count query statementId in XML

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

Baomidou

© 2016-2025 Baomidou™. All Rights Reserved.

Power by Astro Starlight | Sponsored by JetBrains

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