如何将H2控制台连接到嵌入式Spring H2 DB [英] How to connect H2 console to embedded Spring H2 DB

查看:342
本文介绍了如何将H2控制台连接到嵌入式Spring H2 DB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在开发简单的应用程序,它有Spring Ebedded H2数据库用于开发。 database.xml bean conf如下所示:

Ok, Im developing simple app, which has Spring Ebedded H2 database for development. database.xml bean conf looks like this:

<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>

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

H2数据库正在初始化,我的应用程序正在运行,我正在创建实体,它们存储在H2 db中当Tomcat启动时(我知道它因为我使用并检索它们)。但是,当我查看H2控制台时,我的实体表不存在。

H2 database is initializing, My app is working, Im creating entities, and they are stored in H2 db when Tomcat is launched (I know it because I use and retrieve them). However, when I look at H2 console, my Entity tables are not present.

我想H2控制台指向另一个H2数据库,而Spring Embedded H2 Db与之无关H2控制台。

I guess H2 console points on another H2 database, and Spring Embedded H2 Db is not related with that H2 console.

如何修复?

编辑:我可以通过输入 http:/来访问H2控制台/ localhost:8082 在我的网络浏览器中。

Im getting access to H2 console by typing http://localhost:8082 in my web browser.

推荐答案

如果您的应用程序不是弹簧启动,则需要在web.xml文件中添加以下servlet配置

if your application is not spring boot than you need to add below servlet configuration in web.xml file

!-- H2 Database Console for managing the app's database -->
<servlet>
    <servlet-name>H2Console</servlet-name>
    <servlet-class>org.h2.server.web.WebServlet</servlet-class>
    <init-param>
        <param-name>-webAllowOthers</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>H2Console</servlet-name>
    <url-pattern>/admin/h2/*</url-pattern>
</servlet-mapping>

<!-- Handles requests into the application -->

请查看更多详情 https://github.com/spring-projects/greenhouse/blob/master/src/main/webapp/ WEB-INF / web.xml

如果您的应用程序是基于弹簧启动的,则必须遵循 https://springframework.guru/using-the-h2-database-console-in -spring-boot-with-spring-security /

and if your application is spring boot based than you have to follow https://springframework.guru/using-the-h2-database-console-in-spring-boot-with-spring-security/

这篇关于如何将H2控制台连接到嵌入式Spring H2 DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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