简体   繁体   English

可视化嵌入式 H2 数据库

[英]Visualizing an embedded H2 database

I'm trying to configure and use a H2 embedded DB with Spring Boot.我正在尝试通过 Spring Boot 配置和使用 H2 嵌入式数据库。 I'm using the code provided in the doc :我正在使用文档中提供的代码:

@Bean
public DataSource dataSource() {
    return new EmbeddedDatabase db = new EmbeddedDatabaseBuilder()
    .generateUniqueName(true)
    .setType(H2)
    .setScriptEncoding("UTF-8")
    .ignoreFailedDrops(true)
    // .addScript("schema.sql") Omit, auto-generate
    .build();
}

The application works fine, I can create and retrieve data, but I would like to visually see it.该应用程序运行良好,我可以创建和检索数据,但我想直观地看到它。 I've installed the H2 Console Application , but I'm not sure how to connect to the in-memory instance.我已经安装了H2 控制台应用程序,但我不确定如何连接到内存中的实例。 I go to http://localhost:8082 and then, under JDBC URL I input when I get from the connection's metadata (I've tried with auto-generated and manually specified names), but the H2 Console Application seems to be connected to an empty schema.我转到http://localhost:8082然后,在我从连接的元数据中获取时输入的 JDBC URL 下(我尝试使用自动生成和手动指定的名称),但 H2 控制台应用程序似乎已连接到一个空的架构。 My tables don't appear there, only the information schema, and I can't SELECT from my tables either, they just don't exist here.我的表没有出现在那里,只有信息模式,我也不能从我的表中SELECT ,它们只是不存在在这里。

What's going on, what DB am I connecting to?发生了什么,我要连接到哪个数据库? How can I connect to my embedded DB?如何连接到我的嵌入式数据库?

If you want to see your tables on h2 console in your application, you don't need to install "Console Application". 如果要在应用程序的h2控制台上查看表,则无需安装“控制台应用程序”。 You just need to enable to see visually and also set your datasource url in application.properties . 您只需要启用视觉效果即可,还可以在application.properties设置数据源URL。 It seems like that: 看起来像这样:

spring.datasource.url=jdbc:h2:mem:nprensen;(or if you have already a ds you should write that url)
spring.h2.console.enabled=true
spring.h2.console.path=/console  // this is the path for h2 console:localhost:8080/console

Have you provided the following dependency in your project?您是否在项目中提供了以下依赖项?

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM