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

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

问题描述

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

  http:// localhost:8080 / console / 

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



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

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

解决方案

这就是我在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。密码=
spring.jpa.database-platform = org.hibernate.dialect.H2Dialect

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

  @Bean 
public ServletRegistrationBean h2servletRegistration(){
ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
registration.addUrlMappings(/ console / *);
退货登记;
}

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


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/

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

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).

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)?

解决方案

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.

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.

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

In Application.java (or some configuration):

@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

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

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