spring 查看Spring启动的嵌入式H2数据库内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17803718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
View content of embedded H2 database started by Spring
提问by tduchateau
I would like to view in a web browser the content of the H2 database started by Spring thanks to the following configuration:
由于以下配置,我想在Web浏览器中查看Spring启动的H2数据库的内容:
<jdbc:embedded-database id="dataSource" type="H2" />
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="classpath:db/populateDB.sql"/>
</jdbc:initialize-database>
I searched for the JDBC URL in the logs:
我在日志中搜索了 JDBC URL:
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:
这样我就可以按如下方式填写连接表格:


But unfortunately, the db is still empty, whereas it shouldn't due to the populateDB.sql script.
但不幸的是,db 仍然是空的,而由于 populateDB.sql 脚本,它不应该是空的。
Any idea?
任何的想法?
Thanks!
谢谢!
采纳答案by hshib
Pretty much the same question as View content of H2 or HSQLDB in-memory database.
与View content of H2 or HSQLDB in-memory database几乎相同的问题。
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>
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.
这将在与嵌入式数据库相同的 JVM 中启动 H2 Web 控制台和 TCP 服务器,以便您可以使用 Web 浏览器访问端口 8082(输入 jdbc:h2:mem:dataSource 作为 URL),或使用外部 SQL 客户端访问端口 9092如 SQuirreLSQL 并查看相同的数据。
回答by chAmi
With spring boot you can do this with couple of configurations in the application.properties file.
使用 spring boot,您可以通过 application.properties 文件中的几个配置来做到这一点。
spring.h2.console.enabled=true
spring.h2.console.path=/console/
Then you can access h2 web console in http://localhost:8080/console/. Default login configuration should work unless you change them.
然后你可以在http://localhost:8080/console/ 中访问 h2 web 控制台。除非您更改它们,否则默认登录配置应该可以工作。
See spring boot documentation.
请参阅 spring 引导文档。
回答by Thomas Mueller
The database URL jdbc:h2:mem:dataSourcemeans you are using an in-memory database. Now if you start a second Java process and connect to this database, you will end up having two in-memory databases (one for each process).
数据库 URLjdbc:h2:mem:dataSource表示您使用的是内存数据库。现在,如果您启动第二个 Java 进程并连接到该数据库,您将最终拥有两个内存数据库(每个进程一个)。
If you want to connect to the existing database, you have multiple options:
如果要连接到现有数据库,您有多种选择:
Connect to the database from within the same process. Don't start a second process.
Use a persisted database, with a hardcoded absolute path, for example: `jdbc:h2:/data/db/dataSource'.
More complicated / not recommended: If you start a second process, you could theoretically connect to an in-memory database using the server mode. But that means you need to start the server where you ran the test.
从同一进程中连接到数据库。不要开始第二个过程。
使用具有硬编码绝对路径的持久化数据库,例如:`jdbc:h2:/data/db/dataSource'。
更复杂/不推荐:如果您启动第二个进程,理论上您可以使用服务器模式连接到内存数据库。但这意味着您需要启动运行测试的服务器。
回答by user2754985
When using Spring Boot you can register the H2 Console Servlet as follows:
使用 Spring Boot 时,您可以按如下方式注册 H2 控制台 Servlet:
@Bean
public ServletRegistrationBean h2servletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
registration.addUrlMappings("/console/*");
registration.addInitParameter("webAllowOthers", "true");
return registration;
}
If you want the console to be available remotely, the important line is the addInitParameterto set the "webAllowOthers"to "true".
如果您希望控制台提供远程,重要的线是addInitParameter设置"webAllowOthers"到"true"。
回答by F. Geraerts
When you use the an embeddeb with the xml jdbc configuration the default name of the database is 'testdb'
当您使用带有 xml jdbc 配置的 embeddeb 时,数据库的默认名称是“testdb”
Try to use in your url connection:
尝试在您的 url 连接中使用:
jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
回答by Eddie B
For those wanting a Java Config setup it's fairly easy to do as well initializing the TCP server when implementing ServletContextInitializer and chaining the Console Server...
对于那些想要 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/*" );
);
}
回答by Gunjan Shah
I was facing similar issue. But the fix was really very small. Please refer page : https://springframework.guru/using-the-h2-database-console-in-spring-boot-with-spring-security/for more details.
我面临着类似的问题。但修复真的很小。请参阅页面:https: //springframework.guru/using-the-h2-database-console-in-spring-boot-with-spring-security/了解更多详情。
In my case, I have added scope of H2 dependency as "runtime". I removed it and it fixed my issue. Not I am able to see tables in H2-console.
就我而言,我已将 H2 依赖项的范围添加为“运行时”。我删除了它,它解决了我的问题。不是我能够在 H2 控制台中看到表格。
Previous dependency in my pom was :
我的 pom 中以前的依赖项是:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
And new dependency which fixed my issue :
以及解决了我的问题的新依赖项:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

