【发布时间】:2021-09-12 01:44:35
【问题描述】:
我正在尝试将 Postgres DB 连接到我的 Spring Boot 应用程序,但出现以下错误:
未能配置数据源:未指定“url”属性,并且无法配置嵌入式数据源。 原因:无法确定合适的驱动程序类
行动:
考虑以下: 如果您想要一个嵌入式数据库(H2、HSQL 或 Derby),请将其放在类路径中。 如果您有要从特定配置文件加载的数据库设置,您可能需要激活它(当前没有激活的配置文件)。
应用程序.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>
【问题讨论】:
-
不相关,但是:我当然希望您没有真正在应用程序中使用超级用户 (
postgres)。那将是一个非常非常糟糕的主意。