从小我就黑
2019-03-11 15:58
采纳率: 84.1%
浏览 2.3k
已采纳

sprongboot配置双数据源问题

springboot 要配置一个mysql数据库一个sqlserver数据库 下面是我的配置

spring:
  datasource:
    master:
      driver-class-name: com.mysql.jdbc.Driver
      jdbc-url: jdbc:mysql://***************************/gac?useUnicode=true&characterEncoding=UTF-8
      username: ****
      password: ****
    other:
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      jdbc-url: jdbc:sqlserver://***********************
      username: ****
      password: ****
  session:
    store-type: redis
  redis:
    host: ****
    port: ****

主数据库配置

@Primary
    @Bean(name = "masterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "masterSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTypeAliasesPackage("com.gac.entity");
        factoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return factoryBean.getObject();
    }

    @Primary
    @Bean(name = "masterTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean(name = "masterSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

第二个数据库配置

@Bean(name = "otherDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.other")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "otherTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("otherDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);

    }

    @Bean(name = "otherSqlSessionFactory")
    public SqlSessionFactory basicSqlSessionFactory(@Qualifier("otherDataSource") DataSource basicDataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(basicDataSource);
        factoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:other/*.xml"));
        return factoryBean.getObject();
    }

    @Bean(name = "otherSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("otherSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

启动类配置

@Configuration
@EnableTransactionManagement
@SpringBootApplication(exclude={
        DataSourceAutoConfiguration.class,
        //        HibernateJpaAutoConfiguration.class, //(如果使用Hibernate时,需要加)  
        DataSourceTransactionManagerAutoConfiguration.class,
        })

@Import({InitServiceConfiguration.class, InitConfiguration.class})
@ComponentScan("com.gac")
public class adminApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(adminApiApplication.class, args);
    }
}

报错

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: more than one 'primary' bean found among candidates: [masterDataSource, otherDataSource, dataSource]

已经在主配置里面加了@Primary 然后在启动类里面也加了(exclude={
DataSourceAutoConfiguration.class,
//        HibernateJpaAutoConfiguration.class, //(如果使用Hibernate时,需要加)  
DataSourceTransactionManagerAutoConfiguration.class,
})
为什么还会报多数据源问题? 麻烦哪位大佬帮帮看一下问题 谢谢

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 从小我就黑 2019-03-11 20:59
    已采纳

    已经自己解决掉了 我会发出博客的 有需要的盗取看一下我的博客

    已采纳该答案
    打赏 评论
  • T-Monster 2019-03-11 17:31

    对应数据源的id是对应的吗?springboot提供的数据源切换,完全依靠数据源id匹配,个人理解

    打赏 评论

相关推荐 更多相似问题