簡體   English   中英

Spring Boot Postgresql在安裝配置上失敗

[英]Spring boot postgresql fail on setup configuration

我正在嘗試將Spring Boot應用程序連接到Postgres數據庫,但是我無法設置,我也遵循Spring入門教程和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.

我嘗試按照其他響應和教程修復錯誤,但失敗了。 這可能是什么問題? 我需要配置其他東西嗎?

您具有所有依賴關系,但在啟動過程中明確刪除了數據庫自動配置:

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

注釋或刪除該屬性。

您的Thing類文件名是什么? 更改文件名(如Thing然后重新啟動程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM