1、多数据源配置文件
2、配置项
多个数据源配置项中分别添加配置
bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true); //配置驼峰命名
或者
参考地址
@Configuration
@MapperScan(basePackages = "it.aspirin.riderserver.mapper.mysql", sqlSessionTemplateRef = "mySqlSessionTemplate")
public class MySQLDataSourceConfig {
/**
* 配置多数据源导致下划线命名映射为驼峰失效,需要在这里加一个配置,并在下面SqlSessionFactory方法中引入
*
* @return 配置
*/
@Bean
@ConfigurationProperties(prefix = "mybatis.configuration")
public org.apache.ibatis.session.Configuration globalConfiguration() {
return new org.apache.ibatis.session.Configuration();
}
/**
* 自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常
*/
@Bean(name = "mysqlDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.mysql")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "mySqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//这里将下划线映射为驼峰的配置引入
bean.setConfiguration(globalConfiguration());
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/mysql/*.xml"));
return bean.getObject();
}
/**
* 创建事务
*
* @param dataSource
* @return
*/
@Bean(name = "mysqlTransactionManager")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("mysqlDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
/**
* 用于注入dao
*
* @param sqlSessionFactory
* @return
* @throws Exception
*/
@Bean(name = "mySqlSessionTemplate")
@Primary
public SqlSessionTemplate SqlSessionTemplate(@Qualifier("mySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "mysqlDataSource")
public JdbcTemplate mysqlJdbcTemplate(@Qualifier(value = "mysqlDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}