查看Spring启动的嵌入式H2数据库的内容 [英] View content of embedded H2 database started by Spring

查看:276
本文介绍了查看Spring启动的嵌入式H2数据库的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下配置,我想在Web浏览器中查看Spring启动的H2数据库的内容:

I would like to view in a web browser the content of the H2 database started by Spring thanks to the following configuration:

<jdbc:embedded-database id="dataSource" type="H2" />

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

我在日志中搜索了JDBC URL:

I searched for the JDBC URL in the logs:

DEBUG o.s.j.d.SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem:dataSource;DB_CLOSE_DELAY=-1]

这样我就可以如下填写连接表格:

So that I could fill the connection form as follows:

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

But unfortunately, the db is still empty, whereas it shouldn't due to the populateDB.sql script.

有什么主意吗?

谢谢!

推荐答案

只需将以下内容添加到您的配置中.

Simply add the following to your 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>

这将在与嵌入式数据库相同的JVM中启动H2 Web控制台和TCP服务器,以便您可以使用Web浏览器访问端口8082(将URL输入jdbc:h2:mem:dataSource),或使用以下命令访问端口9092外部SQL客户端(例如SQuirreLSQL)并查看相同的数据.

This will start both H2 web console and TCP server in the same JVM as your embedded database so that you can access port 8082 with your web browser (enter jdbc:h2:mem:dataSource as URL), or access port 9092 with external SQL client such as SQuirreLSQL and view the same data.

这篇关于查看Spring启动的嵌入式H2数据库的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆