Java Spring启动数据库在其他计算机上持续存在

Java Spring启动数据库在其他计算机上持续存在,java,spring,hibernate,spring-boot,jpa,Java,Spring,Hibernate,Spring Boot,Jpa,我希望我的Spring Boot应用程序使用我输入的示例数据在其他计算机上运行。目前,我可以退出IDE并重新启动应用程序,它工作正常,但一旦我上传项目供同事下载,他们就没有任何可以访问的数据。我如何实现上传项目的功能,使用应用程序的每个人都可以访问我之前输入的测试数据 主文件夹中的My application.properties: spring.h2.console.enabled=true spring.h2.console.path=/h2 spring.datasource.url=j

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

主文件夹中的My 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
我的身材。格雷德尔:

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'
}

提前多谢

您的数据正在保存到spring.datasource属性配置的数据库中。在您的例子中,这是~/spring-boot-h2-db,它是本地机器上的一个h2数据库

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

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

您还可以使用SpringBoot支持的Flyway等工具在启动时创建参考数据。您可以生成脚本,以创建使用H2命令脚本设置为“fileName”的所有现有数据,如中所述。您可以访问H2控制台,通过添加属性在其中运行脚本命令

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

如果导航到/h2控制台,您将看到一个登录屏幕,询问JDBC连接字符串、用户名和密码。为所有这些数据库输入与属性文件中相同的详细信息,这样就可以对H2 DB运行SQL。

您有多个选项

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

2,如果您想使用h2,可以使用应用程序启动填充它:


3,我猜这是一个h2数据库,存储在您的主目录中的一个文件中,因此您可以复制它。您是否也共享位于用户主目录中的h2数据库文件~/spring-boot-h2-db。这是数据所在的位置。共享此文件,并让您的同事将其放入其用户主目录。非常感谢!首先,更改位置的选项就足够了。是否可以在application.properties中说明db文件位于项目文件夹中并访问它?或者每个下载项目的人都需要手动将db文件放在自己的主目录中吗?Project folder path\TravelDiary您可以将其设置为相对目录,但如果您的目标是设置初始数据,那么Scrach答案中的链接可能是最好的方法。Flyway易于设置,因此您可以确信,无论用户将数据库配置在何处,他们都将拥有相同的数据。我只是在写我的答案的时候忘记了它,不想通过编辑它来窃取信用。但是我已经在H2数据库中设置了很多数据,所以要做的工作会很多。如果将其设置为相对目录,它是如何工作的?以下代码不起作用。。。h2.implicitRelativePath=true-spring.datasource.url=jdbc:h2:~/Traveldiary/h2/spring-boot-h2-db;DB\ U关闭\在\ U出口=FALSE@JaLo您需要以一个新的路径开始您的路径。而不是~表示相对路径。例如,jdbc:h2:file:./db/spring-boot-h2-db将把它放在应用程序工作目录中名为db的目录中。从现有数据库为Flyway生成插入脚本非常简单,尽管我以前为一个项目做过,但我已经在我的答案中添加了一些细节。我强烈建议您在将H2数据库提交到源代码管理中之前探索该选项。