【问题标题】:How to access in-memory h2 database from Intellij IDEA如何从 Intellij IDEA 访问内存中的 h2 数据库
【发布时间】:2019-02-22 15:10:26
【问题描述】:

在 Spring Boot 项目中,我试图从我的 IDE 中查看内存表。

如何从 Intellij IDEA 访问内存中的 h2 数据库。

这是我的 application.yml 中的一个 sn-p:

 h2:
    datasource:
        url: jdbc:h2:mem:mydb
        username: username
        password: 123
        driver-class-name: org.h2.Driver
        init-sql: h2.sql
    console:
      enabled: true
      path: /search/console
      settings:
        trace: false
        web-allow-others: false

Intellij 没有输入内存数据库用户名的字段: 测试连接显示成功,但是它没有看到来自 h2.sql 的表。 我可以使用 h2 控制台访问它们。

【问题讨论】:

标签: java spring-boot intellij-idea h2


【解决方案1】:

默认情况下,IntellJ 不显示任何数据库。

  1. right click datasource 然后选择 Properties

  1. Schemas 选项卡上,您会看到options 的列表以选择应该显示哪个database(我通常选择All databases)。选择您需要显示的database

结果:

【讨论】:

    【解决方案2】:

    请记住,这可能会向您显示数据库而不是表格,因为它们将仅在 h2 控制台中可见。为了让您通过 IntelliJ 访问它们,您可能需要将 url 和连接更改为 文件类型而不是内存中

    所以不要这样:

    #spring.datasource.url=jdbc:h2:mem:testdb
    

    你会有这样的东西:

    spring.datasource.url=jdbc:h2:file:~/Users/yourUser/IdeaProjects/resume-portal/src/main/resources/data/resume-portal;MV_STORE=false;AUTO_SERVER=TRUE
    

    然后在从 url 创建数据源之后

    您现在应该可以看到数据库及其表。

    如果您使用新的 url 连接,控制台仍然可以使用它。

    PS:如果更喜欢使用相对路径,您可以将 url 更改为类似于jdbc:h2:file:./src/main/resources/data/resume-portal;MV_STORE=false;AUTO_SERVER=TRUE

    【讨论】:

      猜你喜欢
      • 2019-04-07
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-22
      • 2011-07-01
      • 1970-01-01
      • 2015-01-21
      相关资源
      最近更新 更多