Spring 引导 + IntelliJ + 嵌入式数据库 = 头痛

Spring Boot + IntelliJ + Embedded Database = Headache

要么我遗漏了一些深藏在某些文档中的核心概念(Spring、Spring Boot、H2、HSQLDB、Derby、IntelliJ),要么我一直盯着这个看长。

我有一个 Spring 启动项目。一开始尝试使用和初始化 H2 数据库,尝试在 IntelliJ 中连接它,结果发现我可能无法在不放弃长子的情况下轻松浏览数据库 (Connect to H2 database using IntelliJ database client)。

所以我转向了 DerbyDB。同样的事情 - db 根文件夹是在我的应用程序中创建的,我在 IntelliJ 中连接到它,但是我的表是从启动应用程序时创建的,无法浏览。

我什至尝试过 SQLite,但对 SQLite 的支持不是很好,某些更新功能不可用,但我至少可以在 IntelliJ 浏览器中找到我的表!

我只想要一个简单的单文件嵌入式数据库,我可以轻松使用、浏览和玩耍。有什么建议吗?!

当我 运行 应用程序时,我看到模式已导出:

2015-07-19 09:37:45.836  INFO 98608 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
Hibernate: drop table user_roles if exists
Hibernate: drop table users if exists
Hibernate: create table user_roles (id bigint generated by default as identity, role_name varchar(255), version bigint, user_id bigint, primary key (id))
Hibernate: create table users (id bigint generated by default as identity, email varchar(255), password varchar(255), username varchar(255), version bigint, primary key (id))
Hibernate: alter table user_roles add constraint FK_g1uebn6mqk9qiaw45vnacmyo2 foreign key (user_id) references users
2015-07-19 09:37:45.849  INFO 98608 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete

在 IntelliJ 中,什么都没有(根据 heenenee 的建议使用 jdbc:h2:./test;AUTO_SERVER=TRUE 的远程源):

我看到一些要关闭的投票,因为我不清楚我在问什么:

如何使用 H2、HSQLDB 或 Derby 数据库开发应用程序并使用 IntelliJ 连接到它们?

H2 Automatic Mixed Mode 应该适合你。使用 jdbc:h2:~/mydbInHomeDir;AUTO_SERVER=TRUE 作为你的 spring.datasource.url。在 IntelliJ 中,创建一个 remote H2 数据源并使用完全相同的 JDBC URL。您可能必须明确按下 IntelliJ 数据库中的“同步”按钮 window 才能显示数据库表。

如果您按照本文中的步骤操作:https://techdev.io/en/developer-blog/querying-the-embedded-h2-database-of-a-spring-boot-application

我认为它会帮助获取 Spring 启动应用程序,其中包含通过 tcp 服务器公开的 H2 内存数据库,以便您可以使用 IntelliJ 数据库客户端连接到它。

我有类似的问题。这是由于休眠的默认 create-drop ddl 策略。 在应用程序关闭休眠后使用此策略会在会话结束时破坏架构,这就是 IntelliJ 不显示任何内容的原因。将 ddl 策略更改为 create,hibernate 将在下次应用程序启动时创建模式并销毁以前的数据。

这是我的配置示例:

application.yml

spring:
  datasource.url: jdbc:h2:./db/testDb
  jpa.hibernate.ddl-auto: create

IntelliJ 数据库属性

结果

添加到上面提到的heenenee。如果您不指定 AUTO_SERVER,则只允许一个连接到您的 H2 实例。

我正在使用 spring-boot 和 spring-data-jpa。确保为代表每个 table(s).

的实体声明了 @Entity

以下是我的application.yml/application.properties

spring.datasource.url: 
jdbc:h2:file:/Users/blah[![enter image description here][1]][1]/db/vlad4;AUTO_SERVER=TRUE
spring.datasource.username: sa
spring.datasource.password:

spring:
  jpa:
    hibernate:
      ddl-auto: create #will create schema based on entities
    show-sql: true

启动您的应用程序并将一些数据导入其中。 Spring 如果类路径中有 import.sql,启动将自动导入您的数据 例如:/src/main/resources/import.sql

像这样配置你的 IntelliJ

如果您不使用 IntelliJ,请下载 server/client combo @ http://www.h2database.com/html/download.html 提取它并启动 browser-based 客户端使用:

h2/bin: java -cp h2*.jar org.h2.tools.Server

通过指定连接字符串连接到您的嵌入式数据库:

使用此处的示例通过控制台和 TCP 公开 in-memory 数据库,我能够根据屏幕截图使用 H2 控制台和 IntelliJ 客户端进行连接。

使用 IntelliJ 连接 - jdbc:h2:tcp://localhost:9092/mem:default

使用 H2 控制台连接:jdbc:h2:mem:default

示例application.yml

spring:
  application:
    name: example-service
  r2dbc:
    url: r2dbc:pool:h2:mem:///default?options=DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
    username: testuser
    password: testpass
    pool:
      initial-size: 100
      max-size: 500
      max-idle-time: 30m
      validation-query: SELECT 1