【问题标题】:Spring Boot Database persists on other computersSpring Boot 数据库持久存在于其他计算机上
【发布时间】:2019-08-26 16:39:58
【问题描述】:

我希望我的 Spring Boot 应用程序使用我输入的示例数据在其他计算机上运行。目前,我可以退出 IDE 并重新启动应用程序,它工作正常,但是当我上传我的项目供同事下载时,他们没有任何可以访问的数据。如何实现可以上传项目的功能,让使用该应用程序的每个人都可以访问我之前输入的测试数据?

我在主文件夹中的 application.properties:

spring.h2.console.enabled=true
spring.h2.console.path=/h2

spring.datasource.url=jdbc:h2:~/spring-boot-h2-db;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

spring.jpa.hibernate.ddl-auto=update

我的 build.gradle:

plugins {
    id 'org.springframework.boot' version '2.1.3.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'de.hsba.bi.traveldiary'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
    testImplementation 'com.h2database:h2'
    implementation 'org.springframework.boot:spring-boot-devtools'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:2.3.0'
    implementation 'org.springframework.boot:spring-boot-starter-web-services'
    implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
    runtime 'com.h2database:h2'
}

提前非常感谢!

【问题讨论】:

  • 您是否也共享位于用户主页中的 h2 数据库文件 (~/spring-boot-h2-db)。这就是数据所在。也分享这个文件,让你的同事把它放到他们的用户主目录中。

标签: java spring hibernate spring-boot jpa


【解决方案1】:

您的数据正在保存到由spring.datasource 属性配置的数据库中。在您的情况下,这是 ~/spring-boot-h2-db,它是您本地计算机上的 H2 数据库。

如果您想在多台机器之间共享数据,那么您需要设置一个它们都可以访问的数据库。对于被多个应用程序使用的数据库,H2 可能不是正确的选择,Postgres 或 MySql 是更好的选择。

另一种选择是更改 H2 数据库文件的位置并将其与您的应用程序一起提交/上传。如果您只是想提供一些初始数据,那么这可能是一个足够好的解决方案,但如果您希望更改在所有应用程序中可见,那么这将无法解决您的问题。

您还可以使用 Flyway(受 Spring Boot 支持)之类的工具在启动时创建参考数据。您可以使用 H2 命令SCRIPT TO 'fileName'(如this SO answer 中所述)生成脚本来创建您设置的所有现有数据。您可以访问 H2 控制台,您可以在其中通过添加属性来运行 SCRIPT 命令

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.h2.console.settings.web-allow-others=true #ONLY if you want to be able to connect using something other than "localhost" in your URL

如果您导航到<application-path>/h2-console,您将看到一个登录屏幕,要求您输入 JDBC 连接字符串、用户名和密码。为所有这些输入与您在属性文件中相同的详细信息,您将能够针对您的 H2 DB 运行 SQL。

【讨论】:

  • 非常感谢!首先,更改位置的选项就足够了。是否可以在 application.properties 中阐明 db 文件位于项目文件夹中并访问它?还是每个下载项目的人都需要手动将 db 文件放在他的主目录中? (项目文件夹路径\Traveldiary)
  • 您可以将其设为相对目录,但如果您的目标是设置初始数据,那么 Scrach 答案中的链接可能是最好的方法。 Flyway 易于设置,然后您可以确信每个用户将拥有相同的数据,无论他们将数据库配置在何处。我只是在写答案时忘记了它,不想通过编辑来窃取信用。
  • 但是我已经在 H2 数据库中设置了很多数据,所以要进行大量的返工。如果将其设为相对目录,它是如何工作的?以下代码不起作用... h2.implicitRelativePath=true spring.datasource.url=jdbc:h2:~/Traveldiary/h2/spring-boot-h2-db;DB_CLOSE_ON_EXIT=FALSE
  • @JaLo 您需要以. 而不是~ 作为相对路径开始您的路径。例如。 jdbc:h2:file:./db/spring-boot-h2-db 会将其放在应用程序工作目录中名为“db”的目录中。虽然从现有数据库为 Flyway 生成插入脚本非常容易(我之前为一个项目做过),但我已经在我的答案中添加了一些详细信息。我强烈建议您在将 H2 数据库提交到源代码管理之前探索该选项。
【解决方案2】:

您有多种选择

1,如果有超过 1 个开发人员在开发一个应用程序,您应该在任何人都可以访问的服务器或计算机上创建一个共享数据库,例如 MySql

2,如果您想使用 h2,您可以使用应用程序启动来填充它: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html

3,我猜这是一个存储在你主目录中的文件中的 h2 db,所以你也可以复制它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多