Java data.sql文件未在spring引导中向表泵送数据
如果我使用SpringBootStarter父版本2.5.0 data.sql,则不会执行该文件。但是如果我使用2.4.1版,一切都会完美地工作。我把我的代码也放在下面 Spring应用程序运行程序类:Java data.sql文件未在spring引导中向表泵送数据,java,spring,spring-boot,Java,Spring,Spring Boot,如果我使用SpringBootStarter父版本2.5.0 data.sql,则不会执行该文件。但是如果我使用2.4.1版,一切都会完美地工作。我把我的代码也放在下面 Spring应用程序运行程序类: package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
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;
}
My 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
My 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>
4.0.0
org.springframework.boot
我完全可以使用SpringBootStarter的父版本2.4.1,我一直使用到现在,但我也很好奇这个问题的原因。说要设置
spring.jpa.defer-datasource-initialization: true
要允许Hibernate在加载data.sql之前创建架构,我可以看到他们提到了:
默认情况下,data.sql脚本现在在运行Hibernate之前运行
初始化。这将调整基于脚本的基本脚本的行为
使用Flyway和Liquibase进行初始化。如果你想用
data.sql要填充Hibernate创建的架构,请设置
spring.jpa.defer-datasource-initialization为true。混合时
不建议使用数据库初始化技术,这将
还允许您使用schema.sql脚本在
Hibernate在通过data.sql填充架构之前创建了架构
所以你可以试试
spring.jpa.defer-datasource-initialization:true
值得一读是的,很有效,非常感谢!!!谢谢你,好先生!