Spring 引导应用程序中的 H2 架构为空

H2 Schema Empty in Spring Boot Application

我正在设置准系统 Spring 引导项目 described here,我 运行 遇到基本设置问题。

似乎没有调用 DatabaseLoader,当我打开该项目的 H2 控制台时,我没有看到包含员工的架构 table。

这是我 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;
    }

}

和数据库加载器: public class 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 它,但 none 的属性数据似乎被保存了,但是 Employees 资源被识别了。

POST:{"firstName": "Bilbo", "lastName": "Baggxins", "description": "burglar"}

返回:

{
  "_links": {
    "self": {
      "href": "http://localhost:8080/api/employees/4"
    },
    "employee": {
      "href": "http://localhost:8080/api/employees/4"
    }
  }
}

这是无耻地从Josh Long那里借来的。这是一个有效的 CommandLineRunner 实现。 Github full code

@Component
class SampleRecordsCLR implements CommandLineRunner {

  private final ReservationRepository reservationRepository;

  @Autowired
  public SampleRecordsCLR(ReservationRepository reservationRepository) {
    this.reservationRepository = reservationRepository;
  }

  @Override
  public void run(String... args) throws Exception {
      Stream.of("Josh", "Jungryeol", "Nosung", "Hyobeom",
              "Soeun", "Seunghue", "Peter", "Jooyong")
              .forEach(name -> reservationRepository.save(new Reservation(name)));

      reservationRepository.findAll().forEach(System.out::println);
  }
}

带Spring引导的默认h2url是:jdbc:h2:mem:testdb

要使用 jdbc:h2:~/test,您必须将此添加到您的 application.properties:

spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:~/test

实际情况是,您正在 spring-boot 的内存数据库 (testdb) 中写入 Employee。然后,你打开h2-console到jdbc:h2:~/test,动态创建,让你浏览空虚。

使用 spring.h2.console.enabled=true 您可以打开浏览器 http://localhost:8080/h2-console 并查看数据库内容。使用以下设置:

  • 保存的设置:通用 H2(嵌入式)
  • Driver Class: org.h2.Driver
  • URL: jdbc:h2:mem:testdb
  • 用户名:sa
  • 密码: