springboot3.2.2中使用动dynamic-datasource多数据源切换(mysql+sqlserver)遇到的bug
先注意,我们使用的是springboot3版本,然后去大佬的仓库找对应的依赖,ps:我都使用多数据源了,你还不让自动注入datasource,有毒吧!,从github中明显看到。
·
使用依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>9.4.1.jre8</version>
</dependency>
application.yml配置
spring:
datasource:
dynamic:
primary: mysql
datasource:
mysql:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/xxl_job?useUnicode=true&allowPublicKeyRetrieval=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
sqlserver:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://ip:port;databaseName=dongbao_test;
username: xxxx
password: xxx
druid:
validation-query: SELECT 1
strict: false
hikari:
pool-name: HikariCP
minimum-idle: 5
idle-timeout: 60000
maximum-pool-size: 10
auto-commit: true
connection-test-query: SELECT 1
异常信息:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (the profiles dev are currently active)
[com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Failed to determine a suitable driver class
2024-05-27 17:46:00.875 [main] INFO o.a.catalina.core.StandardService - Stopping service [Tomcat]
2024-05-27 17:46:00.925 [main] INFO o.s.b.a.l.ConditionEvaluationReportLogger -
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-05-27 17:46:00.933 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter -
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (the profiles dev are currently active).
解决方案:
错误的:
ps:我都使用多数据源了,你还不让自动注入datasource,有毒吧!
// 取消数据源自动注入
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
正解:
先注意,我们使用的是springboot3版本,然后去大佬的仓库找对应的依赖,源码仓库,或者GitHub文档,从github中明显看到
答案已经很明显了:
下面是我修正后的,修改完后项目运行一切正常
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
<version>4.3.0</version>
</dependency>
写在最后:
如果你开始使用你不熟悉的技术依赖,先去gitehub看一下大佬们的解释,会少花两天时间去百度一些花里胡哨的答案!

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)