【问题标题】:How to rollback all changes in MySQL database with Spring Boot如何使用 Spring Boot 回滚 MySQL 数据库中的所有更改
【发布时间】:2017-09-28 23:15:01
【问题描述】:

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

【问题讨论】:

标签: mysql spring spring-boot properties-file


【解决方案1】:

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

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

小心!

如果您不使用 JPA,则可以在应用程序的出口处使用关闭挂钩来清理数据库(活动 uat 配置文件)

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

【讨论】:

    猜你喜欢
    • 2021-10-12
    • 2023-03-16
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 2023-01-09
    相关资源
    最近更新 更多