Springboot mybais配置多資料來源過程解析
一、分包方式實現:
1、在application.properties中配置兩個資料庫:
#druid連線池
#dataSoureOne(這裡是我本地的資料來源)
spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.one.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.one.jdbc-url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTCspring.datasource.one.username=root
spring.datasource.one.password=root
#dataSoureTwo(這裡是我們伺服器的資料來源)
spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.two.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.two.jdbc-url=jdbc:mysql://xx.xxx.xx.xxx:3306/kds_master_info?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTCspring.datasource.two.username=root
spring.datasource.two.password=KDSmaster123
2、建立連個資料來源的配置檔案:
注意下面DataSource包引入的是import javax.activation.DataSource;
@Configuration// 配置mybatis的介面類放的地方 @MapperScan(basePackages = "com.example.mybatis.mapper",sqlSessionFactoryRef = "sqlSessionFactoryOne") public class DataSourceConfigOne { @Bean(name = "dataSourceOne") @Primary// 表示這個資料來源是預設資料來源 // 讀取application.properties中的配置引數對映成為一個物件,prefix表示引數的字首 @ConfigurationProperties(prefix = "spring.datasource.one") public DataSource dataSourceOne() { return DataSourceBuilder.create().build(); } @Bean(name = "sqlSessionFactoryOne") @Primary public SqlSessionFactory sqlSessionFactoryOne(@Qualifier("dataSourceOne") DataSource datasource)throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(datasource); bean.setMapperLocations( // 設定mybatis的xml所在位置 new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml")); bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);return bean.getObject(); } @Primary public SqlSessionTemplate sqlsessiontemplateOne(@Qualifier("sqlsessiontemplateOne") SqlSessionFactory sessionfactory) { return new SqlSessionTemplate(sessionfactory); } }
@Configuration @MapperScan(basePackages = "com.example.mybatis.mapper2",sqlSessionFactoryRef = "sqlSessionFactoryTwo") public class DataSourceConfigTwo { @Bean(name = "dataSourceTwo") // 讀取application.properties中的配置引數對映成為一個物件,prefix表示引數的字首 @ConfigurationProperties(prefix = "spring.datasource.two") public DataSource dataSourceTwo() { return DataSourceBuilder.create().build(); } @Bean(name = "sqlSessionFactoryTwo") public SqlSessionFactory sqlSessionFactoryTwo(@Qualifier("dataSourceTwo") DataSource datasource)throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource((javax.sql.DataSource) datasource); bean.setMapperLocations( // 設定mybatis的xml所在位置 new PathMatchingResourcePatternResolver().getResources("classpath:mapper2/*.xml")); bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true); return bean.getObject(); } public SqlSessionTemplate sqlsessiontemplateTwo(@Qualifier("sqlsessiontemplateTwo") SqlSessionFactory sessionfactory) { return new SqlSessionTemplate(sessionfactory); } }
注意:1、@Primary這個註解必須要加,因為不加的話spring將分不清楚那個為主資料來源(預設資料來源)2、mapper的介面、xml形式以及dao層都需要兩個分開,目錄如圖:
3、bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(“XXXX”));mapper的xml形式檔案位置必須要配置,不然將報錯:no statement (這種錯誤也可能是mapper的xml中,namespace與專案的路徑不一致導致的)
4、在service層中根據不同的業務注入不同的dao層:
5.開始我啟動專案並訪問介面會報錯,查看了半小時才發現,是下劃線與駝峰對映失敗,這個要在sqlSessionFactoryOne和sqlSessionFactoryTwo裡面新增一行bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);才可以,然後繼續訪問,又報錯Failed to obtain JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure.後來查詢得知,要在上面的資料庫連線url中將&useSSL=true改為&useSSL=false
最後測試一下,兩個資料庫userInfo和user表的資料都顯示出來了:
userInfo:
user:
最後還有一個錯誤忘了補充,在這裡補充一下,我的springboot是2.x版本,在配置單個數據源時候,資料庫連線的url是spring.datasource.url=xxx,這樣沒有問題,但是在配置多資料來源的時候spring.datasource.one.url和spring.datasource.two.url會報錯jdbcUrl is required with driverClassName.將spring.datasource.one.url和spring.datasource.two.url中的url改成spring.datasource.one.jdbc-url,也就是將url改成jdbc-url即可。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。