Aar*_*ski 100 java spring jpa h2 spring-boot
我只是试图查看嵌入式H2数据库的H2数据库内容,当我在application.properties中没有指定任何内容并以mvn spring:run开头时,spring-boot会创建.我可以看到hibernate JPA创建表但是如果我尝试在下面的URL访问h2控制台,则数据库没有表.
http://localhost:8080/console/
Run Code Online (Sandbox Code Playgroud)
我看到这样的建议: 查看Spring启动的嵌入式H2数据库的内容
但是我不知道在spring-boot中把建议的XML放在哪里,即使我这样做了,我也不希望在配置外部数据库时h2console再次可用,所以我更有可能需要处理这个使用某种条件代码(或者只是允许弹簧在最理想的情况下自动处理它,在激活maven配置文件时我只包含H2).
有没有人有一些示例代码显示如何让H2控制台在启动时工作(以及找出spring正在使用的jdbc连接字符串是什么方式)?
Aar*_*ski 103
这就是我用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
Run Code Online (Sandbox Code Playgroud)
在Application.java(或某些配置)中:
@Bean
public ServletRegistrationBean h2servletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
registration.addUrlMappings("/console/*");
return registration;
}
Run Code Online (Sandbox Code Playgroud)
然后您可以访问{server}/console /上的H2控制台.输入此URL作为JDBC URL:jdbc:h2:mem:AZ
geo*_*and 52
从Spring Boot开始1.3.0.M3,H2控制台可以自动配置.
先决条件是:
即使您不使用Spring Boot Dev Tools,您仍然可以通过设置spring.h2.console.enabled为自动配置控制台true
查看文档的这一部分以获取所有详细信息.
请注意,以这种方式配置时,可以从以下位置访问控制台:http:// localhost:8080/h2-console /
Krz*_*zor 45
我找到了一个关于这个主题的好教程:
https://springframework.guru/using-the-h2-database-console-in-spring-boot-with-spring-security/
基本上我正确的JDBC URL是: jdbc:h2:mem:testdb
man*_*ni0 20
来自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.
Run Code Online (Sandbox Code Playgroud)
将上面两行添加到我的application.properties文件就足以使用默认用户名(sa)和密码(空)访问H2数据库Web控制台.
bin*_*iam 20
与Step by Step指南类似的答案.
pom.xml或build.gradleMaven的
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
摇篮
dependencies {
compile("org.springframework.boot:spring-boot-devtools")
}
Run Code Online (Sandbox Code Playgroud)
http://localhost:8080/h2-console/jdbc:h2:mem:testdb为JDBC URLrak*_*k22 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
Run Code Online (Sandbox Code Playgroud)
Wit*_*rba 15
对于直接来自Spring Initialzr的Spring Boot 2.1.1:
devtools的默认值为http://127.0.0.1:8080/h2-console/
没有devtools-您需要在属性中进行设置:spring.h2.console.enabled=true spring.h2.console.path=/h2-console
到达那里后-设置JDBC URL:jdbc:h2:mem:testdb(默认设置不起作用)
如果您使用Spring Boot的开发人员工具,则默认启用H2控制台.它可以从/h2-console/ 访问.在登录界面上,输入JDBC URL使用值jdbc:h2:mem:testdb.注意mem字符串.
如果你不使用Spring Boot的开发者工具,可以使控制台application.properties使用spring.h2.console.enabled=true.这将启用控制台/h2-console.如果要更改URL,则可以添加其他条目spring.h2.console.path=my_console_path.
默认架构名称是testdb.
Spring Boot Documentation中的更多细节.
检查spring application.properties
spring.datasource.url = jdbc:h2:mem:testdb; DB_CLOSE_DELAY = -1; DB_CLOSE_ON_EXIT = FALSE
这里的testdb是数据库定义的确保其他连接时h2控制台具有相同的值,否则它将连接到默认数据库
为了获取表,您需要做的就是创建2个sql文件schema.sql(用于创建表)和data.sql(用于创建表的数据)。这些文件将放在src / main / resources文件夹中。Spring Boot自动检测到它们,并在运行时负责其余的工作。
如果您在项目中使用两个以上的数据库,请确保使用特定的文件,例如(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
Run Code Online (Sandbox Code Playgroud)
您可以按照以下链接中的步骤进行操作。
https://springframework.guru/using-the-h2-database-console-in-spring-boot-with-spring-security/
小智 5
对于 Spring Boot 2.3.3.RELEASE 直接来自 Spring Initialzr:
POM:数据 jpa、h2、web
应用特性: 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'
Run Code Online (Sandbox Code Playgroud)
现在将上述 JDBC URL 用于 h2-console 并单击Connect。
| 归档时间: |
|
| 查看次数: |
188355 次 |
| 最近记录: |