首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未能确定合适的驱动程序类- Spring PostgreSQL

未能确定合适的驱动程序类- Spring PostgreSQL
EN

Stack Overflow用户
提问于 2021-06-29 21:04:34
回答 1查看 3.7K关注 0票数 0

我正在尝试将Postgres DB连接到我的Spring应用程序,但我得到了以下错误:

未能配置DataSource:未指定“datasource”属性,也无法配置嵌入式数据源。原因:未能确定合适的驱动程序类

操作:

考虑以下几点:如果您想要一个嵌入式数据库(H2、HSQL或Derby),请将其放在类路径上。如果要从特定的配置文件加载数据库设置,则可能需要激活它(当前没有活动的配置文件)。

application.yaml

代码语言:javascript
复制
datasource:
  driver-class-name: org.postgresql.Driver
  url: jdbc:postgresql://localhost:5432/my_db
  username: postgres
  password: pswrd
# JPA properties
jpa:
  hibernate:
    ddl-auto: update # When you launch the application for the first time - switch "none" at "create"
  show-sql: true
  database: my_db
  database-platform: org.hibernate.dialect.PostgreSQLDialect
  open-in-view: false
  generate-ddl: true

项目结构:

pom.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>PostgresApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>PostgresApp</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-30 06:11:25

您似乎从您的spring中的所有属性中丢失了application.yaml前缀。它应包括以下内容:

代码语言:javascript
复制
spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/my_db
    username: postgres
    password: pswrd
  # JPA properties
  jpa:
    hibernate:
      ddl-auto: update # When you launch the application for the first time - switch "none" at "create"
    show-sql: true
    database: my_db
    database-platform: org.hibernate.dialect.PostgreSQLDialect
    open-in-view: false
    generate-ddl: true
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68185621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档