Java 设置:Spring boot+jpa+postgresql

Java 设置:Spring boot+jpa+postgresql,java,spring,hibernate,spring-boot,spring-data-jpa,Java,Spring,Hibernate,Spring Boot,Spring Data Jpa,搜索了很长时间后,我不知道如何使用spring boot和postgresql。 Spring启动时没有警告。 postgres服务器正在运行,但我不知道spring boot是否可以访问它。 以下文件正确吗 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/200

搜索了很长时间后,我不知道如何使用spring boot和postgresql。 Spring启动时没有警告。 postgres服务器正在运行,但我不知道spring boot是否可以访问它。 以下文件正确吗

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>

        <groupId>org.springframework</groupId>
        <artifactId>gs-spring-boot</artifactId>
        <version>0.1.0</version>

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
        </parent>

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

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jetty</artifactId>
            </dependency>
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>4.3.3.RELEASE</version>
            </dependency>
        </dependencies>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>


    </project>
实体:

@Entity
public class Frame implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  private String raw;
  private String time;
  private double tempRad;
  private double tempAirIn;
  private double tempAirOut;

  public Frame(String time, double tempRad, double tempAirIn, double tempAirOut){
    this.time = time;
    this.tempRad = tempRad;
    this.tempAirIn = tempAirIn;
    this.tempAirOut = tempAirOut;
  }
.
.
.
Application.java:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        FrameServiceImpl frameServiceImpl = new FrameServiceImpl();
        int lower = 12;
        int higher = 29;
        for(int i = 0; i > 10000; i++){
            frameServiceImpl.save(new Frame(LocalDateTime.now().plusMinutes(i).toString(),
                (Math.random() * (higher-lower)) + lower,
                (Math.random() * (higher-lower)) + lower,
                (Math.random() * (higher-lower)) + lower));

        }
        SpringApplication.run(Application.class, args);
        System.out.println("Server started.");
    }

}
RestController:

@RestController
public class HelloController {

    @Autowired
    private FrameRepository frameRepository;

    @RequestMapping("/findall")
    public String findAll(){
        String result = "";
        int i = 0;

        for(Frame f : frameRepository.findAll()){
            result += i + " : " + f.toString() + "</br>";
            i++;
        }

        return "result : " + result;
    }
}
[编辑] 现在正在工作。 -FrameServiceImpl需要@Component进行@Autowired -@Autowired组件在主功能静态中不工作 -@自动连接FrameServiceImpl,不要实例化它。

在属性文件中,我这样做了

和聚甲醛


你可以在frames表中添加几条记录,看看是否在控制器输出中看到它们。frame表是空的,我不知道为什么。SpringBoot应该设置它,请参见spring.jpa.hibernate.ddl auto=update。你的意思是手动添加它们。我会努力的,你是对的。我在数据库中手动添加了两行,我可以在浏览器中看到它们。所以我的问题是将它们保存在数据库中。
@RestController
public class HelloController {

    @Autowired
    private FrameRepository frameRepository;

    @RequestMapping("/findall")
    public String findAll(){
        String result = "";
        int i = 0;

        for(Frame f : frameRepository.findAll()){
            result += i + " : " + f.toString() + "</br>";
            i++;
        }

        return "result : " + result;
    }
}
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/springbootdb
spring.datasource.username=postgres
spring.datasource.password=db
spring.jpa.database-platform=postgres
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=false
<dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
</dependency>