重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

springboot中如何实现druid多数据源配置

这篇文章主要为大家展示了“spring boot中如何实现druid多数据源配置”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“spring boot中如何实现druid多数据源配置”这篇文章吧。

成都创新互联专注于杜尔伯特企业网站建设,成都响应式网站建设,电子商务商城网站建设。杜尔伯特网站建设公司,为杜尔伯特等地区提供建站服务。全流程定制开发,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务

application.yml

mybatis:
  configLocation: classpath:mybatis_config.xml
spring:
  #  data:
  #    MongoDB:
  #      host: 10.0.5.126
  #      port: 27017
  #      database: xx
  #authentication-database: xx
  #password:
  #username:
  datasource:
    druid:
      min-idle: 3
      initial-size: 5
      max-active: 10
      # 连接等待超时时间
      max-wait: 10000
      # 配置检测可以关闭的空闲连接间隔时间
      time-between-eviction-runs-millis: 60000
      # 配置连接在池中的最小生存时间
      min-evictable-idle-time-millis: 300000
      validation-query: select '1'
      test-while-idle: true
      test-on-borrow: true
      test-on-return: false
      # 打开PSCache,并且指定每个连接上PSCache的大小
      pool-prepared-statements: true
      max-open-prepared-statements: 20
      max-pool-prepared-statement-per-connection-size: 20
      filter:
        stat:
          merge-sql: true
          # 慢日志查询
          log-slow-sql: true
          # 慢SQL记录超过5秒的sql在druid控制台标红
          slow-sql-millis: 5000
        wall:
          enabled: false
        commons-log:
          enabled: false
        log4j:
          connection-log-enabled: false
        slf4j:
          statement-log-enabled: false
        log4j2:
          statement-log-enabled: false
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 如果项目用的logback则删掉log4j
      filters: stat,wall
      # 配置Druid Spring监控切面
      #      aop-patterns: com.xitor.service.*,com.xonitor.schedule.*,com.xonitor.controller.*
      # 配置DruidStatFilter
      web-stat-filter:
        enabled: true
        url-pattern: "/*"
        exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*,/slife_server/druid/*"
      # 配置DruidStatViewServlet
      stat-view-servlet:
        enabled: true
        url-pattern: "/druid/*"
        allow: ""  # allow: 127.0.0.1,192.168.163.1 IP白名单(没有配置或者为空,则允许所有访问)
        # IP黑名单 (存在共同时,deny优先于allow)
        #        deny: 192.168.1.73
        #  禁用HTML页面上的“Reset All”功能 true是不禁用
        reset-enable: true
        login-username: admin
        login-password: xx
      use-global-data-source-stat: false
      clear-filters-enable: true
      time-between-log-stats-millis: 3600000 #配置每1小时输出一次统计日志,统计后将清空日志
    oms:
      name: oms
      driver-class-name: org.postgresql.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:postgresql://127.0.0.1:5432/eagle
      username: xx
      password: xx
    star-talk:
      name: starTalk
      driver-class-name: org.postgresql.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:postgresql://10.0.5.105:5432/ejabberd
      username: xx
      password: xx
    logging:
      name: logging
      driver-class-name: org.postgresql.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:postgresql://127.0.0.1:5432/eagle
      username: xx
      password: xx
    wpms: 
      name: wpms
      driver-class-name: oracle.jdbc.driver.OracleDriver
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:oracle:thin:@localhost:1521:ORCL
      username: xx
      password: xx

DataSourceConfigurer

@Configuration
public class OMSDataSourceConfigurer {

    @Value("${mybatis.configLocation}")
    private Resource configLocation;

    @Primary
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.oms")
    public DataSourceProperties omsDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(initMethod = "init")
    @Primary
    @ConfigurationProperties("spring.datasource.druid")
    public DruidDataSource omsDataSource(@Qualifier("omsDataSourceProperties") DataSourceProperties dataSourceProperties) {
        return (DruidDataSource) dataSourceProperties.initializeDataSourceBuilder()
                .build();
    }

    @Bean
    @Primary
    public SqlSessionFactory omsSqlSessionFactory(@Qualifier("omsDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        Assert.notNull(this.configLocation, "");
        Assert.isTrue(this.configLocation.exists(), "");
        bean.setConfigLocation(configLocation);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis_mapper/oms/*.xml"));
        return bean.getObject();
    }

    @Bean
    @Primary
    public DataSourceTransactionManager omsTransactionManager(@Qualifier("omsDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    @Primary
    public SqlSessionTemplate omsSqlSessionTemplate(@Qualifier("omsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


}

WPMSDataSourceConfigurer

与上面配置相同,不要添加 @Primary

MybatisConfigurer

@Configuration
@ComponentScan("com.gridvo.eagle.repository")
@MapperScan(basePackages = "com.xx.mybatis.oms.dao", sqlSessionTemplateRef = "omsSqlSessionTemplate")
@MapperScan(basePackages = "com.xx.mybatis.startalk.dao", sqlSessionTemplateRef = "starTalkSqlSessionTemplate")
@MapperScan(basePackages = "com.xx.mybatis.logging.dao", sqlSessionTemplateRef = "loggingSqlSessionTemplate")
@MapperScan(basePackages = "com.xxmybatis.wpms.dao", sqlSessionTemplateRef = "wpmsSqlSessionTemplate")
@EnableTransactionManagement(proxyTargetClass = true)
public class MybatisConfigurer {

}

以上是“spring boot中如何实现druid多数据源配置”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


本文标题:springboot中如何实现druid多数据源配置
链接URL:http://cqcxhl.com/article/jocjpe.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP