【问题标题】:Spring boot 2 does not work with PostgresSpring Boot 2 不适用于 Postgres
【发布时间】:2023-03-26 10:50:01
【问题描述】:

我有一个带有 spring boot 2.0.3.RELEASE 的 Spring Boot JPA 应用程序并连接到 PostgreSQL,当我运行该应用程序时,我收到以下错误消息:

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:
  datasource:
    url: jdbc:postgresql://localhost:5432/testS
    username: postgres
    password: postgres123
    driver-class-name: org.postgresql.Driver

依赖关系:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
    </dependency>

堆栈跟踪:

Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

我该如何解决这个问题?

【问题讨论】:

  • 分享 stacktrace 会有帮助的
  • @GauravSrivastav 完成
  • 尝试在数据源中使用属性:dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
  • 还是一样的问题

标签: java spring spring-boot jpa


【解决方案1】:

您刚刚更改了依赖项,如下所示。在依赖项中包含版本和范围元素。

<dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.1-901.jdbc4</version>
    <scope>runtime</scope>
</dependency>

【讨论】:

  • 还是一样的问题
  • 你的 postgres 版本,jar 应该和它兼容。另一件事是检查依赖项是否下载。
【解决方案2】:

我在 application.yml 中使用 spring 配置文件时遇到了这个问题。 我只在 yaml 的配置文件部分定义了 spring.datasource.* 属性。 但是,当我将这些属性移到 MAIN 部分(即默认配置文件)时,问题就消失了。

【讨论】:

    猜你喜欢
    • 2018-08-17
    • 1970-01-01
    • 2015-04-13
    • 2018-10-18
    • 2018-07-04
    • 2019-02-24
    • 2016-04-02
    • 2020-03-19
    • 2019-11-18
    相关资源
    最近更新 更多