springboot+mysql配置多数据源时遇到的问题

2022/6/29 2:20:25

本文主要是介绍springboot+mysql配置多数据源时遇到的问题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

解决方案:spring boot 启动类加上 exclude = DataSourceAutoConfiguration.class  代表启动项目的时候 不加载这个类

2.Unsatisfied dependency expressed through method 'sqlSessionTemplate' parameter 0

解决方案:Pom中的<relativePath /> 这一行需要被删掉

3.org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionTemplate' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionTemplate' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.apache.ibatis.session.SqlSessionFactory' available: more than one 'primary' bean found among candidates: [clusterSqlSessionFactory, masterSqlSessionFactory]

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.apache.ibatis.session.SqlSessionFactory' available: more than one 'primary' bean found among candidates: [clusterSqlSessionFactory, masterSqlSessionFactory]

报错原因,两个config文件中的方法上都加了@Primary

解决方案:将其中一个配置文件中的@Primary注掉就好了

4.more than one 'primary' bean found among candidates: [clusterSqlSessionFactory, masterSqlSessionFactory]

解决方案:因为项目中引用到第三方jar,第三方jar 里可以也有对ObjectMapper使用@Primary,造成冲突,去掉代码中的@Primary解决异常

5.Parameter 0 of method sqlSessionTemplate in org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration required a single bean, but 2 were found

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

出现原因:两个配置文件中的方法上都没有(或者都有)@Primary注解

解决方案:

  在springboot1.x时  datasource上要在一个写数据源上标记@Primary为主数据源

  而在springboot2.X时则不需要

     在其中一个配置文件中的方法上加上@Primary注解就好了

 

 

成功的方法:是在主masterConfig中的方法上加上@Primary



这篇关于springboot+mysql配置多数据源时遇到的问题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程