PaginationInnerInterceptor依赖
时间: 2025-01-24 14:35:14 浏览: 49
### 使用 `PaginationInnerInterceptor` 的配置
为了正确使用 `PaginationInnerInterceptor`,需确保拦截器按照特定顺序添加到项目中。如果应用了多租户功能,则应先添加 `TenantLineInnerInterceptor` 后再添加 `PaginationInnerInterceptor`[^1]。
```java
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 添加多租户拦截器 (如果有)
interceptor.addInnerInterceptor(new TenantLineInnerInterceptor());
// 添加分页插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
```
### 处理特殊情况下的分页需求
对于某些业务逻辑,可能需要在请求参数设置为不分页的情况下获取所有记录并计算总数。MyBatis Plus 默认行为是在 `size=0` 时不返回任何数据而仅提供总条数统计;而在 `size=-1` 时虽然能取得全部结果集但不包含总计数量[^3]。
针对上述情况的一种解决方案是通过自定义实现来覆盖默认的 `PaginationInnerInterceptor` 行为:
```java
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
// 自定义分页处理器类继承官方提供的分页处理程序
public class CustomizedPaginationInnerInterceptor extends PaginationInnerInterceptor {
@Override
protected void processPage(Page<?> page, MetaObject metaObject) {
super.processPage(page, metaObject);
// 当 size 设置为 0 时执行全量查询操作,并保留 total 记录
if (page.getSize() == 0 && !CollectionUtils.isEmpty(page.getRecords())) {
Long count = selectCount(metaObject.getValue("ew").toString());
((AbstractPage) page).setTotal(count);
}
}
private long selectCount(String ewStr){
// 实现具体的计数逻辑...
throw new UnsupportedOperationException("Not implemented yet.");
}
}
// 修改配置文件中的 Bean 定义以使用新的定制化版本
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 替换原有的 PaginationInnerInterceptor 为自定义版
interceptor.addInnerInterceptor(new CustomizedPaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
```
此方案允许应用程序根据实际应用场景灵活调整分页机制的行为模式,从而满足更广泛的数据访问需求。
阅读全文
相关推荐


















