配置application.yml
spring:
datasource:
# 第一个数据源
first:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.41.128:3306/first?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: 123456
# 第二个数据源
second:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://172.16.0.9:3306/second?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: 123456
获取DB配置
@Data
@Component
@ConfigurationProperties(prefix = "spring.datasource.first")
public class FirstDataBaseProperties {
private String url;
private String username;
private String password;
private String driverClassName;
}
@Data
@Component
@ConfigurationProperties(prefix = "spring.datasource.second")
public class SecondDataBaseProperties {
private String url;
private String username;
private String password;
private String driverClassName;
}
配置数据源
@Configuration
// 第一个数据源与Mapper接口绑定
@MapperScan(basePackages = "com.deri.task.dao.first",sqlSessionTemplateRef ="firstSqlSessionTemplate")
public class FirstDataSourceConfig {
@Autowired
FirstDataBaseProperties firstDataBaseProperties;
@Bean(name = "firstDS")
// 多数据源情况下需要指定主数据源
@Primary
public DataSource getFirstDataSource() {
DataSource build = DataSourceBuilder.create()
.driverClassName(firstDataBaseProperties.getDriverClassName())
.url(firstDataBaseProperties.getUrl())
.username(firstDataBaseProperties.getUsername())
.password(firstDataBaseProperties.getPassword())
.build();
return build;
}
@Bean(name = "firstSqlSessionFactory")
@Primary
public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDS") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean("firstTransactionManger")
@Primary
public DataSourceTransactionManager firstTransactionManger(@Qualifier("firstDS") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
// 创建SqlSessionTemplate
@Bean(name = "firstSqlSessionTemplate")
@Primary
public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("firstSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
// 第二个数据源与Mapper接口绑定
@MapperScan(basePackages = "com.deri.task.dao.second",sqlSessionTemplateRef ="secondSqlSessionTemplate")
public class FirstDataSourceConfig {
@Autowired
SecondDataBaseProperties secondDataBaseProperties;
@Bean(name = "secondDS")
public DataSource getFirstDataSource() {
DataSource build = DataSourceBuilder.create()
.driverClassName(secondDataBaseProperties.getDriverClassName())
.url(secondDataBaseProperties.getUrl())
.username(secondDataBaseProperties.getUsername())
.password(secondDataBaseProperties.getPassword())
.build();
return build;
}
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory firstSqlSessionFactory(@Qualifier("secondDS") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean("secondTransactionManger")
public DataSourceTransactionManager firstTransactionManger(@Qualifier("secondDS") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
// 创建SqlSessionTemplate
@Bean(name = "secondSqlSessionTemplate")
public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
参考链接
已知问题
- mybatis驼峰命名转换失效问题
# 默认在application.yml中配置,但是在多数据源场景下该配置失效,需要在代码中显式指定.
mybatis:
configuration:
map-underscore-to-camel-case: true
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory firstSqlSessionFactory(@Qualifier("secondDS") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// 指定驼峰转换
bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return bean.getObject();
}
另一种配置方法
不用创建
FirstDataBaseProperties
和SecondDataBaseProperties
.其余保持一样.
@Primary
@Bean(name = "publicDataSourceProperties")
@ConfigurationProperties(prefix = "spring.datasource.public")
public DataSourceProperties publicDataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "publicDS")
@Primary
public DataSource getFirstDataSource(@Qualifier("publicDataSourceProperties") DataSourceProperties dataSourceProperties) {
return dataSourceProperties.initializeDataSourceBuilder().build();
}