描述:springboot整合多数据源后无法打印控制台没打印sql
原因:因为(mybatis.configuration.log-impl)tk.mybatis 当SqlSessionFactory不存在定义的时候定义SqlSessionFactory;而我们配置多数据源时,重新定义了SqlSessionFactory;因此,如果要打印sql 语句的话,需在我们配置多数据源的SqlSessionFactory时添加MybatisProperties;
解决方案
1.确认配置文件已有以下配置
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
#日志输出
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
2.只需在某一个配置多数据源的配置上加入MybatisProperties即可
/**
* 配置mybatisPropertie 即可
*/
@Primary
@Bean("db1SqlSessionFactory")
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource
, MybatisProperties mybatisPropertie
) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/mapping/mysqlmapper/*.xml"));
bean.setConfiguration(mybatisPropertie.getConfiguration());
return bean.getObject();
}
参考: