分页不生效的原因都是拦截器的问题
添加拦截器有两种情况
在配置类上加拦截器
@Configuration
public class MyBatisPlusConfig {
/**
* 新增分页拦截器,并设置数据库类型为mysql
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 向Mybatis过滤器链中添加分页拦截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
2.当你设置了数据源需要在数据源上添加拦截器
@Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
//修改为MybatisSqlSessionFactoryBean
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
//插件类
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
sqlSessionFactoryBean.setDataSource(dataSourceProxy);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
sqlSessionFactoryBean.setGlobalConfig(globalConfig);
//添加插件
sqlSessionFactoryBean.setPlugins(mybatisPlusInterceptor);
return sqlSessionFactoryBean.getObject();
}
文章来源地址https://www.toymoban.com/news/detail-617741.html
文章来源:https://www.toymoban.com/news/detail-617741.html
到了这里,关于MyBatis-Plus 分页设置不生效的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!