1. 程式人生 > >SpringBoot多資料來源的配置(SpringBoot+MyBatis)

SpringBoot多資料來源的配置(SpringBoot+MyBatis)

遇到的問題 1,@Primary註解是必要的,不然會出現異常.
  1. org.springframework.beans.factory.NoUniqueBeanDefinitionException:No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: test1DataSource,test2DataSource
2,因為存在多個DataSource,SqlSessionFactory,PlatformTransactionManager,SqlSessionTemplate所以要使用"name"來區分. 3,SqlSessionTemplate如果不配置,在訪問主資料來源的資料沒有問題,但是訪問另一個數據源就會出現異常.
  1. org
    .apache.ibatis.binding.BindingException:Invalid bound statement (not found): cn.zz.mapper1.UserMapper1.findAll
4,[email protected]上需要加註解 @MapperScan(basePackages = "cn.zz.mapper1", sqlSessionTemplateRef  = "test2SqlSessionTemplate")
結構
資料來源
  1. package cn.zz.config
    ;
  2. import org.apache.ibatis.session.SqlSessionFactory;
  3. import org.mybatis.spring.SqlSessionFactoryBean;
  4. import org.mybatis.spring.SqlSessionTemplate;
  5. import org.mybatis.spring.annotation.MapperScan;
  6. import org.springframework.beans.factory.annotation.Qualifier;
  7. import org.springframework.boot.autoconfigure
    .jdbc.DataSourceBuilder;
  8. import org.springframework.boot.context.properties.ConfigurationProperties;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.context.annotation.Primary;
  12. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  13. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  14. import org.springframework.transaction.PlatformTransactionManager;
  15. import javax.sql.DataSource;
  16. @Configuration
  17. @MapperScan(basePackages ="cn.zz.mapper", sqlSessionTemplateRef ="test1SqlSessionTemplate")
  18. publicclassDataSource1{
  19. @Bean(name ="test1DataSource")
  20. @ConfigurationProperties(prefix ="spring.datasource.primary")
  21. @Primary
  22. publicDataSource testDataSource(){
  23. returnDataSourceBuilder.create().build();
  24. }
  25. @Bean(name ="test1SqlSessionFactory")
  26. @Primary
  27. publicSqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource")DataSource dataSource)throwsException{
  28. SqlSessionFactoryBean bean =newSqlSessionFactoryBean();
  29. bean.setDataSource(dataSource);
  30. bean.setMapperLocations(newPathMatchingResourcePatternResolver().getResources("classpath:mybatis/*.xml"));
  31. return bean.getObject();
  32. }
  33. @Bean(name ="test1TransactionManager")
  34. @Primary
  35. publicPlatformTransactionManager testTransactionManager(@Qualifier("test1DataSource")DataSource dataSource){
  36. returnnewDataSourceTransactionManager(dataSource);
  37. }
  38. @Bean(name ="test1SqlSessionTemplate")
  39. @Primary
  40. publicSqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory")SqlSessionFactory sqlSessionFactory)throwsException{
  41. returnnewSqlSessionTemplate(sqlSessionFactory);
  42. }
  43. }
  1. package cn.zz.config;
  2. import org.apache.ibatis.session.SqlSessionFactory;
  3. import org.mybatis.spring.SqlSessionFactoryBean;
  4. import org.mybatis.spring.SqlSessionTemplate;
  5. import org.mybatis.spring.annotation.MapperScan;
  6. import org.springframework.beans.factory.annotation.Qualifier;
  7. import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
  8. import org.springframework.boot.context.properties.ConfigurationProperties;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  12. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  13. import org.springframework.transaction.PlatformTransactionManager;
  14. import javax.sql.DataSource;
  15. @Configuration
  16. @MapperScan(basePackages ="cn.zz.mapper1", sqlSessionTemplateRef ="test2SqlSessionTemplate")
  17. publicclassDataSource2{
  18. @Bean(name ="test2DataSource")
  19. @ConfigurationProperties(prefix ="spring.datasource.secondary")
  20. publicDataSource testDataSource(){
  21. returnDataSourceBuilder.create().build();
  22. }
  23. @Bean(name ="test2SqlSessionFactory")
  24. publicSqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource")DataSource dataSource)throwsException{
  25. SqlSessionFactoryBean bean =newSqlSessionFactoryBean();
  26. bean.setDataSource(dataSource);
  27. bean.setMapperLocations(newPathMatchingResourcePatternResolver().getResources("classpath:mybatis1/*.xml"));
  28. return bean.getObject();
  29. }
  30. @Bean(name ="test2TransactionManager")
  31. publicPlatformTransactionManager testTransactionManager(@Qualifier("test2DataSource")DataSource dataSource){
  32. returnnewDataSourceTransactionManager(dataSource);
  33. }
  34. @Bean(nam