首页 > 解决方案 > Spring Boot 数据库持久存在于其他计算机上

问题描述

我希望我的 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'
}

提前非常感谢!

标签: javaspringhibernatespring-bootjpa

解决方案


你有多种选择

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

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

3,我猜这是一个存储在您主目录中的文件中的 h2 db,因此您也可以复制它<-我不确定这是否有效,但理论上应该没问题


推荐阅读