查看由Spring启动的embedded式H2数据库的内容

我想在网页浏览器中查看由Spring启动的H2数据库的内容,感谢以下configuration:

<jdbc:embedded-database id="dataSource" type="H2" /> <jdbc:initialize-database data-source="dataSource"> <jdbc:script location="classpath:db/populateDB.sql"/> </jdbc:initialize-database> 

我在日志中search了JDBC URL:

 DEBUG osjdSimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem:dataSource;DB_CLOSE_DELAY=-1] 

所以我可以填写连接forms如下:

在这里输入图像说明

但不幸的是,数据库仍然是空的,而不应该由于populateDB.sql脚本。

任何想法?

谢谢!

与H2或HSQLDB内存数据库的查看内容几乎相同的问题。

只需将以下内容添加到您的configuration。

 <bean id="h2Server" class="org.h2.tools.Server" factory-method="createTcpServer" init-method="start" destroy-method="stop" depends-on="h2WebServer"> <constructor-arg value="-tcp,-tcpAllowOthers,-tcpPort,9092"/> </bean> <bean id="h2WebServer" class="org.h2.tools.Server" factory-method="createWebServer" init-method="start" destroy-method="stop"> <constructor-arg value="-web,-webAllowOthers,-webPort,8082"/> </bean> 

这将在与您的embedded式数据库相同的JVM中启动H2 Web控制台和TCP服务器,以便您可以使用Web浏览器(inputjdbc:h2:mem:dataSource作为URL)访问端口8082,或使用外部SQL客户端访问端口9092如SQuirreLSQL和查看相同的数据。

数据库URL jdbc:h2:mem:dataSource表示您正在使用内存数据库。 现在,如果启动第二个Java进程并连接到此数据库,则最终将拥有两个内存数据库(每个进程一个)。

如果你想连接到现有的数据库,你有多个选项:

  • 从同一个进程中连接到数据库。 不要开始第二个过程。

  • 使用持久化的数据库,并使用硬编码的绝对path,例如:`jdbc:h2:/ data / db / dataSource'。

  • 更复杂/不推荐:如果启动第二个进程,理论上可以使用服务器模式连接到内存数据库。 但是这意味着您需要启动运行testing的服务器。

使用Spring Boot时,可以按如下方式注册H2 Console Servlet:

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

如果您希望控制台远程可用,重要的一行是addInitParameter ,将"webAllowOthers"设置为"true"

使用spring引导,你可以用application.properties文件中的几个configuration来做到这一点。

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

然后,您可以访问http:// localhost:8080 / console /中的 h2 Web控制台。 默认的loginconfiguration应该工作,除非你改变它们。

请参阅spring启动文档 。

当您使用带有xml jdbcconfiguration的embeddeb时,数据库的默认名称是'testdb'

尝试在你的url连接中使用:

 jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1 

对于那些想要进行Java Config安装的人来说,在实现ServletContextInitializer和链接控制台服务器的时候,初始化TCP服务器是相当容易的。

 @Configuration public class WebConfig implements ServletContextInitializer{ ... @Override public void onStartup( ServletContext servletContext ) //do stuff onStartUp... initH2TCPServer( servletContext ); .... @Bean(initMethod="start", destroyMethod="stop") public Server initH2TCPServer(ServletContext servletContext) { log.debug( "Initializing H2 TCP Server" ); try { server = Server.createTcpServer( "-tcp", "-tcpAllowOthers", "-tcpPort", "9092" ); } catch( SQLException e ) { e.printStackTrace(); } finally { //Always return the H2Console... initH2Console( servletContext ); } return server; } public void initH2Console( ServletContext servletContext ) { log.debug( "Initializing H2 console" ); ServletRegistration.Dynamic h2ConsoleServlet = servletContext.addServlet( "H2Console", new org.h2.server.web.WebServlet() ); h2ConsoleServlet.addMapping( "/console/*" ); ); }