我有一个用Spring Boot开发的Spring MVC应用程序 . 顺便说一下,这个应用程序仅用于学习目的 .
默认情况下,应用程序启动并使用MySQL数据库 . 对于单元和集成测试,我使用内存H2数据库,它完美地工作 .
现在,对于UAT,它需要有另一个MySQL数据库 . 因此,在这个新数据库中,我已经为应用程序插入了足够的数据以便正常工作,但每次应用程序在UAT配置文件下运行时,它都会填充我必须手动清理或从sql文件恢复的数据 .
我要2个档案 . 首先,它是 生产环境 概况:
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost/dbprod
spring.datasource.username = root
spring.datasource.password = mysql
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
其次,它是uat配置文件:
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost/dbuat
spring.datasource.username = root
spring.datasource.password = mysql
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
我的问题是如何告诉Spring在UAT配置文件下它应该丢弃所做的所有更改(插入,更新和删除) . 如果可以在application-uat.properties文件中完成,那将会很好
1 回答
如果您使用JPA,则可以在application-uat.properties文件中设置'create-drop'选项,以便在会话结束后使用该模式:
小心!
如果您不使用JPA,则可以在应用程序的出口处使用关闭挂钩来清理数据库(活动的uat配置文件)
https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-application-exit