springboot多数据源配置

application.yml

server:
  port: 8080
  servlet:
    context-path: /

spring:
  application:
    name: crm-full
  datasource:
      # 使用druid数据源
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.cj.jdbc.Driver
      druid:
        crm:
          url: jdbc:mysql://192.xxx.x.xx:3306/crm?useSSL=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
          username: admin
          password: xxxxxxxxx!
        xir:
          url: jdbc:mysql://192.xxx.x.xx:3306/xir?useSSL=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
          username: admin
          password: xxxxxxxxx!

        # 配置初始化大小(默认0)、最小、最大(默认8)
        initial-size: 1
        min-idle: 1
        max-active: 10
        # 配置获取连接等待超时的时间
        max-wait: 60000
        # 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大。 默认为false
        pool-prepared-statements: true
        # 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。
        max-open-prepared-statements: 20
        # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
        time-between-eviction-runs-millis: 60000
        # 配置一个连接在池中最小和最大生存的时间,单位是毫秒
        min-evictable-idle-time-millis: 300000
        max-evictable-idle-time-millis: 900000

        # 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。
        # 如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。
        validation-query: SELECT 1 FROM DUAL
        # 申请连接时执行validationQuery检测连接是否有效 默认为true
        test-on-borrow: false
        # 归还连接时执行validationQuery检测连接是否有效 默认为false
        test-on-return: false
        # 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
        test-while-idle: true
        # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
        filters: stat,wall,slf4j
        # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
        connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
        # 合并多个DruidDataSource的监控数据
        useGlobalDataSourceStat: true

两个数据源confg类

@Configuration
@MapperScan(basePackages = "com.xxxx.fullSync.dao.crm", sqlSessionFactoryRef = "crmSqlSessionFactory")
public class CrmDataSourceAutoConfiguration {
    //crm mappers路径
    private static final String MAPPERS_LOCAL = "classpath:mappers/crm/*.xml";

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.druid.crm")
    public DruidDataSource crmDataSource() {
        return new DruidDataSource();
    }

    @Bean
    @Primary
    public DataSourceTransactionManager crmTransactionManager(@Qualifier("crmDataSource") DruidDataSource crmDataSource) {
        return new DataSourceTransactionManager(crmDataSource);
    }

    @Bean
    @Primary
    public SqlSessionFactory crmSqlSessionFactory(@Qualifier("crmDataSource") DruidDataSource crmDataSource) throws Exception {
        final SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(crmDataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPERS_LOCAL));
        return bean.getObject();
    }
}
@Configuration
@MapperScan(basePackages = "com.xxxx.fullSync.dao.xir", sqlSessionFactoryRef = "xirSqlSessionFactory")
public class XirDataSourceAutoConfiguration {
    //fullSync mappers路径
    private static final String MAPPERS_LOCAL = "classpath:mappers/xir/*.xml";

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.druid.xir")
    public DruidDataSource xirDataSource() {
        return new DruidDataSource();
    }

    @Bean
    public DataSourceTransactionManager xirTransactionManager(@Qualifier("xirDataSource") DruidDataSource xirDataSource) {
        return new DataSourceTransactionManager(xirDataSource);
    }

    @Bean
    public SqlSessionFactory xirSqlSessionFactory(@Qualifier("xirDataSource") DruidDataSource xirDataSource) throws Exception {
        final SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(xirDataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPERS_LOCAL));
        return bean.getObject();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值