我正在编写具有两个实体类教师和部门的 Spring Boot 应用程序。我正在使用 JPA 和 H2 内存数据库。当我运行应用程序表时没有在 h2 数据库中创建。我已经使用 @componentScan 注释指定要扫描的包,但我认为 spring 没有找到实体类的位置。
应用类-
package com.H2DatabaseDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages="com.H2DatabaseDemo")
public class H2DatabaseDemoApplication {
public static void main(String[] args) {
SpringApplication.run(H2DatabaseDemoApplication.class, args);
}
}
作者实体-
package com.H2DatabaseDemo.model;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Author {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
private String title;
private String body;
public Author() {}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
邮政实体-
package com.H2DatabaseDemo.model;
import java.awt.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Post {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
private String name;
public Post() {}
public Post(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
相关分类