配置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
@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);
}
@Bean(name = "firstSqlSessionTemplate")
@Primary
public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("firstSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@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);
}
@Bean(name = "secondSqlSessionTemplate")
public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
參考連接
- https://www.cnblogs.com/zhoutao825638/p/10382261.html
已知問題
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();
}