【发布时间】:2016-08-22 01:20:27
【问题描述】:
我正在设置一个准系统 Spring Boot 项目described here,但我遇到了基本设置问题。
似乎没有调用 DatabaseLoader,当我打开这个项目的 H2 控制台时,我看不到包含 Employee 表的架构。
这是我的 pom.xml 的相关部分:
<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-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<!--
<scope>runtime</scope>
-->
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
我的域名:
@Data
@Entity
public class Employee {
private @Id @GeneratedValue Long id;
private String firstName;
private String lastName;
private String description;
public Employee() {}
public Employee(String firstName, String lastName, String description) {
this.firstName = firstName;
this.lastName = lastName;
this.description = description;
}
}
还有数据库加载器: 公共类 DatabaseLoader 实现 CommandLineRunner { 私有最终 EmployeeRepository 存储库;
@Autowired
public DatabaseLoader(EmployeeRepository repository) {
this.repository = repository;
}
@Override
public void run(String... strings) throws Exception {
this.repository.save(new Employee("duke", "earl", "dude"));
}
}
应用程序在 localhost:8080/console 启动后导航到控制台显示 jdbc:h2:~/test 只有一个 INFORMATION_SCHEMA 和用户菜单。我还将它设置为 CRUD 存储库,虽然我可以发布到它,但似乎没有保存任何属性数据,但可以识别员工资源。
POST:{"firstName": "Bilbo", "lastName": "Baggxins", "description": "burglar"}
返回:
{
"_links": {
"self": {
"href": "http://localhost:8080/api/employees/4"
},
"employee": {
"href": "http://localhost:8080/api/employees/4"
}
}
}
【问题讨论】:
标签: spring spring-boot h2