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

Jac*_*ack 17 spring intellij-idea h2 spring-boot

要么我错过了一些核心概念,这些概念深埋在一些文档中(Spring,Spring Boot,H2,HSQLDB,Derby,IntelliJ),或者我已经盯着这个太久了.

我有一个Spring Boot项目.尝试首先使用和初始化H2数据库,尝试在IntelliJ中连接它只是为了意识到我可能无法轻松浏览数据库而不放弃我的长子(使用IntelliJ数据库客户端连接到H2数据库).

所以我搬到了DerbyDB.同样的事情 - db root文件夹是在我的应用程序中创建的,我在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
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

我看到一些投票要结束,因为我不清楚我在问什么:

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

hee*_*nee 14

H2自动混合模式应该没问题.使用jdbc:h2:~/mydbInHomeDir;AUTO_SERVER=TRUE为您的spring.datasource.url.在IntelliJ中,创建一个远程 H2数据源并使用完全相同的JDBC URL.您可能必须明确按下IntelliJ数据库窗口中的"同步"按钮以显示要显示的数据库表.


Vla*_*mir 7

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

我正在使用 spring-boot 和 spring-data-jpa。确保为代表每个表的实体声明了@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
Run Code Online (Sandbox Code Playgroud)

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

像这样配置你的 IntelliJ 在此处输入图片说明

如果您不使用 IntelliJ 下载服务器/客户端组合@ http://www.h2database.com/html/download.html 解压缩它并使用以下命令启动基于浏览器的客户端:

h2/bin: java -cp h2*.jar org.h2.tools.Server
Run Code Online (Sandbox Code Playgroud)

通过指定连接字符串连接到您的嵌入式数据库: 在此处输入图片说明


sol*_*nmv 6

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

这是我的配置示例:

应用程序.yml

spring:
  datasource.url: jdbc:h2:./db/testDb
  jpa.hibernate.ddl-auto: create
Run Code Online (Sandbox Code Playgroud)

IntelliJ 数据库属性

在此处输入图片说明

结果

在此处输入图片说明