在application.yml中配置多个数据源链接
spring:
datasource:
sys:
username: informix
password: informix
url:
driver-class-name: com.informix.jdbc.IfxDriver
tomcat:
#连接池处于活动状态的数据库连接的最大数目,0表示不限制,表示最大并发
max-active: 10
#初始化连接数
initial-size: 1
validation-query: select count(*) from systables
#指定连接被调用时是否经过校验 配合validationQuery
test-on-borrow: true
#指定连接返回到池中是时是否经过校验 配合validationQuery
test-on-return: true
test-while-idle: true
#连接池处于空闲状态的数据库连接的最小数目 低于此数值将会创建所欠缺的连接,设0无限制
min-idle: 2
#连接池处于空闲状态的数据库连接的最大数目,取非正整数表示不受限制,超过此数值时多余的空闲连接将会被释放
max-idle: 8
jdbc-interceptors: ConnectionState;SlowQueryReport(threshold=200)
#是否清除已经超过 removeAbandonedTimeout 设置的无效连接,自动回收超时连接
remove-abandoned: true
#nc数据源 对应NcDataSourceConfig
imcs:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/sys
driver-class-name: com.mysql.jdbc.Driver
config中新建ImcsDataSourceConfig和SysDataSourceConfig两个类分别代理两个数据源。
代码如下:
@Configuration
@MapperScan(value = "com.boco.nscs.mapper.imcs",sqlSessionTemplateRef = "imcsSqlSessionTemplate")
public class ImcsDataSourceConfig {
@Bean(name = "imcsDataSource")
@ConfigurationProperties(prefix = "spring.datasource.imcs")
public DataSource myDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "imcsSqlSessionFactory")
public SqlSessionFactory mySqlSessionFactory(@Qualifier("imcsDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setTypeAliasesPackage("com.boco.nscs.entity");
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/sqlmaps/*.xml"));
//设置 jdbctype默认为空 oracle需要
bean.getObject().getConfiguration().setJdbcTypeForNull(JdbcType.VARCHAR);
return bean.getObject();
}
@Bean(name = "imcsTransactionManager")
public DataSourceTransactionManager myTransactionManager(@Qualifier("imcsDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "imcsSqlSessionTemplate")
public SqlSessionTemplate mySqlSessionTemplate(@Qualifier("imcsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@MapperScan(value = "com.boco.nscs.mapper.hwpre",sqlSessionTemplateRef = "sysSqlSessionTemplate")
public class SysDataSourceConfig {
@Bean(name = "sysDataSource")
@ConfigurationProperties(prefix = "spring.datasource.sys")
@Primary
public DataSource myDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "sysSqlSessionFactory")
@Primary
public SqlSessionFactory mySqlSessionFactory(@Qualifier("sysDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setTypeAliasesPackage("com.boco.nscs.entity");
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/sqlmaps/*.xml"));
//设置 jdbctype默认为空 oracle需要
bean.getObject().getConfiguration().setJdbcTypeForNull(JdbcType.VARCHAR);
return bean.getObject();
}
@Bean(name = "sysTransactionManager")
@Primary
public DataSourceTransactionManager myTransactionManager(@Qualifier("sysDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sysSqlSessionTemplate")
@Primary
public SqlSessionTemplate mySqlSessionTemplate(@Qualifier("sysSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Primary注解的作用是设置主数据库配置,@MapperScan 扫描mapper,绑定数据库,mapper.xml中数据源映射的mapper中的数据源,基本实现了多数据源配置,已亲测!!!