前言:在我的项目中,明明在application.yml中将这个属性设为了
mybatis:
configuration:
map-underscore-to-camel-case: true,结果却没有导致后边无法找到匹配的属性。
原因
经过一番DEBUG分析,发现项目为了做多数据源,将生成
org.apache.ibatis.session.SqlSessionFactory
的配置抽出到使用@Configuration修饰的配置类中。
而application.yml配置文件中关于驼峰转换的配置内容没有放进去。
解决方法一:
第一个数据源:
@Bean
@ConfigurationProperties(prefix = "mybatis.configuration")
public org.apache.ibatis.session.Configuration globalConfiguration1() {
return new org.apache.ibatis.session.Configuration();
}
@Bean(name = "master1SqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("master1DataSource")
DataSource masterDataSource, org.apache.ibatis.session.Configuration globalConfiguration1)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setConfiguration(globalConfiguration1);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(Master1DataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
第二个数据源也设置的话,千万要记得新生成新数据数据源的Configuration
第二个数据源:
@Bean
@ConfigurationProperties(prefix = "mybatis.configuration")
public org.apache.ibatis.session.Configuration globalConfiguration2() {
return new org.apache.ibatis.session.Configuration();
}
@Bean(name = "master2SqlSessionFactory")
public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("master2DataSource") DataSource
master2DataSource, org.apache.ibatis.session.Configuration globalConfiguration2) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(master2DataSource);
sessionFactory.setConfiguration(globalConfiguration2);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(Master2DataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
可以看到数据源一,生产的Bean是globalConfiguration1赋予给masterSqlSessionFactory里的形参org.apache.ibatis.session.Configuration globalConfiguration1,形参对象要名字要和globalConfiguration1一样,要不就得加@Qualifier(“globalConfiguration1”) org.apache.ibatis.session.Configuration configuration 以确保注入唯一Bean。因为该类有两个Bean(一个是globalConfiguration1,另一个是globalConfiguration2)。
注意:千万不要把master1数据源的Configuration bean注入到另一个master2数据源SqlSessionFactory里,不然会发生执行的sql语句是注入的另一个数据库的表
解决方法二:
如果不引用在yml配置mybatis:configuration:内容,自己定义也是可以的。
@Bean(name = "master2SqlSessionFactory")
public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("master2DataSource") DataSource
master2DataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(master2DataSource);
org.apache.ibatis.session.Configuration configuration =
new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
sessionFactory.setConfiguration(configuration);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(Master2DataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
通过新定义创建一个Configuration并设置驼峰true然后设置到sessionFactory也是可以的。
1.
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
2.
configuration.setMapUnderscoreToCamelCase(true);
3.
sessionFactory.setConfiguration(configuration);