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:
Property | Type | Default | Description |
---|---|---|---|
overflow | boolean | false | Whether to handle overflow beyond total pages |
maxLimit | Long | Maximum number of items per page | |
dbType | DbType | Database type | |
dialect | IDialect | Dialect 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 classMyPage selectPageVo(MyPage page);// Or return ListList<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 inputIPage
cannot benull
. To temporarily disable pagination, set thesize
parameter to a value less than 0 when initializingIPage
. If the return type isList
, the inputIPage
can benull
, but you need to manually setIPage.setRecords(returned List)
. If XML needs to retrieve values frompage
, usepage.property
syntax.
Additional Notes
- When generating
countSql
, tables inleft join
that do not participate inwhere
conditions will be optimized out. It is recommended to always use aliases for tables and fields in SQL withleft 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
.
Property | Type | Default | Description |
---|---|---|---|
records | List<T> | emptyList | Query result list |
total | Long | 0 | Total number of records |
size | Long | 10 | Items per page (default: 10) |
current | Long | 1 | Current page |
orders | List<OrderItem> | emptyList | Sorting field information |
optimizeCountSql | boolean | true | Automatically optimize COUNT SQL |
optimizeJoinOfCountSql | boolean | true | Whether to remove join queries in COUNT SQL optimization |
searchCount | boolean | true | Whether to perform count query |
maxLimit | Long | Maximum number of items per page | |
countId | String | Custom 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.