Java Spring-boot和内存db H2。 db不在h2 web控制台中

问题描述 投票:2回答:1

这是我的嵌入式数据库:

   public void init() {
      EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
      db = builder
            .setType(EmbeddedDatabaseType.H2)
            .addScript("h2/create.sql")
            .addScript("h2/insert.sql")
            .build();
    }

当我启动JUnit测试时,我没有在Web控制台中看到在应用程序上下文初始化期间创建的数据库。

@Before
public void initTest() throws SQLException {
    Server webServer = Server.createWebServer("-web", "-webAllowOthers", "-webPort", "8082");
    webServer.start();
}

怎么了?

java spring-boot h2 in-memory-database
1个回答
1
投票

请参考:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-sql-h2-console

您可能希望使用以下属性:

spring.h2.console.enabled=true
spring.h2.console.path=/path/to/console

或以编程方式启动服务器:

@Bean
public ServletRegistrationBean h2servletRegistration() {
    ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
    registration.addUrlMappings("/console/*");
    registration.addInitParameter("webAllowOthers", "true");
    return registration;
}
© www.soinside.com 2019 - 2024. All rights reserved.