yml 配置:
spring:
profiles:
include: DBhnkyyy,DBLogApi
resource 下新增两个文件:
application-DBhnkyyy.yml(这个和上面的 DBhnkyyy 对应)
spring:
datasource:
hnkyyy:
name: hnkyyy
jdbc-url: jdbc:oracle:thin:@localhost:hnkyyy
username: username
password: password
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: oracle.jdbc.driver.OracleDriver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
这里 hnkyyy 一定不能大写!反正我一大写就报错
application-DBLogApi.yml(这个和上面的 DBLogApi 对应)
spring:
datasource:
logapi:
name: logapi
jdbc-url: jdbc:mysql://localhost:3306/logapi?&characterEncoding=utf-8&serverTimezone=UTC
username: username
password: password
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
java 文件:
DataSourceConfig :
@Configuration
public class DataSourceConfig {
// Orcale 数据库
// 这里的 bean 名字要和上面自定义的名字一样
@Bean(name = "hnkyyy")
@ConfigurationProperties(prefix = "spring.datasource.hnkyyy") // application.properteis中对应属性的前缀
public DataSource dataSourceCrm() {
return DataSourceBuilder.create().build();
}
// mysql 数据库
@Bean(name = "logapi")
@ConfigurationProperties(prefix = "spring.datasource.logapi") // application.properteis中对应属性的前缀
public DataSource dataSourcelogapi() {
return DataSourceBuilder.create().build();
}
}
每添加一个数据库,就在这里给一个 DataSource
MybatisDbhnkyyConfig:
@Configuration
@MapperScan(basePackages = {"com.ky.mapper.hnkyyy"}, sqlSessionFactoryRef = "sqlSessionFactoryCrm")
public class MybatisDbhnkyyConfig {
@Autowired
@Qualifier("hnkyyy")
private DataSource hnkyyy;
@Bean
public SqlSessionFactory sqlSessionFactoryCrm() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(hnkyyy); // 连接 hnkyyy 库
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplateCrm() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryCrm()); // 使用上面配置的Factory
return template;
}
}
MybatisDbLogApiConfig:
@Configuration
@MapperScan(basePackages = {"com.ky.mapper.logapi"}, sqlSessionFactoryRef = "sqlSessionFactorylogapi")
public class MybatisDbLogApiConfig {
@Autowired
@Qualifier("logapi")
private DataSource logapi;
@Bean
public SqlSessionFactory sqlSessionFactorylogapi() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(logapi); // 连接 logapi 库
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplatelogapi() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactorylogapi()); // 使用上面配置的Factory
return template;
}
}