我正在尝试将Postgres DB连接到我的Spring应用程序,但我得到了以下错误:
未能配置DataSource:未指定“datasource”属性,也无法配置嵌入式数据源。原因:未能确定合适的驱动程序类
操作:
考虑以下几点:如果您想要一个嵌入式数据库(H2、HSQL或Derby),请将其放在类路径上。如果要从特定的配置文件加载数据库设置,则可能需要激活它(当前没有活动的配置文件)。
application.yaml
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
<?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>发布于 2021-06-30 06:11:25
您似乎从您的spring中的所有属性中丢失了application.yaml前缀。它应包括以下内容:
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: truehttps://stackoverflow.com/questions/68185621
复制相似问题