SpringBoot多資料來源的配置(SpringBoot+MyBatis)
阿新 • • 發佈:2019-02-12
遇到的問題
1,@Primary註解是必要的,不然會出現異常.
結構
資料來源
2,因為存在多個DataSource,SqlSessionFactory,PlatformTransactionManager,SqlSessionTemplate所以要使用"name"來區分. 3,SqlSessionTemplate如果不配置,在訪問主資料來源的資料沒有問題,但是訪問另一個數據源就會出現異常.
org.springframework.beans.factory.NoUniqueBeanDefinitionException:No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: test1DataSource,test2DataSource
4,[email protected]上需要加註解 @MapperScan(basePackages = "cn.zz.mapper1", sqlSessionTemplateRef = "test2SqlSessionTemplate")
org
.apache.ibatis.binding.BindingException:Invalid bound statement (not found): cn.zz.mapper1.UserMapper1.findAll
結構
資料來源
package cn.zz.config
;import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure
.jdbc.DataSourceBuilder;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages ="cn.zz.mapper", sqlSessionTemplateRef ="test1SqlSessionTemplate")
publicclassDataSource1{
@Bean(name ="test1DataSource")
@ConfigurationProperties(prefix ="spring.datasource.primary")
@Primary
publicDataSource testDataSource(){
returnDataSourceBuilder.create().build();
}
@Bean(name ="test1SqlSessionFactory")
@Primary
publicSqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource")DataSource dataSource)throwsException{
SqlSessionFactoryBean bean =newSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(newPathMatchingResourcePatternResolver().getResources("classpath:mybatis/*.xml"));
return bean.getObject();
}
@Bean(name ="test1TransactionManager")
@Primary
publicPlatformTransactionManager testTransactionManager(@Qualifier("test1DataSource")DataSource dataSource){
returnnewDataSourceTransactionManager(dataSource);
}
@Bean(name ="test1SqlSessionTemplate")
@Primary
publicSqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory")SqlSessionFactory sqlSessionFactory)throwsException{
returnnewSqlSessionTemplate(sqlSessionFactory);
}
}
package cn.zz.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages ="cn.zz.mapper1", sqlSessionTemplateRef ="test2SqlSessionTemplate")
publicclassDataSource2{
@Bean(name ="test2DataSource")
@ConfigurationProperties(prefix ="spring.datasource.secondary")
publicDataSource testDataSource(){
returnDataSourceBuilder.create().build();
}
@Bean(name ="test2SqlSessionFactory")
publicSqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource")DataSource dataSource)throwsException{
SqlSessionFactoryBean bean =newSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(newPathMatchingResourcePatternResolver().getResources("classpath:mybatis1/*.xml"));
return bean.getObject();
}
@Bean(name ="test2TransactionManager")
publicPlatformTransactionManager testTransactionManager(@Qualifier("test2DataSource")DataSource dataSource){
returnnewDataSourceTransactionManager(dataSource);
}
@Bean(nam