一、MybatisPlusInterceptor
Starting from Mybatis Plus 3.4.0 version, the old version of PaginationInterceptor is no longer used, but MybatisPlusInterceptor is used.
MybatisPlusInterceptor is a series of interceptor chains that implement InnerInterceptor, which can also be understood as a collection. Can include some interceptors as follows
- Automatic pagination: PaginationInnerInterceptor (most commonly used)
- Multi-tenant: TenantLineInnerInterceptor
- Dynamic table name: DynamicTableNameInnerInterceptor
- Optimistic lock: OptimisticLockerInnerInterceptor
- sql performance specification: IllegalSQLInnerInterceptor
- Prevent full table update and deletion: BlockAttackInnerInterceptor
Second, the configuration method of the old version of the paging plug-in (Before Mybatis Plus 3.4.0 version)
@Configuration
@MapperScan(basePackages = {"com.zimug.**.mapper"})
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
// paginationInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
// paginationInterceptor.setLimit(500);
// 开启 count 的 join 优化,只针对部分 left join
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
}
3. New configuration method of paging plug-in (Mybatis Plus 3.4.0 version and later versions)
The new paging plug-in, one mitigation and two mitigation follow the rules of mybatis, you need to set MybatisConfiguration#useDeprecatedExecutor = false to avoid caching problems
@Configuration
@MapperScan(basePackages = {"com.zimug.**.mapper"})
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//向Mybatis过滤器链中添加分页拦截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//还可以添加i他的拦截器
return interceptor;
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}
Fourth, the use of paging query
The use of paging query has not changed, and it is still consistent with the previous version of Mybatis, and there is no change . Here is a simple example
Page<SysUserOrg> page = new Page<> (pageNum,pageSize); //查询第pageNum页,每页pageSize条数据
//将分页参数page作为Mybatis或Mybatis Plus的第一个参数传入持久层函数,即可完成分页查询
return mySystemMapper.selectUser(page, 其他参数 );
Welcome to follow my blog, there are many boutique collections
- This article is reproduced indicate the source (en must not turn only the text): letters Gebo off .
If you think it is helpful to you, please like and share it for me! Your support is my inexhaustible creative motivation! . In addition, the author has output the following high-quality content recently, and I look forward to your attention.