Spring Boot 默认 H2 jdbc 连接(和 H2 控制台) [英] Spring Boot default H2 jdbc connection (and H2 console)

查看:72
本文介绍了Spring Boot 默认 H2 jdbc 连接(和 H2 控制台)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想查看嵌入式 H2 数据库的 H2 数据库内容,当我没有在 application.properties 中指定任何内容并以 mvn spring:run 开头时,spring-boot 创建该数据库.我可以看到 hibernate JPA 创建了表,但是如果我尝试通过以下 URL 访问 h2 控制台,则数据库没有表.

I am simply trying to see the H2 database content for an embedded H2 database which spring-boot creates when I don't specify anything in my application.properties and start with mvn spring:run. I can see hibernate JPA creating the tables but if I try to access the h2 console at the URL below the database has no tables.

http://localhost:8080/console/

我看到这样的建议:查看Spring启动的嵌入式H2数据库内容

但我不知道在 spring-boot 中将建议的 XML 放在哪里,即使我这样做了,我也不希望在配置外部数据库时 h2console 不再可用我更有可能需要使用某种条件代码来处理这个问题(或者可能只是在最理想的情况下让 spring 自动处理它,我只在激活 maven 配置文件时包含 H2).

But I don't know where to put the suggested XML in spring-boot and even if I did, I don't want the h2console to be available anymore when an external database is configured so it is more likely that I need to handle this with some kind of conditional code (or maybe just allow spring to automatically handle it in the most ideal case where I only include H2 when a maven profile is activated).

有没有人有一些示例代码显示如何让 H2 控制台在启动时工作(以及找出 spring 使用的 jdbc 连接字符串的方法)?

Does anyone have some sample code showing how to get the H2 console working in boot (and also the way to find out what the jdbc connection string that spring is using is)?

推荐答案

这就是我如何让 H2 控制台在带有 H2 的 spring-boot 中工作.我不确定这是否正确,但由于没有其他人提供解决方案,因此我建议这是最好的方法.

This is how I got the H2 console working in spring-boot with H2. I am not sure if this is right but since no one else has offered a solution then I am going to suggest this is the best way to do it.

就我而言,我为数据库选择了一个特定名称,以便在启动 H2 控制台时可以输入一些内容(在本例中为AZ").我认为所有这些都是必需的,尽管省略 spring.jpa.database-platform 似乎没有任何伤害.

In my case, I chose a specific name for the database so that I would have something to enter when starting the H2 console (in this case, "AZ"). I think all of these are required though it seems like leaving out the spring.jpa.database-platform does not hurt anything.

在 application.properties 中:

In application.properties:

spring.datasource.url=jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

在 Application.java(或一些配置)中:

In Application.java (or some configuration):

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

然后您可以在 {server}/console/访问 H2 控制台.输入它作为 JDBC URL:jdbc:h2:mem:AZ

Then you can access the H2 console at {server}/console/. Enter this as the JDBC URL: jdbc:h2:mem:AZ

这篇关于Spring Boot 默认 H2 jdbc 连接(和 H2 控制台)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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