我希望使用我输入的示例数据在其他计算机上运行我的Spring应用程序。目前,我可以退出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'
}
提前谢谢!
发布于 2019-04-04 20:34:12
您的数据将保存到由spring.datasource
属性配置的数据库中。在您的示例中,这是~/spring-boot-h2-db
,它是本地计算机上的H2数据库。
如果您想在多台机器之间共享数据,那么您需要设置一个他们都可以访问的数据库。对于多个应用程序使用的数据库来说,H2可能不是正确的选择,Postgres或MySql是更好的选择。
另一个选项是更改H2数据库文件的位置,并与应用程序一起提交/上载它。如果您只是尝试提供一些初始数据,那么这可能是一个足够好的解决方案,但是如果您希望在所有应用程序生成时都能看到更改,那么这不会解决您的问题。
您还可以使用Flyway这样的工具(由Spring支持)在启动时创建引用数据。您可以生成脚本,使用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。
发布于 2019-04-04 20:40:42
你有多种选择
1,如果有一个以上的开发人员正在开发一个应用程序,您应该在服务器或计算机上创建一个共享DB,例如,任何人都可以访问的MySql。
2,如果您想使用h2,可以使用应用程序启动来填充它:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html
3,我猜这是一个存储在主目录中的文件中的h2 db,所以您也可以复制它<- Im不确定这是否有效,但理论上应该是可以的。
https://stackoverflow.com/questions/55524155
复制相似问题