原因:配置多数据源时,application.yml的有关mybatis的配置是失效的,因为他不知道配置哪一个数据源
解决:
第一种:
- application.yml 的配置驼峰保留
- 定义全局的配置(代码片段一:)
- 在sqlSessionFactory中设置配置(代码片段二:)
代码片段一:
@Bean
@ConfigurationProperties(prefix = "mybatis.configuration")
public org.apache.ibatis.session.Configuration globalConfiguration() {
return new org.apache.ibatis.session.Configuration();
}
代码片段二:
@Bean(name = "firstSqlSessionFactory")
@Primary
public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDataSource") DataSource dataSource, org.apache.ibatis.session.Configuration configuration) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setConfiguration(configuration);
// bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return bean.getObject();
}
第二种:在sqlSessionFactory返回的bean中设置驼峰(代码片段三)
代码片段三:
@Bean(name = "firstSqlSessionFactory")
@Primary
public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return bean.getObject();
}