解决报错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
背景
spring-boot项目使用动态数据源,配置了mysql和clickhouse数据源。
由于漏洞问题,计划将jdk从8升级到17版本,springboot升级到3.x
升级后,启动报错如下:
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
解决方案
-
网络上有几种解决方案,但都不适用于本案例
其他案例参考:jdk8版本案例 -
本案例解决办法为升级动态数据源组件
参考:Spring Boot 3 整合 Mybatis-Plus 动态数据源实现多数据源切换
参考:dynamic-datasource 官网原因:升级jdk17后, dynamic-datasource的依赖坐标发生变化,Spring Boot 3 为了区别其他版本,使用的是 dynamic-datasource-spring-boot3-starter , 而其他版本的 Spring Boot 使用 dynamic-datasource-spring-boot-starter
本案例中将 dynamic-datasource-spring-boot-starter更新为dynamic-datasource-spring-boot3-starter即可
- spring boot 3
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
<version>4.3.0</version>
</dependency>
- spring boot3以下的版本
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>4.3.0</version>
</dependency>