springboot使用druid报错 ‘url’ attribute is not specified and no embedded datasource could be configured.

在我学习springboot的过程中,需要用到druid,根据网上教程所写的在配置项中连接本地mysql数据库的配置代码为:

spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/xxx
      username: root
      password: root

但是在测试的时候遇到了问题,发现连不上mysql数据库,报错信息显示为

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 连接数据库时没有正确配置数据库连接信息。
我是用的是java jdk17的版本,这让我非常的困惑,明明依赖配置和别人一样,只有jdk版本不一样,但是为什么连不上呢,经过我漫长的查询,发现其他代码都没什么问题,应该是配置文件有问题,估计还是数据库配置的问题。
后来我尝试着改变了配置代码的写法,发现问题解决,改变后的代码如下:

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/xxx
    username: root
    password: root

    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver

问题成功解决

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐