Sping Boot 无法使用PostgreSQL驱动程序加载数据源

qoefvg9y  于 2022-12-29  发布在  PostgreSQL
关注(0)|答案(6)|浏览(231)

我已经成功地使用Spring Boot 1.0.2.RELEASE(直到今天还是1.0.1.RELEASE)开发了一个原型。
我一直在寻找,寻找,尝试解决方案,如:Spring Boot jdbc datasource autoconfiguration fails on standalone tomcatSpring Boot / Spring Data import.sql doesn't run Spring-Boot-1.0.0.RC1
他们都建议让Spring Boot来完成这项工作。当使用H2时,一切都正常,但当我试图切换到PostgreSQL时,我得到:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(org.springframework.orm.jpa.JpaVendorAdapter)] threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined

我的版本如下:

loadConfiguration()

def loadConfiguration() {
def environment = hasProperty('env') ? env : 'dev'
project.ext.envrionment = environment
println "Environment is set to $environment"

def configFile = file('config.groovy')
def config = new ConfigSlurper("$environment").parse(configFile.toURL())
project.ext.config = config
}

buildscript {
ext {
    springBootVersion = '1.0.2.RELEASE'
}
repositories {
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-   plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
apply plugin: 'groovy'

war {
baseName = 'test'
version =  '0.0.1-SNAPSHOT'
}

configurations {
providedRuntime
}

repositories {
mavenCentral()
}

dependencies {
compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-thymeleaf:${springBootVersion}")

compile("org.springframework.boot:spring-boot-starter-jdbc:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}")
compile("postgresql:postgresql:9.1-901.jdbc4")
//compile("com.h2database:h2")

testCompile("org.springframework.boot:spring-boot-starter-test:${springBootVersion}")

}

task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}

application.properties:

spring.jpa.database=POSTGRESQL
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update

spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost/cms
spring.datasource.username=cms
spring.datasource.password=NA

Removing application.properties and changing the dependency back to H2 and everything is OK.
我找不到我做错了什么:-(

hgtggwj0

hgtggwj01#

这是从哪里来的:一月一号你是说一月一号吗

mo49yndu

mo49yndu2#

spring.datasource.driver-class-name=org.postgresql.Driver

如果您最近刚将postgres添加到您的依赖项中,请确保重新加载您的项目。

xoefb8l8

xoefb8l83#

有时候,如果依赖项没有刷新,请尝试重新导入mvn项目,同时将以下内容添加到pom.xml

<dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901.jdbc4</version>
</dependency>

或者您的build.gradle

implementation "postgresql:postgresql:9.1-901.jdbc4"
oknwwptz

oknwwptz4#

我也遇到过类似的问题,需要检查pom.xml,但我保留了H2 dep,将其替换为

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

好像已经帮我修好了。

uemypmqf

uemypmqf5#

我在我的build.gradle中有这个:
实现“组织postgresql:postgresql:* 版本号 *”
但不管我怎么做,那个jar都没被下载.
作为一种变通方法,我从here下载了这个jar(点击Jar(...)),并手动将其添加到我的Java构建路径-库中。虽然不理想,但这是唯一能在2分钟内完成的事情!

qmb5sa22

qmb5sa226#

我刚刚看到这个线程,但它对我没有帮助,对我来说,问题也是在一个错误配置的pom.xml中,我只需要添加JPA:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

希望能有所帮助!

相关问题