在以下方法配置了:
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor innerInterceptor=new PaginationInnerInterceptor();
innerInterceptor.setDbType(DbType.MYSQL);
innerInterceptor.setOverflow(true);
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
interceptor.addInnerInterceptor(innerInterceptor);
return interceptor;
}
但是一直都不生效,人都枯了。
一直百度也没找出来,后来找到是因为配置jdbc的DataSourceConfig类,并且在这里我加载了MybatisSqlSessionFactoryBean,所以上面配置的interceptor并没有生效。
@Autowired
MybatisPlusInterceptor mybatisPlusInterceptor;
@Bean(name = "DBDataSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("DBDataSource") DataSource dataSource)
throws Exception {
MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
//加载分页拦截器
sessionFactory.setPlugins(mybatisPlusInterceptor);
return sessionFactory.getObject();
}
吐槽一下,为啥interceptor变成了setPlugins,就感觉有毒。一直用setIn、addIn尝试都没有智能提示。