Ice*_*nte 2 spring h2 spring-boot
我正在设置这里描述的准系统Spring Boot项目,我遇到了基本设置的问题.
似乎没有调用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>
Run Code Online (Sandbox Code Playgroud)
我的域名:
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
和数据库加载器:公共类DatabaseLoader实现CommandLineRunner {private final EmployeeRepository repository;
@Autowired
public DatabaseLoader(EmployeeRepository repository) {
this.repository = repository;
}
@Override
public void run(String... strings) throws Exception {
this.repository.save(new Employee("duke", "earl", "dude"));
}
}
Run Code Online (Sandbox Code Playgroud)
在localhost:8080/console启动应用程序后导航到控制台只显示了一个INFORMATION_SCHEMA和用户菜单的jdbc:h2:〜/ test.我还将其设置为CRUD存储库,虽然我可以发布到它,但似乎没有保存任何属性数据,但是可以识别Employees资源.
POST:{"firstName": "Bilbo", "lastName": "Baggxins", "description": "burglar"}
Run Code Online (Sandbox Code Playgroud)
回来:
{
"_links": {
"self": {
"href": "http://localhost:8080/api/employees/4"
},
"employee": {
"href": "http://localhost:8080/api/employees/4"
}
}
}
Run Code Online (Sandbox Code Playgroud)
Spring Boot的默认h2 url是: jdbc:h2:mem:testdb
要使用jdbc:h2:~/test
你必须将其添加到您的application.properties
:
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:~/test
Run Code Online (Sandbox Code Playgroud)
发生了什么,你是在spring-boot的内存数据库(testdb)中编写Employee.然后,打开h2-console jdbc:h2:~/test
,动态创建它,让你浏览空虚.
有了spring.h2.console.enabled=true
你可以打开浏览器http://localhost:8080/h2-console
,并查看数据库的内容.使用以下设置:
归档时间: |
|
查看次数: |
1745 次 |
最近记录: |