在開發中常常會遇到一個程序須要調用多個數據庫的狀況,總得來講分爲下面的幾種狀況:java
下面針對第一種狀況,提供一個解決方案。mysql
由於兩個數據庫的功能和結構不同,因此能夠根據功能和結構把DAO分爲兩個package。而後再mapperscan中指定不一樣的package對接不一樣的數據源,便可達到多個數據源的共存。spring
spring: datasource: emanage: url: jdbc:mysql://127.0.0.1:3306/emanage?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC&useAffectedRows=true username: root password: ****** driver-class-name: com.mysql.cj.jdbc.Driver ehr: url: jdbc:mysql://127.0.0.1:3306/ehr?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC&useAffectedRows=true username: root password: ******** driver-class-name: com.mysql.cj.jdbc.Driver
爲了避免必要的干擾,我把druid數據源的配置部分給刪除了。sql
@Configuration @MapperScan(basePackages = {"com.emanage.ehr.mapper.emanage"},sqlSessionTemplateRef = "sqlTemplate1") public class DataSourceConfig1 { @Bean(name = "datasource1") @ConfigurationProperties(prefix = "spring.datasource.emanage") public DruidDataSource druidDataSource1() { return DruidDataSourceBuilder.create().build(); } @Bean(name = "sqlFactory1") public SqlSessionFactory sqlSessionFactory(@Qualifier("datasource1") DruidDataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); factoryBean.setMapperLocations(resolver.getResources("classpath*:mapper/emanage/**Mapper.xml")); return factoryBean.getObject(); } @Bean(name = "sqlTemplate1") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlFactory1") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
@Configuration @MapperScan(basePackages = {"com.emanage.ehr.mapper.ehr"},sqlSessionTemplateRef = "sqlTemplate2") public class DataSourceConfig2 { @Bean(name = "datasource2") @ConfigurationProperties(prefix = "spring.datasource.ehr") public DataSource druidDataSource1() { return DataSourceBuilder.create().build(); } @Bean(name = "sqlFactory2") public SqlSessionFactory sqlSessionFactory(@Qualifier("datasource2") DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); factoryBean.setMapperLocations(resolver.getResources("classpath*:mapper/ehr/**Mapper.xml")); return factoryBean.getObject(); } @Bean(name = "sqlTemplate2") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlFactory2") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
兩個datasource的配置基本上同樣。就是創建datasource,sqlsessionFactory,sqlSessionTemplate的注入。而後經過mapperscan來指定具體什麼包採用什麼數據源。而後再對應包裏就和之前單數據源同樣操做便可。數據庫
若是用myBatis, SqlSessionFactory 部分可使用SqlSessionFactoryBean來生成。可是若是用mybatis plus必定要用MybatisSqlSessionFactoryBean 來生成SqlSessionFactory。不然會報錯 ,沒法直接經過BaseMapper去調用查詢。
若是要再不一樣的包中混合上XML進行調用。須要在SqlSessionFactory的配置中設置factoryBean.setMapperLocations(resolver.getResources("classpath*:mapper/ehr/**Mapper.xml"));session
優勢:
簡單,經過簡單的設置。就能夠知足大多數的狀況。數據結構
那麼問題來了:若是要在一個package下面,想調用哪一個就調用哪一個數據源怎麼辦呢?有時間了,下一篇文章寫寫另外的實現方式。mybatis
但願對初學者有價值,若是有疑問歡迎留言交流。app