环境说明
jdk1.7, springboot1.5.8.RELEASE, druid1.1.5,nutz1.r.66
配置步骤
- 配置文件(application.properties)
t1.data1.driver-class-name=oracle.jdbc.driver.OracleDrivert1.data1.url=jdbc:oracle:thin:@171.12.84.126:1521:ora11gt1.data1.username=oracle_78t1.data1.password=oracle_78t1.data1.initialSize=5t1.data1.minIdle=5t1.data1.maxActive=100t1.data1.maxWait=60000t1.data1.timeBetweenEvictionRunsMillis=60000t1.data1.minEvictableIdleTimeMillis=300000t1.data1.validationQuery=SELECT 1 FROM DUALt1.data1.testWhileIdle=truet1.data1.testOnBorrow=falset1.data1.testOnReturn=falset1.data1.filters=stat,wall,log4jt1.data1.poolPreparedStatements = truet1.data1.maxOpenPreparedStatements = 20t1.data1.logSlowSql=truet.data.driver-class-name=oracle.jdbc.driver.OracleDrivert.data.url=jdbc:oracle:thin:@171.12.84.126:1521:ora11gt.data.username=oracle_102t.data.password=oracle_102t.data.initialSize=5t.data.minIdle=5t.data.maxActive=100t.data.maxWait=60000t.data.timeBetweenEvictionRunsMillis=60000t.data.minEvictableIdleTimeMillis=300000t.data.validationQuery=SELECT 1 FROM DUALt.data.testWhileIdle=truet.data.testOnBorrow=falset.data.testOnReturn=falset.data.filters=stat,wall,log4jt.data.poolPreparedStatements = truet.data.maxOpenPreparedStatements = 20t.data.logSlowSql=true
- 配置多数据源
@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; }
- 集成druid
@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); }
- crud操作
@Autowired NutDao dao; @Autowired @Qualifier("sdao") NutDao sdao;
说明
当存在多个数据源时,最好加上@Primary注解,这样默认使用这个连接,不需要使用Qualifier注解说明使用某个连接。