【发布时间】:2020-02-14 23:50:18
【问题描述】:
我对尊敬的用户的问候 我是 Spring Boot 的新手,我只想在 H2 数据库中创建简单的表 这是模型类
@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
直到这里应用程序运行良好,表没有显示在 H2 控制台中的唯一问题(我不知道它是否真的创建了,但是当我使用 H2 控制台时,表是立即创建的)
那么请问可能是什么问题? 任何帮助表示赞赏
【问题讨论】:
-
你应该设置
spring.jpa.hibernate.ddl-auto=update -
如果您在内存数据库 H2 中使用,请使用此属性:spring.jpa.hibernate.ddl-auto=update
标签: spring spring-boot spring-mvc jpa h2