spring boot默认H2 jdbc连接(和H2控制台)

时间:2014-07-09 13:57:18

标签: java spring jpa h2 spring-boot

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

http://localhost:8080/console/

我看到像这样的建议: View content of embedded H2 database started by Spring

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

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

14 个答案:

答案 0 :(得分:94)

这就是我使用H2在spring-boot中运行H2控制台的方法。我不确定这是否正确,但由于没有其他人提供解决方案,所以我建议这是最好的方法。

就我而言,我为数据库选择了一个特定的名称,这样我就可以在启动H2控制台时输入一些东西(在这种情况下," AZ")。我认为所有这些都是必需的,虽然看起来好像没有弹出.jpa.database-platform不会伤害任何东西。

在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(或某些配置)中:

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

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

答案 1 :(得分:45)

从Spring Boot 1.3.0.M3开始,H2控制台可以自动配置。

先决条件是:

  • 您正在开发一个网络应用
  • 启用Spring Boot开发工具
  • H2在类路径

即使您没有使用Spring Boot Dev Tools,您仍然可以通过将spring.h2.console.enabled设置为true

来自动配置控制台

查看文档的this部分了解所有详细信息。

请注意,以这种方式配置时,可以通过以下网址访问控制台:http://localhost:8080/h2-console/

答案 2 :(得分:43)

我找到了一个关于这个主题的好教程:

https://springframework.guru/using-the-h2-database-console-in-spring-boot-with-spring-security/

基本上,我的正确JDBC URL是:jdbc:h2:mem:testdb

答案 3 :(得分:19)

分步指南的类似答案。

  1. 开发人员工具依赖关系添加到pom.xmlbuild.gradle
  2. <强>的Maven

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
    

    <强>摇篮

    dependencies {
        compile("org.springframework.boot:spring-boot-devtools")
    }
    
    1. http://localhost:8080/h2-console/
    2. 访问数据库
    3. jdbc:h2:mem:testdb指定为JDBC URL
    4. 您应该将项目中指定的实体视为表格。

答案 4 :(得分:17)

来自http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

H2 Web控制台(H2ConsoleProperties):

spring.h2.console.enabled=true //Enable the console.
spring.h2.console.path=/h2-console //Path at which the console will be available.

将上述两行添加到我的application.properties文件中,足以使用默认用户名(sa)和密码(空)访问H2数据库Web控制台。

答案 5 :(得分:16)

我在/resources/application.properties中只有以下属性。运行spring boot后,使用此URL(http://localhost:8080/h2-console/),H2控制台中的表可见并读取以查看表数据,您也可以运行简单的SQL命令。有一件事,在你的java代码中,在获取数据时,列名是大写的,即使schema.sql使用的是小写名称:)

spring.datasource.initialize=true
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=- 1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true

答案 6 :(得分:8)

如果您使用的是Spring Boot的开发人员工具,则默认情况下会启用H2控制台。可以从/h2-console /访问它。在登录界面上,输入JDBC URL使用值jdbc:h2:mem:testdb。请注意mem字符串。

如果您不使用Spring Boot的开发者工具,则可以使用application.propertiesspring.h2.console.enabled=true中启用控制台。这将在/h2-console下启用控制台。如果您想更改网址,则可以使用spring.h2.console.path=my_console_path添加其他条目。

默认架构名称为testdb

Spring Boot Documentation中的更多详情。

答案 7 :(得分:7)

直接从Spring Initialzr获得的 Spring Boot 2.1.1

  1. 带有 devtools 的默认值为http://127.0.0.1:8080/h2-console/

    • POM:spring-boot-starter,h2,spring-boot-starter-web, spring-boot-devtools
  2. 没有devtools -您需要在以下属性中进行设置:spring.h2.console.enabled=true spring.h2.console.path=/h2-console

    • POM:spring-boot-starter,h2,spring-boot-starter-web

到达那里后-设置JDBC URL:jdbc:h2:mem:testdb(默认设置不起作用)

答案 8 :(得分:4)

为了获取表,您需要做的就是创建2个sql文件schema.sql(用于表创建)和data.sql(用于创建表的数据)。这些文件放在src / main / resources文件夹中。 Spring启动会自动检测它们并在运行期间处理其余部分。

如果您在项目中使用2个以上的数据库,请确保使用特定文件,例如(schema-h2.sql - 用于h2 DB,schema-oracle.sql - 用于oracle DB)。 data.sql也应该遵循相同的原则。

还要确保通过在schema.sql中添加drop table语句作为第一个语句来删除表。避免附加重复记录。

弹簧靴的链接在这里。

我的application.properties如下。

spring.datasource.url=jdbc:h2:~/file/Shiva;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.platform=h2
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.datasource.initialize=true 
spring.error.whitelabel.enabled=true
spring.h2.console.path=/console
spring.jpa.hibernate.ddl-auto=none
spring.datasource.continue-on-error=true
spring.jpa.hibernate.ddl-auto=create
spring.hibernate.hbm2ddl.auto=update
spring.hibernate.show_sql=true

您可以按照以下链接中的步骤操作。

https://springframework.guru/using-the-h2-database-console-in-spring-boot-with-spring-security/

答案 9 :(得分:4)

检查spring application.properties

  

spring.datasource.url = jdbc:h2:mem:testdb; DB_CLOSE_DELAY = -1; DB_CLOSE_ON_EXIT = FALSE

这里testdb是数据库定义的 确保h2控制台具有相同的值,而以其他方式连接时它将连接到默认数据库

enter image description here

答案 10 :(得分:3)

登录H2控制台时,请使用jdbc:h2:mem:testdb作为路径。

很显然,如果您更改了Spring Boot属性,则数据源可能会有所不同,但似乎您正在努力寻找默认值。这里的所有都是它的!登录到H2后,您将看到您的架构。

答案 11 :(得分:3)

对于Spring Boot 2.3.3,直接从Spring Initialzr释放:

POM:数据jpa,h2,网络

应用程序属性:spring.h2.console.enabled=true

在运行应用程序时,请在运行控制台中查找以下行:

2020-08-18 21:12:32.664  INFO 63256 --- [           main] o.s.b.a.h2.H2ConsoleAutoConfiguration    : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:eaa9d6da-aa2e-4ad3-9e5b-2b60eb2fcbc5'

现在将上述JDBC URL用于h2-console并单击Connect

答案 12 :(得分:2)

我发现使用spring boot 2.0.2.RELEASE,在POM文件中配置spring-boot-starter-data-jpa和com.h2database不仅足以使H2控制台正常工作。您必须如下配置spring-boot-devtools。 您可以选择遵循Aaron Zeckoski在此帖中的说明

  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
 </dependency>

答案 13 :(得分:1)

当我遇到同样的问题时,我犯了一个非常愚蠢的错误。我添加了H2 DB来运行单元测试用例,因此在scope中将test设置为pom.xml。使用mvn spring:run运行应用程序时,我删除了scope,现在可以正常使用了。

相关问题