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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24655684/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:50:40  来源:igfitidea点击:

spring boot default H2 jdbc connection (and H2 console)

javaspringjpah2spring-boot

提问by Aaron Zeckoski

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.

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

http://localhost:8080/console/

I see suggestions like this one: View content of embedded H2 database started by Spring

我看到这样的建议: 查看由 Spring 启动的嵌入式 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).

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

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 控制台在启动时工作(以及找出 spring 使用的 jdbc 连接字符串的方法)?

采纳答案by Aaron Zeckoski

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 控制台在带有 H2 的 spring-boot 中工作的方式。我不确定这是否正确,但由于没有其他人提供解决方案,因此我建议这是最好的方法。

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.

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

In application.properties:

在 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

In Application.java (or some configuration):

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

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

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

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

回答by Krzysztof Kaczor

I have found a nice tutorial about this topic:

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

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

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

Basically the correct JDBC URL for me was: jdbc:h2:mem:testdb

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

回答by geoand

As of Spring Boot 1.3.0.M3, the H2 console can be auto-configured.

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

The prerequisites are:

先决条件是:

  • You are developing a web app
  • Spring Boot Dev Tools are enabled
  • H2 is on the classpath
  • 你正在开发一个网络应用程序
  • Spring Boot 开发工具已启用
  • H2 在类路径上

Even if you don't use Spring Boot Dev Tools, you can still auto-configure the console by setting spring.h2.console.enabledto true

即使您不使用 Spring Boot Dev Tools,您仍然可以通过设置spring.h2.console.enabled为自动配置控制台true

Check out thispart of the documentation for all the details.

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

Note that when configuring in this way the console is accessible at: http://localhost:8080/h2-console/

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

回答by mancini0

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

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

H2 Web Console (H2ConsoleProperties):

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.

Adding the above two lines to my application.properties file was enough to access the H2 database web console, using the default username (sa) and password (empty, as in don't enter a password when the ui prompts you).

将以上两行添加到我的 application.properties 文件中就足以访问 H2 数据库 web 控制台,使用默认用户名 (sa) 和密码(空,因为在 ui 提示时不要输入密码)。

回答by rak22

I had only below properties in /resources/application.properties. After running spring boot, using this URL(http://localhost:8080/h2-console/), the table in H2 console was visible and read to view the table data, also you can run simple SQL commands. One thing, in your java code, while fetching data, the column names are upper-case, even though schema.sql is using lower-case names :)

我在 /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

回答by biniam

A similar answer with Step by Step guide.

与逐步指南类似的答案。

  1. Add Developer toolsdependency to your pom.xmlor build.gradle
  1. 开发人员工具依赖项添加到您的pom.xmlbuild.gradle

Maven

马文

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

Gradle

摇篮

dependencies {
    compile("org.springframework.boot:spring-boot-devtools")
}
  1. Access the db from http://localhost:8080/h2-console/
  2. Specify jdbc:h2:mem:testdbas JDBC URL
  3. You should see the entity you specified in your project as a table.
  1. 从访问数据库 http://localhost:8080/h2-console/
  2. 指定jdbc:h2:mem:testdb为 JDBC URL
  3. 您应该会看到您在项目中指定的实体作为表格。

回答by kemparaj565

In order to get the tables all you need to do is create 2 sql files schema.sql(for table creation) and data.sql(data for the created tables). These files to be put in src/main/resources folder. Spring boot auto detects them and takes care of the rest during runtime.

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

If your using more than 2 DB in your project ensure to use specific files like (schema-h2.sql -- for h2 DB , schema-oracle.sql -- for oracle DB). The same to be followed for data.sql too.

如果您在项目中使用 2 个以上的 DB,请确保使用特定文件,例如 (schema-h2.sql -- for h2 DB,schema-oracle.sql -- for oracle DB)。data.sql 也是如此。

Also ensure that you drop tables by adding drop table statement in your schema.sql as first statement. To avoid appending of duplicate records.

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

The link for spring boot is here.

Spring Boot 的链接在这里。

My application.properties is as follows.

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

You can follow the steps in the below link.

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

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

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

回答by georger

If you use Spring Boot's developer tools, it comes with H2 Console enabled by default. It can be accessed from /h2-console/. On the login interface, for input JDBC URLuse value jdbc:h2:mem:testdb. Pay attention to memstring.

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

If you don't use Spring Boot's developer tools, you can enable the console in application.propertiesusing spring.h2.console.enabled=true. This will enable console under /h2-console. If you want to change the URL then you can add another entry with spring.h2.console.path=my_console_path.

如果你不使用 Spring Boot 的开发者工具,你可以在application.properties使用spring.h2.console.enabled=true. 这将在/h2-console. 如果要更改 URL,则可以添加另一个条目spring.h2.console.path=my_console_path

The default schema name is testdb.

默认架构名称是testdb.

More details in Spring Boot Documentation.

Spring Boot 文档中的更多详细信息。

回答by Pragnesh Rana

I found that with spring boot 2.0.2.RELEASE, configuring spring-boot-starter-data-jpa and com.h2database in the POM file is not just enough to have H2 console working. You must configure spring-boot-devtools as below. Optionally you could follow the instruction from Aaron Zeckoski in this post

我发现使用 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>

回答by Pragnesh Rana

Use jdbc:h2:mem:testdb as your path when logging into the H2 console.

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

Obviously if you have altered Spring Boot properties your datasource may be different, but it seems like you're struggling with how to find the default. That's all there is to it! You'll see your schema after logging in to H2.

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