gpt4 book ai didi

java - data.sql 文件未在 Spring Boot 中将数据泵入表

转载 作者:行者123 更新时间:2023-12-04 14:36:55 26 4
gpt4 key购买 nike

如果我使用 spring-boot-starter-parent 版本 2.5.0 data.sql 文件不会被执行。但是,如果我使用 2.4.1 版,一切正常。我也把我的代码放在下面。
Spring 应用程序运行器类:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

}
我的实体类:
package com.example.demo.entities;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;

@NoArgsConstructor
@AllArgsConstructor

@Getter
@Setter
@Entity
@Table(name = "cars")
public class Car {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;

@Column(name = "car_name")
public String carName;

@Column(name = "car_year")
public Integer carYear;

}
我的 application.properties 文件:
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=admin
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=create
spring.datasource.initialization-mode=always
我的 data.sql 文件:
INSERT INTO cars(car_name, car_year)
VALUES('car1', 2001);

INSERT INTO cars(car_name, car_year)
VALUES('car2', 2002);

INSERT INTO cars(car_name, car_year)
VALUES('car3', 2003);

INSERT INTO cars(car_name, car_year)
VALUES('car4', 2004);
我的 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 https://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.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>
一些带图片的额外信息:
My project structure and the table created in the database
And also I've never seen these two lines while I was running the application on spring-boot-starter-parent version 2.4.1
我完全可以使用 spring-boot-starter-parent 版本 2.4.1 直到现在我一直在使用,但我也很好奇这个问题的原因。

最佳答案

Spring Boot 2.5 Release Notes说要设置

spring.jpa.defer-datasource-initialization: true
允许 Hibernate 在加载 data.sql 之前创建模式。

关于java - data.sql 文件未在 Spring Boot 中将数据泵入表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67663817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com