一、场景复现
配置了两个数据源,查询Dao却报错表不存在。
(1)maven
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
(2)配置文件
spring:application:name: multi-datasourceprofiles:active: devdatasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/base?autoReconnect=true&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8username: rootpassword: admin
mybatis:mapper-locations: classpath*:mapper/base/*.xmltype-aliases-package: com.mk.mybatis.multidatasourceconfiguration:map-underscore-to-camel-case: truesys:one-mybatis:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/man?autoReconnect=true&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8username: rootpassword: admin
(3)配置代码
base数据源
@Configuration
@MapperScan(value = "com.mk.mybatis.multidatasource.base.dao", sqlSessionTemplateRef = "baseSqlSessionTemplate")
public class BaseMybatisConfig {@Primary@Bean@ConfigurationProperties(prefix = "spring.datasource")public DataSourceProperties baseDataSourceProperties() {return new DataSourceProperties();}@Primary@Bean("datasource")public DataSource baseDataSource(@Qualifier("baseDataSourceProperties") DataSourceProperties dataSourceProperties) {DataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().build();return dataSource;}@Primary@Beanpublic SqlSessionFactory baseSqlSessionFactory(@Qualifier("datasource") DataSource dataSource, MybatisProperties mybatisProperties) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(mybatisProperties.resolveMapperLocations());bean.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackage());bean.setConfigurationProperties(mybatisProperties.getConfigurationProperties());bean.setConfiguration(mybatisProperties.getConfiguration());bean.setConfigLocation(Optional.ofNullable(mybatisProperties.getConfigLocation()).map(location -> {try {ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();return resourceResolver.getResource(location);} catch (Exception var3) {return null;}}).orElse(null));return bean.getObject();}@Primary@Beanpublic DataSourceTransactionManager transactionManager(@Qualifier("datasource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Primary@Beanpublic SqlSessionTemplate baseSqlSessionTemplate(@Qualifier("baseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}
one数据源
@Configuration
@MapperScan(value = "com.mk.mybatis.multidatasource.one.dao", sqlSessionTemplateRef = "oneSqlSessionTemplate")
@Slf4j
public class OneMybatisConfig {@Bean@ConfigurationProperties(prefix = "sys.one-mybatis.datasource")public DataSourceProperties oneDataSourceProperties() {return new DataSourceProperties();}@Beanpublic DataSource oneDataSource(@Qualifier("oneDataSourceProperties") DataSourceProperties dataSourceProperties) {DataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().build();return dataSource;}@Beanpublic SqlSessionFactory oneSqlSessionFactory(@Qualifier("oneDataSource") DataSource dataSource, MybatisProperties mybatisProperties)throws Exception{SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(mybatisProperties.resolveMapperLocations());bean.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackage());bean.setConfigurationProperties(mybatisProperties.getConfigurationProperties());bean.setConfiguration(mybatisProperties.getConfiguration());bean.setConfigLocation(Optional.ofNullable(mybatisProperties.getConfigLocation()).map(location->{try {ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();return resourceResolver.getResource(location);} catch (Exception var3) {return null;}}).orElse(null));return bean.getObject();}@Beanpublic DataSourceTransactionManager oneTransactionManager(@Qualifier("oneDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Beanpublic SqlSessionTemplate oneSqlSessionTemplate(@Qualifier("oneSqlSessionFactory") SqlSessionFactory sqlSessionFactory){return new SqlSessionTemplate(sqlSessionFactory);}
}
(4)SqlSessionFactory debug到的数据源是正确的
(5)两个数据源扫描的dao最后关联的数据都是one数据源
二、分析原因
以前遇到过是数据源@Resource生成bean顺序乱造成注入数据源地址错误。现在这种情况数据源没有注入错误,sqlSessionFactory也没有错,结果生成Dao关联到的数据源却是错的,百思不得其解。
经过后期的比对,发现两个SqlSessionFactory共用一个MybatisProperties,配置多一个MybatisProperties就没有出现这个问题了。
原因是共用了mybatisProperties.getConfiguration()对象,造成数据源都是最后初始化的数据源。
三、解决方案
定义oneMybatisProperties,注入自己的MybatisProperties,结果共用问题
@Bean@ConfigurationProperties(prefix = "sys.one-mybatis.mybatis")public MybatisProperties oneMybatisProperties(){return new MybatisProperties();}@Beanpublic SqlSessionFactory oneSqlSessionFactory(@Qualifier("oneDataSource") DataSource dataSource,@Qualifier("oneMybatisProperties") MybatisProperties mybatisProperties)throws Exception{SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(mybatisProperties.resolveMapperLocations());bean.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackage());bean.setConfigurationProperties(mybatisProperties.getConfigurationProperties());bean.setConfiguration(mybatisProperties.getConfiguration());bean.setConfigLocation(Optional.ofNullable(mybatisProperties.getConfigLocation()).map(location->{try {ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();return resourceResolver.getResource(location);} catch (Exception var3) {return null;}}).orElse(null));return bean.getObject();}