Springboot配置多数据源

在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中的数据源,基本实现了多数据源配置,已亲测!!! 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值