Java Spring-boot和内存DB H2.DB不在H2 Web控制台中



这是我的嵌入式数据库:

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

当我启动Junit测试时,我在应用程序上下文Intialization中创建的DB中看不到。

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

怎么了?

请参阅:https://docs.spring.io/spring-boot/docs/current/referent/referent/html/html/boot-features-sql.html.html#boot-features-sql-sql--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;
}

最新更新