springboot配置多數據源
阿新 • • 發佈:2018-02-01
目前 toolbar ont oca exceptio init user brush temp 在做項目的過程中難免會遇到這種情況:一個項目需要兩個數據庫中的數據,希望這篇文章能給遇到這些問題的小夥伴一點幫助
第一步:將兩個數據源的mapper接口和xml文件分別放入不同的文件夾下;
第二步:在application.yml文件中加入雙數據源,一定要指定主數據源,不然會報錯
spring: datasource: primary: driver-class-name: com.mysql.jdbc.Driver url: url地址 username: 用戶名 password: 密碼 secondary: driver-class-name: com.mysql.jdbc.Driver url: url地址 username: 用戶名 password: 密碼
第三步:config類:
@Configuration @MapperScan(basePackages = "數據源1的mapper路徑:com.dao.mapper.interface1", sqlSessionTemplateRef = "test1SqlSessionTemplate") public class DataSource1Config { @Bean(name = "test1DataSource") @ConfigurationProperties(prefix = "spring.datasource.primary") @Primary public DataSource testDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "test1SqlSessionFactory") @Primary public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/數據源1/*.xml"));//指定mapper.xml路徑 return bean.getObject(); } @Bean(name = "test1TransactionManager") @Primary public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "test1SqlSessionTemplate") @Primary public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
@Configuration @MapperScan(basePackages = "數據源2的mapper路徑:com.dao.mapper.interface2", sqlSessionTemplateRef = "test2SqlSessionTemplate") public class DataSource2Config { @Bean(name = "test2DataSource") @ConfigurationProperties(prefix = "spring.datasource.secondary") public DataSource testDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "test2SqlSessionFactory") public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/數據源2/*.xml"));//指定mapper.xml路徑 return bean.getObject(); } @Bean(name = "test2TransactionManager") public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "test2SqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
此時,項目是可以運行起來的,但是MySql有一個問題,就是配置雙數據源後,當連接池空閑時間超過8小時,數據庫連接就會自動斷掉,為了避免這種情況,還需要在yml文件下加入如下配置:
datasource: primary: max-idle: 10 max-wait: 10000 min-idle: 5 initial-size: 5 validation-query: SELECT 1 test-on-borrow: false test-while-idle: true time-between-eviction-runs-millis: 18800 #就是這句話 secondary: max-idle: 10 max-wait: 10000 min-idle: 5 initial-size: 5 validation-query: SELECT 1 test-on-borrow: false test-while-idle: true time-between-eviction-runs-millis: 18800 #就是這句話
目前配置雙數據源已經完成了。
當然,上面的配置是針對使用默認數據源的配置,但是還有很多童鞋使用了阿裏的druid數據源,兩種原理基本相同,只是稍作改動:
spring: datasource: druid: primary: driver-class-name: com.mysql.jdbc.Driver url: url地址 username: 用戶名 password: 密碼 secondary: driver-class-name: com.mysql.jdbc.Driver url: url地址 username: 用戶名 password: 密碼
在config中作如下修改:
將 DataSourceBuilder 改為 DuridDataSourceBuilder
springboot配置多數據源