maven 如何自动创建Liquibase变更集?

6ioyuze2  于 2022-11-22  发布在  Maven
关注(0)|答案(1)|浏览(200)

我想通过maven liquibase插件命令自动生成一个变更日志文件。变更日志应该包含一个为DiscountCode实体创建数据库表的变更集。diff是这种情况下正确的插件命令吗?

@Entity
@Table(name = "discount_code")
public class DiscountCode {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long discountId;

    private String discountCode;
    private LocalDate expiration;
    private BigDecimal discountValue;

    public DiscountCode() {}

    public DiscountCode(String discountCode, LocalDate expiration, BigDecimal discountValue) {
        this.discountCode = discountCode;
        this.expiration = expiration;
        this.discountValue = discountValue;
    }

    public Long getDiscountId() {
        return discountId;
    }

    public void setDiscountId(Long discountId) {
        this.discountId = discountId;
    }

    public String getDiscountCode() {
        return discountCode;
    }

    public void setDiscountCode(String discountCode) {
        this.discountCode = discountCode;
    }

    public LocalDate getExpiration() {
        return expiration;
    }

    public void setExpiration(LocalDate expiration) {
        this.expiration = expiration;
    }

    public BigDecimal getDiscountValue() {
        return discountValue;
    }

    public void setDiscountValue(BigDecimal discountValue) {
        this.discountValue = discountValue;
    }

    @Override
    public String toString() {
        return (
            "DiscountCode{" +
            "discountId=" +
            discountId +
            ", discountCode='" +
            discountCode +
            '\'' +
            ", expiration=" +
            expiration +
            ", discountValue=" +
            discountValue +
            '}'
        );
    }
}

以下是可用的liquibase命令:

7cwmlq89

7cwmlq891#

解决方案是使用Liquibase Hibernate插件。

插件配置

<plugins>
    <plugin>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-maven-plugin</artifactId>
        <version>3.4.1</version>
        <configuration>                  
            <propertyFile>src/main/resources/liquibase.properties</propertyFile>
        </configuration> 
        <dependencies>
            <dependency>
                <groupId>org.liquibase.ext</groupId>
                <artifactId>liquibase-hibernate4</artifactId>
                <version>3.5</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>4.1.7.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-jpa</artifactId>
                <version>1.7.3.RELEASE</version>
            </dependency>
        </dependencies>               
    </plugin> 
</plugins>

液体基质性质

changeLogFile=classpath:liquibase-changeLog.xml
url=jdbc:mysql://localhost:3306/oauth_reddit
username=tutorialuser
password=tutorialmy5ql
driver=com.mysql.jdbc.Driver
referenceUrl=hibernate:spring:org.baeldung.persistence.model
  ?dialect=org.hibernate.dialect.MySQLDialect
diffChangeLogFile=src/main/resources/liquibase-diff-changeLog.xml

有关详细信息,请查看此页面:https://www.baeldung.com/liquibase-refactor-schema-of-java-app

相关问题