首先,springboot項目結構以下java
springboot配置文件內容以下spring
動態數據源的配置類以下(必須保證能被ComponentScan掃描到):sql
1 package com.letzgo.config; 2
3 import com.alibaba.druid.pool.DruidDataSource; 4 import org.apache.ibatis.session.SqlSessionFactory; 5 import org.mybatis.spring.SqlSessionFactoryBean; 6 import org.mybatis.spring.SqlSessionTemplate; 7 import org.mybatis.spring.annotation.MapperScan; 8 import org.springframework.beans.factory.annotation.Qualifier; 9 import org.springframework.boot.context.properties.ConfigurationProperties; 10 import org.springframework.context.annotation.Bean; 11 import org.springframework.context.annotation.Configuration; 12 import org.springframework.context.annotation.Primary; 13 import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 14 import org.springframework.jdbc.datasource.DataSourceTransactionManager; 15
16 import javax.sql.DataSource; 17
18 /** 19 * @author allen 20 * @date 2019-01-10 15:08 21 */
22 public class DynamicDatasourceConfig { 23
24 @Configuration 25 @MapperScan(basePackages = "com.letzgo.dao.master") 26 public static class Master { 27 @Primary 28 @Bean("masterDataSource") 29 @Qualifier("masterDataSource") 30 @ConfigurationProperties(prefix = "spring.datasource.master") 31 public DataSource dataSource() { 32 return new DruidDataSource(); 33 } 34
35 @Primary 36 @Bean("masterSqlSessionFactory") 37 @Qualifier("masterSqlSessionFactory") 38 public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception { 39 SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); 40 factoryBean.setDataSource(dataSource); 41 factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/master/*.xml")); 42 return factoryBean.getObject(); 43 } 44
45 @Primary 46 @Bean("masterTransactionManager") 47 @Qualifier("masterTransactionManager") 48 public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) { 49 return new DataSourceTransactionManager(dataSource); 50 } 51
52 @Primary 53 @Bean("masterSqlSessionTemplate") 54 @Qualifier("masterSqlSessionTemplate") 55 public SqlSessionTemplate sqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { 56 return new SqlSessionTemplate(sqlSessionFactory); 57 } 58
59 } 60
61 @Configuration 62 @MapperScan(basePackages = "com.letzgo.dao.slave") 63 public static class Slave { 64 @Bean("slaveDataSource") 65 @Qualifier("slaveDataSource") 66 @ConfigurationProperties(prefix = "spring.datasource.slave") 67 public DataSource dataSource() { 68 return new DruidDataSource(); 69 } 70
71 @Bean("slaveSqlSessionFactory") 72 @Qualifier("slaveSqlSessionFactory") 73 public SqlSessionFactory sqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception { 74 SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); 75 factoryBean.setDataSource(dataSource); 76 factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/slave/*.xml")); 77 return factoryBean.getObject(); 78 } 79
80 @Bean("slaveTransactionManager") 81 @Qualifier("slaveTransactionManager") 82 public DataSourceTransactionManager transactionManager(@Qualifier("slaveDataSource") DataSource dataSource) { 83 return new DataSourceTransactionManager(dataSource); 84 } 85
86 @Bean("slaveSqlSessionTemplate") 87 @Qualifier("slaveSqlSessionTemplate") 88 public SqlSessionTemplate sqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { 89 return new SqlSessionTemplate(sqlSessionFactory); 90 } 91 } 92
93 }
完成基本配置以後,分別在master和slave中寫一個數據庫訪問操做,再開放兩個簡單的接口,分別觸發master和slave的數據看訪問操做。數據庫
至此沒項目基本結構搭建已完成,啓動項目,進行測試。apache
咱們會發現這樣master的數據庫訪問是能正常訪問的,可是slave的數據庫操做是不行的,報錯信息以下:springboot
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):***session
對於這樣錯誤,起初企圖經過百度解決,大部分都是說xml文件的命名空間和dao接口全名不對應或者說是接口方法和xml中的方法不對應等等解決方法,mybatis
本人檢查了本身的代碼多遍重啓多遍均沒法解決,並非說這些方法不對,可是本案例的問題卻不是這些問題致使的。最後無奈,只能硬着頭皮去看源碼,最後發現了問題所在。app
debug源碼調試到最後,發現不管是執行mater仍是slave的數據庫操做,使用了相同的SqlSession,同一個!!!這個確定是有問題的。框架
繼續看源碼進行查,看SqlSession的注入過程。
咱們知道mybatis只要寫接口不用寫實現類(應該是3.0以後的版本),其實是使用了代理,每一個dao接口,在spring容器中實際上是對應一個MapperFactoryBean(不懂FactoryBean的能夠去多看看spring的一些核心接口,要想看懂spring源碼必需要知道的)。
當從容器中獲取bean的時候,MapperFactoryBean的getObject方法就會根據SqlSession實例生產一個MapperProxy對象的代理類。
問題的關鍵就在於MapperFactoryBean,他繼承了SqlSessionDaoSupport類,他有一個屬性,就是SqlSession,並且剛纔所說的建立代理類所依賴的SqlSession實例就是這個。那咱們看這個SqlSession實例是何時注入的就能夠了,就能找到爲何注入了同一個對象了。
找spring注入的地方,spring注入的方式我的目前知道的有註解處理器如@Autowired的註解處理器AutowiredAnnotationBeanPostProcessor等相似的BeanPostProcessor接口的實現類,還有一種就是在BeanDefinition中定義器屬性的注入方式,在bean的定義階段就決定了的,前者若是不知道的能夠看看,在此不作贅述,後者的處理過程源碼以下(只截取核心部分,感興趣的能夠本身看一下處理過程,調用鏈比較深,貼代碼會比較多,看着眼花繚亂):
debug到dao接口類的的BeanDefinition(上文已說過實際上是MapperFactoryBean),發現他的autowiremode是2,參照源碼
便可發現爲按照類型自動裝配
最關鍵的來了:
debug的時候發現,master的dao接口執行到this.autowireByType(beanName, mbd, bw, newPvs)方法中,給MapperFactoryBean中SqlSession屬性注入的實例是masterSqlSessionTemplate對象,
slave的dao接口執行該方法時注入的也是masterSqlSessionTemplate對象,按類型注入,spring容器中找到一個即注入(此時slaveSqlSessionTemplate也在容器中,爲何按類型注入找到了masterSqlSessionTemplate卻沒報錯,應該是@Primary的做用)
至此,問題產生的緣由已基本找到,那該如何解決呢?BeanDefinition爲何會定義成autowiremode=2呢,只能找@MapperScan看了,看這個註解的處理源碼,最後找到ClassPathMapperScanner如下方法:
1 private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) { 2 Iterator var3 = beanDefinitions.iterator(); 3
4 while(var3.hasNext()) { 5 BeanDefinitionHolder holder = (BeanDefinitionHolder)var3.next(); 6 GenericBeanDefinition definition = (GenericBeanDefinition)holder.getBeanDefinition(); 7 if (this.logger.isDebugEnabled()) { 8 this.logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName() + "' and '" + definition.getBeanClassName() + "' mapperInterface"); 9 } 10
11 definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName()); 12 definition.setBeanClass(this.mapperFactoryBean.getClass()); 13 definition.getPropertyValues().add("addToConfig", this.addToConfig); 14 boolean explicitFactoryUsed = false; 15 if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) { 16 definition.getPropertyValues().add("sqlSessionFactory", new RuntimeBeanReference(this.sqlSessionFactoryBeanName)); 17 explicitFactoryUsed = true; 18 } else if (this.sqlSessionFactory != null) { 19 definition.getPropertyValues().add("sqlSessionFactory", this.sqlSessionFactory); 20 explicitFactoryUsed = true; 21 } 22
23 if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) { 24 if (explicitFactoryUsed) { 25 this.logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored."); 26 } 27
28 definition.getPropertyValues().add("sqlSessionTemplate", new RuntimeBeanReference(this.sqlSessionTemplateBeanName)); 29 explicitFactoryUsed = true; 30 } else if (this.sqlSessionTemplate != null) { 31 if (explicitFactoryUsed) { 32 this.logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored."); 33 } 34
35 definition.getPropertyValues().add("sqlSessionTemplate", this.sqlSessionTemplate); 36 explicitFactoryUsed = true; 37 } 38
39 if (!explicitFactoryUsed) { 40 if (this.logger.isDebugEnabled()) { 41 this.logger.debug("Enabling autowire by type for MapperFactoryBean with name '" + holder.getBeanName() + "'."); 42 } 43
44 definition.setAutowireMode(2); 45 } 46 } 47
48 }
44行是關鍵,可是有個條件,這個條件成立的緣由就是@MapperScan註解沒有指定過sqlSessionTemplateRef或者sqlSessionFactoryRef,正由於沒有指定特定的sqlSessionTemplate或者sqlSessionFactory,mybatis默認採用按類型自動裝配的方式進行注入。
至此,問題解決方案已出:
代碼中的兩個@MapperScan用法分別改成:
1 @MapperScan(basePackages = "com.letzgo.dao.master", sqlSessionFactoryRef = "masterSqlSessionFactory", sqlSessionTemplateRef = "masterSqlSessionTemplate") 2
3 @MapperScan(basePackages = "com.letzgo.dao.slave", sqlSessionFactoryRef = "slaveSqlSessionFactory", sqlSessionTemplateRef = "slaveSqlSessionTemplate")
重啓進行測試,問題解決。
PS:
仍是對各類註解使用方法不瞭解(或者說對框架的源碼不瞭解),致使搞了這麼久的問題,還好最後查到了,記錄於此,給本身加深印象,也但願解決方案能幫到部分同行。之後仍是要多看源碼,哈哈哈。