Spring Boot-PostgreSQL驱动程序无法在类路径中找到



我正试图将PostgreSQL数据库连接到我的Spring应用程序,但我不断收到与我的应用程序无法找到org.PostgreSQL.Driver类有关的错误。

这是pom.xml,我证明我是从依赖项获得jar的:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>slupov</groupId>
<artifactId>slupov-personal</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<finalName>slupov-personal</finalName>
<plugins>
...
</plugins>
</build>
<!--    Spring Boot dependency-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
</parent>
<dependencies>
<!-- https://mvnrepository.com/artifact/postgresql/postgresql -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
...
</dependencies>
</project>

我可以确认下载的jar包含org.postgresql.Driver.

现在,当我试图检查类是否在类路径中时,我会得到一个异常,显示它不在:

try {
Class.forName("org.postgresql.Driver");
//on classpath
} catch(ClassNotFoundException e) {
// breaks here -> not on classpath
System.out.println("Not");
}

这是我的应用程序。Hibernate连接的属性

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.connection.url=jdbc:postgresql://localhost:5432/SlupovPersonal?useSSL=false
spring.datasource.connection.username=postgres
spring.datasource.connection.password=Sigma255!
spring.datasource.dialect=org.hibernate.dialect.PostgreSQLDialect9.2
spring.datasource.show_sql=true
spring.datasource.current_session_context_class=thread
spring.datasource.hbm2ddl.auto=create
spring.datasource.hibernate.dbcp.initialSize=5
spring.datasource.hibernate.dbcp.maxTotal=20
spring.datasource.hibernate.dbcp.maxIdle=10
spring.datasource.hibernate.dbcp.minIdle=5
spring.datasource.hibernate.dbcp.maxWaitMillis=-1

如何修复此问题,使我的应用程序通过Hibernate使用数据库?

我修改了你的application.properties以使用

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/SlupovPersonal?useSSL=false
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.properties.show_sql=true
spring.jpa.properties.current_session_context_class=thread
spring.jpa.properties.hbm2ddl.auto=create
spring.datasource.dbcp2.initial-size=5
spring.datasource.dbcp2.maxTotal=20
spring.datasource.dbcp2.maxIdle=10
spring.datasource.dbcp2.minIdle=5
spring.datasource.dbcp2.maxWaitMillis=-1

此外,我们需要将postgressql的版本更改为第二条注释中建议的更高版本,或者您可以删除该版本,使其采用spring-boot托管版本。

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>

最新更新