springboot3.2.2中使用动dynamic-datasource多数据源切换(mysql+sqlserver)遇到的bug

使用依赖

        <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看一下大佬们的解释,会少花两天时间去百度一些花里胡哨的答案!

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值