我刚刚创建了一个新的SpringBoot应用程序(Java11),并试图连接到Postgres数据库。
启动SpringBoot应用程序时,试图连接到数据库时出错。错误报告说它是Cannot load driver class: org.postgresql.Driver
。
问题
如何更改下面的配置以使SpringBoot应用程序连接到数据库?
数据库版本
PostgreSQL 12.6 on x86_64- Apple -darwin16.7.0,由Apple版本8.1.0 (clang-802.0.42)编译,64位
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
application.properties
# pims datasource
spring.datasource.url=jdbc:postgresql://localhost:5432/pims
spring.datasource.username=postgres
spring.datasource.password=
spring.datasource.driverClassName=org.postgresql.Driver
#spring.datasource.driver-class-name=org.postgresql.Driver
#spring.jpa.database-platform=postgres
spring.jpa.show-sql=true
spring.datasource.dbcp2.test-while-idle=true
spring.datasource.dbcp2.validation-query=select 1
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
当我运行@SpringBootApplication
类时,SpringBoot开始按预期启动,但会得到以下错误。
误差
错误启动ApplicationContext。若要显示条件报告,请在启用“调试”的情况下重新运行应用程序。2021-06-15 11:58:51.770错误68967 --主o.s.boot.SpringApplication
应用程序运行失败
org.springframework.beans.factory.UnsatisfiedDependencyException:错误创建名为“dataSourceScriptDatabaseInitializer”的类路径资源org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class:不满意的依赖项是通过方法'dataSourceScriptDatabaseInitializer‘参数0表示的;嵌套异常是创建类路径资源中定义的名为'dataSource’的bean时通过工厂方法失败创建bean的错误;嵌套异常是org.springframework.beans.BeanInstantiationException:未能实例化com.zaxxer.hikari.HikariDataSource:工厂方法'dataSource‘抛出的异常;嵌套异常是java.lang.IllegalStateException:无法加载驱动程序类: org.postgresql.Driver
如果我删除以下条目(即,不要在application.properties
中定义驱动程序,而只是具有pom依赖项):
spring.datasource.driverClassName=org.postgresql.Driver
然后,我得到以下错误:
Failed to load driver class org.postgresql.Driver in either of HikariConfig class loader or Thread context classloader
发布于 2021-06-15 10:44:29
当我扩展外部依赖树时,没有为postgressql找到任何jar。在重新导入maven依赖项后,将正确添加该依赖项,并且问题将消失。现在没事了。
发布于 2021-06-15 11:43:18
您不需要任何特定的配置来连接数据库,除非您的存储库和实体包没有从另一个依赖项中提取。
我建议您检查您的计算机和maven存储库之间的连接。你应该有点想念你的后遗症。
(您还将尝试提供postgresql工件的特定版本)
https://stackoverflow.com/questions/67984274
复制相似问题