springboot集成druid配置多数据源连接

2019-04-16 14:04:46 浏览数 (2)


##环境说明

jdk1.7, springboot1.5.8.RELEASE, druid1.1.5,nutz1.r.66

##配置步骤

  1. 配置文件(application.properties)
代码语言:javascript复制
t1.data1.driver-class-name=oracle.jdbc.driver.OracleDriver
t1.data1.url=jdbc:oracle:thin:@171.12.84.126:1521:ora11g
t1.data1.username=oracle_78
t1.data1.password=oracle_78
t1.data1.initialSize=5
t1.data1.minIdle=5
t1.data1.maxActive=100
t1.data1.maxWait=60000
t1.data1.timeBetweenEvictionRunsMillis=60000
t1.data1.minEvictableIdleTimeMillis=300000
t1.data1.validationQuery=SELECT 1 FROM DUAL
t1.data1.testWhileIdle=true
t1.data1.testOnBorrow=false
t1.data1.testOnReturn=false
t1.data1.filters=stat,wall,log4j
t1.data1.poolPreparedStatements = true
t1.data1.maxOpenPreparedStatements = 20
t1.data1.logSlowSql=true
t.data.driver-class-name=oracle.jdbc.driver.OracleDriver
t.data.url=jdbc:oracle:thin:@171.12.84.126:1521:ora11g
t.data.username=oracle_102
t.data.password=oracle_102
t.data.initialSize=5
t.data.minIdle=5
t.data.maxActive=100
t.data.maxWait=60000
t.data.timeBetweenEvictionRunsMillis=60000
t.data.minEvictableIdleTimeMillis=300000
t.data.validationQuery=SELECT 1 FROM DUAL
t.data.testWhileIdle=true
t.data.testOnBorrow=false
t.data.testOnReturn=false
t.data.filters=stat,wall,log4j
t.data.poolPreparedStatements = true
t.data.maxOpenPreparedStatements = 20
t.data.logSlowSql=true
  1. 配置多数据源
代码语言:javascript复制
@Bean(name = "ds")
    @ConfigurationProperties(prefix = "t.data")
    public DataSource dataSource() throws SQLException {
        DataSource build = DataSourceBuilder.create().type(DruidDataSource.class).build();
        return build;
    }

    @Primary
    @Bean
    @ConfigurationProperties(prefix = "t1.data1")
    public DataSource dataSource1() throws SQLException {
        DataSource build = DataSourceBuilder.create().type(DruidDataSource.class).build();
        return build;
    }
  1. 集成druid
代码语言:javascript复制
@Primary
    @Primary
    @Bean
    public NutDao dao(DataSource dataSource) {
        return new NutDao(dataSource);
    }

    @Bean(name = "sdao")
    public NutDao sdao(@Qualifier("ds") DataSource dataSource) {
        return new NutDao(dataSource);
    }
  1. crud操作
代码语言:javascript复制
    @Autowired
    NutDao dao;

    @Autowired
    @Qualifier("sdao")
    NutDao sdao;

说明

当存在多个数据源时,最好加上@Primary注解,这样默认使用这个连接,不需要使用Qualifier注解说明使用某个连接。

源码参考

https://gitee.com/strictnerd/rdb-es.git

0 人点赞