Java Spring-boot和内存db H2中。数据库不在H2 Web控制台中

时间:2019-03-05 15:11:11

标签: java spring-boot h2 in-memory-database

这是我的嵌入式数据库:

   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();
}

怎么了?

1 个答案:

答案 0 :(得分:1)

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

您可能要使用以下属性:

@Bean
public ServletRegistrationBean h2servletRegistration() {
    ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
    registration.addUrlMappings("/console/*");
    registration.addInitParameter("webAllowOthers", "true");
    return registration;
}

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

getInitialProps