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 Name | Type | Default Value | Description |
---|---|---|---|
overflow | boolean | false | Whether to handle overflow beyond the total page count |
maxLimit | Long | Limit for the number of records per page | |
dbType | DbType | Database type | |
dialect | IDialect | Dialect 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 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
parameter cannot benull
. To temporarily disable pagination, initialize theIPage
with asize
parameter less than 0. If the return type isList
, the inputIPage
parameter can benull
, but you need to manually setIPage.setRecords(returned List)
. If the XML needs to retrieve values from thepage
object, usepage.property
syntax.
Other Considerations
- When generating the
countSql
, if a table in aleft join
does not participate in thewhere
condition, it will be optimized (removed). It is recommended to always use aliases for tables and fields in any SQL involvingleft 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 Name | Type | Default Value | Description |
---|---|---|---|
records | List<T> | emptyList | List of query data records |
total | Long | 0 | Total number of records for the query list |
size | Long | 10 | Number of records displayed per page, defaults to 10 |
current | Long | 1 | Current page number |
orders | List<OrderItem> | emptyList | Sorting field information |
optimizeCountSql | boolean | true | Automatically optimize COUNT SQL |
optimizeJoinOfCountSql | boolean | true | Whether to automatically remove the join part when optimizing COUNT SQL |
searchCount | boolean | true | Whether to perform a count query |
maxLimit | Long | Limit for the number of records per page | |
countId | String | XML 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.