【问题标题】:Spring Boot DataSource: 'url' attribute is not specifiedSpring Boot DataSource:未指定“url”属性
【发布时间】:2020-10-30 15:29:56
【问题描述】:

我正在尝试遵循本指南 https://spring.io/guides/gs/accessing-data-mysql/

但本指南适用于 maven,我正在尝试 gradle

得到这个错误


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

在 application.properties 我只有这些东西。

spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

我尝试了 SO 上所有可能的东西。

我唯一的部门是这些

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

【问题讨论】:

  • spring.datasource.url 未在您的属性文件中设置。假设您使用的是本地 mysql 数据库,可能应该将其设置为:localhost:3306/mydb
  • @Ervin 我认为由于stackoverflow的格式而错过了它。我已经更新了这个问题。如果我遗漏了什么,您能告诉我吗?
  • 可以参考这个链接:stackoverflow.com/questions/52915923/…
  • 您确定使用了这个 application.properties 文件吗?你在哪里定义了这个属性文件?
  • @Berthol 我也检查了那个问题,他们告诉我排除我不想做的DataSourceAutoConfiguration.class

标签: mysql spring spring-boot


【解决方案1】:

这是一个老问题,但对于遇到这个问题的其他人来说,如果您有任何机会使用 IntelliJ(就像 OP 在此处基于他/她的 cmets 所做的那样),请确保我们心爱的 IDE 识别您的 application.properties/通过转到File -> Project Structure -> Modules 然后选择您的resources 文件并单击Mark as: 标题中的“资源”(基于 IntelliJ 社区版 2019.1)。另外请记住,毫无疑问,IDE 肯定会通知您,通过重新导入任何 Maven 更改,您将需要再次执行此过程。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,并通过将版本为 2.4.x 的项目迁移到 2.3.x 来解决它。

    【讨论】:

    • 降级绝不是解决办法
    【解决方案3】:

    Spring boot 已将 url 更改为 jdbc-url。

    你需要如下使用

    spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/db_example

    文档链接: https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#data-properties

    强调一下,我们当然会在文档中搜索“spring.cloud.config.jdbc-url”,但我们应该直接通过 spring.datasource.hikari.jdbc-url 进行搜索。

    【讨论】:

    • 我假设您正在使用带有 spring boot 的 Hikari 连接池。这是默认设置。检查您的 pom 依赖树 mvn dependency:tree 以找出您拥有的 jdbc 池 jar。这些属性被关联到下划线连接池。
    【解决方案4】:

    我会在别处寻找原因。

    您的错误日志告诉您 Reason: Failed to determine a suitable driver class。请构建您的项目,并检查 mysql 驱动程序是否包含在构建的 jar 文件中。如果缺少 jar,请尝试将您的 mysql 依赖范围从 runtimeOnly 更改为 implementation。 如果您尝试从 IDE 运行此项目,则可能会出现同样的问题(至少我认为是这样),并且解决方案在这种情况下也应该有所帮助。

    Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. 错误不幸的是非常普遍,并且可能在许多不同的情况下发生。例如,上周我在使用 oracle Oracle 时收到此错误,其中在 properties.yaml 中指定的特殊登录密码没有双引号。

    【讨论】:

    • 当我“忘记”在构建环境中包含 H2 驱动程序时遇到了同样的错误
    【解决方案5】:

    检查目标类路径目录。如果 application.properties 文件不存在,则删除目标并重建。

    【讨论】:

    • 在 intelliJ 哪里可以找到这个?
    【解决方案6】:

    或者你将不得不通过将这一行添加到文件application.properties来防止Spring boot自动配置数据源。

    spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

    【讨论】:

    • 您好,谢谢您的回复。没有。这也没有奏效。有一个问题提到了这种方法。但不幸的是,这没有奏效。不过谢谢!
    • 我向您推荐这个网站。它肯定会帮助你。 baeldung.com/spring-boot-failed-to-configure-data-source
    • Berthol :: 已经尝试了这些方法,仍然出现以下错误。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-07-27
    • 2019-10-20
    • 2023-02-07
    • 2020-12-24
    • 2020-04-26
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多