Spring Boot Postgresql的设置配置失败

时间:2019-03-17 16:29:57

标签: java spring postgresql spring-boot jpa

我正在尝试将Spring Boot应用程序连接到postgres数据库, 但是我无法设置,我也遵循春季入门教程和bealdum,但我无法解决问题:

这是我的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 http://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.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>example</name>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我的属性文件:

spring.datasource.url=jdbc:postgresql://localhost:5432/database
spring.datasource.username=user
spring.datasource.password=pass
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto = update

我的Spring Boot应用程序类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories
public class ExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(ExampleApplication.class, args);
    }

}

控制器类:

import com.example.example.entities.Thing;
import com.example.example.repositories.ThingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CreateThingController {

    private ThingRepository thingRepository;

    @Autowired
    public CreateThingController(ThingRepository thingRepository){
        this.thingRepository = thingRepository;
    }

    @PostMapping("/things")
    public void createThing(@RequestBody Thing thing) {
        this.thingRepository.save(thing);
    }
}

我的实体类:

import org.springframework.data.annotation.Id;

import javax.persistence.*;

@Entity
public class Thing {

    @Id
    @Column(name = "id", nullable = false, updatable = false)
    private String id;

    @Column(name = "name", nullable = false, updatable = false)
    private String name;

    public Thing(
            String id,
            String name
    ) {
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

这是我的存储库界面:

import com.example.example.entities.Thing;
import org.springframework.stereotype.Repository;
import org.springframework.data.jpa.repository.JpaRepository;


@Repository
public interface ThingRepository extends JpaRepository<Thing, String> 
{

}

错误是这样的:

Parameter 0 of constructor in com.example.example.controllers.CreateThingController required a bean named 'entityManagerFactory' that could not be found.

我尝试按照其他响应和教程修复错误,但失败了。 这可能是什么问题?我需要配置其他内容吗?

2 个答案:

答案 0 :(得分:1)

您具有所有依赖关系,但在启动过程中明确删除了数据库自动配置:

spring.autoconfigure.exclude=
      org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

评论或删除该属性。

答案 1 :(得分:0)

您的Thing类文件名是什么?更改该文件名,例如Thing,然后重新启动程序。