springboot多数据源配置

转自:https://blog.csdn.net/qq_35206261/article/details/81778224?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

#mysql数据库
spring:
  datasource:
    repair:
      jdbc-url: jdbc:mysql://121.41.**.**:3306/******?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
      username: "******"
      password: "@******!"
      driver-class-name: com.mysql.cj.jdbc.Driver
    bi:
      jdbc-url: jdbc:mysql://47.97.**.**:3306/******?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
      username: "******"
      password: "******"
      driver-class-name: com.mysql.cj.jdbc.Driver
    csapp:
      jdbc-url: jdbc:mysql://13.56.**.**:3306/******?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
      username: "******"
      password: "******"
      driver-class-name: com.mysql.cj.jdbc.Driver
    ###数据源的其他配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

 

@Configuration
@MapperScan(basePackages = "com.cupshe.dc.service.mapper.bi", sqlSessionTemplateRef = "biSqlSessionTemplate")
public class DataSourceBiConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.bi")
    public DataSource biDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public SqlSessionFactory biSqlSessionFactory(@Qualifier("biDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/mapper/bi/*/*.xml"));
        return bean.getObject();
    }

    @Bean
    public DataSourceTransactionManager biTransactionManager(@Qualifier("biDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public SqlSessionTemplate biSqlSessionTemplate(@Qualifier("biSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值