使用 Spring Boot 在 H2 数据库中创建 table 时出现问题
Problem creating table in H2 database with SpringBoot
我对尊敬的用户的问候
我是 spring 引导的新手,我只想在 H2 数据库中创建简单的 table
这是模型 class
@Entity
public class Human
{
@Id
private long id;
private String firstname;
private String lastname;
private String email;
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
这是 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</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>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</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>
</plugin>
</plugins>
</build>
和application.properties:
spring.h2.console.enabled= true
spring.h2.console.path=/h2
spring.datasource.url=jdbc:h2:file:~/user
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
直到这里应用程序运行良好,唯一的问题是 table 没有在 H2 控制台中显示(我不知道它是否真的创建但是当我使用 H2 控制台时 table 立即创建)
所以请问可能是什么问题?
感谢任何帮助
您需要检查和验证几件事以确保其正常工作。
- 确保定义存储库层correctly.Also确保您提供了所有正确的注释。
- 如果您已为表创建查询,请确保将这些查询放在文件名
schema.sql
中。此外,请确保将文件放在目录 src/main/resources/
[=52= 下] 在提供 schema.sql
时确保禁用 spring.jpa.hibernate.ddl-auto
因为 两者不会同时工作 作为有意的。
In a JPA-based app, you can choose to let Hibernate create the
schema or use schema.sql, but you cannot do both. Make sure to disable
spring.jpa.hibernate.ddl-auto if you use schema.sql.
- 如果您有任何要执行的数据更新查询,请将它们放入文件名
data.sql
中。此外,请确保将文件放在 src/main/resources/
folder.This 目录下在 ddl-auto 创建表后执行。
存储库层如:
@Repository
public interface HumanRepository extends JpaRepository<Human, Integer> {
}
正如@sovannarith cheav 所说,您需要在 application.properties
或 application.yml
中提供 属性,例如:
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto
属性 可以有不同的值,具体取决于您的用途-case.Values 对于这个 属性 可以是:
validate: validate the schema, makes no changes to the database.
update: update the schema.
create: creates the schema, destroying previous data.
create-drop: drop the schema at the end of the session.
如果这不起作用,可能是 springboot 没有识别 H2,所以请尝试设置以下内容:
spring.datasource.platform=h2
spring.datasource.initialization-mode=always
官方Doc
这里的解决方法就是为main添加注解class
@EntityScan("com.example.model")
所以这里 Spring 将扫描包 com.example.model 并与 H2 数据库连接,然后创建 table
The table human of origins Human entity
我对尊敬的用户的问候 我是 spring 引导的新手,我只想在 H2 数据库中创建简单的 table 这是模型 class
@Entity
public class Human
{
@Id
private long id;
private String firstname;
private String lastname;
private String email;
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
这是 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</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>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</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>
</plugin>
</plugins>
</build>
和application.properties:
spring.h2.console.enabled= true
spring.h2.console.path=/h2
spring.datasource.url=jdbc:h2:file:~/user
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
直到这里应用程序运行良好,唯一的问题是 table 没有在 H2 控制台中显示(我不知道它是否真的创建但是当我使用 H2 控制台时 table 立即创建)
所以请问可能是什么问题? 感谢任何帮助
您需要检查和验证几件事以确保其正常工作。
- 确保定义存储库层correctly.Also确保您提供了所有正确的注释。
- 如果您已为表创建查询,请确保将这些查询放在文件名
schema.sql
中。此外,请确保将文件放在目录src/main/resources/
[=52= 下] 在提供schema.sql
时确保禁用spring.jpa.hibernate.ddl-auto
因为 两者不会同时工作 作为有意的。
In a JPA-based app, you can choose to let Hibernate create the schema or use schema.sql, but you cannot do both. Make sure to disable spring.jpa.hibernate.ddl-auto if you use schema.sql.
- 如果您有任何要执行的数据更新查询,请将它们放入文件名
data.sql
中。此外,请确保将文件放在src/main/resources/
folder.This 目录下在 ddl-auto 创建表后执行。
存储库层如:
@Repository
public interface HumanRepository extends JpaRepository<Human, Integer> {
}
正如@sovannarith cheav 所说,您需要在 application.properties
或 application.yml
中提供 属性,例如:
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto
属性 可以有不同的值,具体取决于您的用途-case.Values 对于这个 属性 可以是:
validate: validate the schema, makes no changes to the database.
update: update the schema.
create: creates the schema, destroying previous data.
create-drop: drop the schema at the end of the session.
如果这不起作用,可能是 springboot 没有识别 H2,所以请尝试设置以下内容:
spring.datasource.platform=h2
spring.datasource.initialization-mode=always
官方Doc
这里的解决方法就是为main添加注解class
@EntityScan("com.example.model")
所以这里 Spring 将扫描包 com.example.model 并与 H2 数据库连接,然后创建 table
The table human of origins Human entity