最近做新项目,采用新框架,需要集成输出完整SQL插件到框架中
【单数据源】插件配置地址:https://blog.csdn.net/kkk123445/article/details/115715732?spm=1001.2014.3001.5502
在集成的过程中,出现了多数据源的情况下,拦截器失效的问题,死活不打印日志
原代码如下(省略了关键性代码):
@Configuration
@MapperScan(basePackages = "com.**.dao", sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisDataSourceAutoConfiguration {
@Primary
@Bean(name = "sqlSessionFactory1")
public SqlSessionFactory basicSqlSessionFactory(DruidDataSource basicDataSource) throws Exception {
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
factoryBean.setDataSource(basicDataSource);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapping/*.xml"));
return factoryBean.getObject();
}
}
原来执行完sessionFactoryBean.getObject() 后,插件虽然由spring管理,但不会生效
查了官方文档后,发现需要预先配置好加入的插件,代码如下:
@Primary
@Bean(name = "sqlSessionFactory1")
public SqlSessionFactory basicSqlSessionFactory(@Qualifier("dataSource1") DruidDataSource basicDataSource) throws Exception {
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
factoryBean.setDataSource(basicDataSource);
/* start */
//配置拦截器插件
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.addInterceptor(new SqlInterceptor());
factoryBean.setConfiguration(configuration);
/* end */
factoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:mapping/*.xml"));
return factoryBean.getObject();
}
至此,成功打印!