Spring启动postgresql在安装配置上失败

问题描述 投票:0回答:2

我试图将spring启动应用程序连接到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

我的春季启动应用程序类:

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.

我试图修复其他响应和教程后的错误,但我失败了。这可能是什么问题?我需要配置其他东西吗?

java spring postgresql spring-boot jpa
2个回答
1
投票

您拥有所有依赖项,但是您在启动期间明确删除了数据库自动配置:

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

评论或删除该属性。


0
投票

你的Thing类文件名是什么?更改文件名,如Thing并重新启动该程序。

© www.soinside.com 2019 - 2024. All rights reserved.