mysql - 如何使用 Spring Boot 回滚 MySQL 数据库中的所有更改

标签 mysql spring spring-boot properties-file

我有一个使用 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 文件中完成,那就太好了

最佳答案

如果您使用 JPA,则可以在 application-uat.properties 文件中设置“create-drop”选项,以便在 session 后销毁架构:

spring.jpa.hibernate.ddl-auto=create-drop 

小心!

如果您不使用 JPA,您可以在应用程序退出时使用关闭 Hook 来清理数据库(事件 uat 配置文件)

https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-application-exit

关于mysql - 如何使用 Spring Boot 回滚 MySQL 数据库中的所有更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46479601/

相关文章:

链接到辅助表的插入/更新查询的 MYSQL 语法错误

java - 在Spring Boot java中从数据库生成xml

java - 添加 JMS 信息会破坏 EhCache

php - 不使用 get_result() 的准备语句

php - PDO - 迭代绑定(bind)变量

java - @WithUserDetails 注释不适用于 Spring 单元测试

java - 评论树,回复回复即可显示

java - Spring boot - @Service类调用另一个@Service类

angular - 将授权 header 添加到 Springfox

php - 为什么日期是表格中的必填字段?