时间:2021-07-01 10:21:17 帮助过:2人阅读
@Configuration
@MapperScan(basePackages = AdsDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "adsSqlSessionFactory")
public class AdsDataSourceConfig {
static final String PACKAGE = "com.jyall.ehr.mapper"; //扫描的mapper包结构
@Value("${spring.datasource.url}") //配置文件中的配置
private String dbUrl;
@Value("${spring.datasource.username}") //配置文件中的配置
private String dbUser;
@Value("${spring.datasource.password}") //配置文件中的配置
private String dbPassword;
@Bean(name = "adsDataSource")
@Primary //此注解表示在默认的数据源配置,即在默认配置时用到的数据源配置
public DataSource adsDataSource() {
DataSource dataSource = new DataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl(dbUrl);
dataSource.setUsername(dbUser);
dataSource.setPassword(dbPassword);
return dataSource;
}
@Bean(name = "adsTransactionManager")
@Primary
public DataSourceTransactionManager adsTransactionManager(@Qualifier("adsDataSource") DataSource adsDataSource) {
return new DataSourceTransactionManager(adsDataSource);
}
@Bean(name = "adsSqlSessionFactory")
@Primary
public SqlSessionFactory adsSqlSessionFactory(@Qualifier("adsDataSource") DataSource adsDataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(adsDataSource);
return sessionFactory.getObject();
}
}
至此多数据源的配置都已经完成,其他配置跟但数据源的配置时一样的,不在赘述。。。
spring boot 多数据源配置(多种数据库)
标签: