简体   繁体   English

Spring 引导 - PostgreSQL 驱动程序无法位于类路径中

[英]Spring Boot - PostgreSQL driver cannot be located in classpath

I'm trying to connect a PostgreSQL database to my Spring application but I keep getting errors that are linked with my app not being able to find the org.postgresql.Driver class. I'm trying to connect a PostgreSQL database to my Spring application but I keep getting errors that are linked with my app not being able to find the org.postgresql.Driver class.

Here's the pom.xml as I proof I'm getting the jar from the dependency:这是 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>

I can confirm the downloaded jar contains the org.postgresql.Driver.我可以确认下载的 jar 包含 org.postgresql.Driver。

Now when I try to check whether the class is in my classpath I get an exception showing it is not there:现在,当我尝试检查 class 是否在我的类路径中时,我得到一个异常,表明它不存在:

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

Here's my application.properties for the Hibernate connection:这是 Hibernate 连接的application.properties

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

The How do I fix this so my app uses the database through Hibernate?如何解决此问题,以便我的应用通过 Hibernate 使用数据库?

I modified your application.properties to work我修改了你的 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

Also we need to change the version of postgressql to a later version as suggested in the second comment or you can remove the version so that it takes the spring boot managed version.此外,我们需要按照第二条评论中的建议将 postgressql 的版本更改为更高版本,或者您可以删除该版本,以便它采用 spring 引导管理版本。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM