Spring Boot 集成 Liquibase,数据库也能做版本控制!

本文介绍如何在 Spring Boot 项目中集成 Liquibase 进行数据库变更管理,包括创建表、添加字段、创建索引和初始化数据。同时,文章还提到一个 Liquibase 模版生成器插件,用于生成 YAML 格式的 changelog,以及如何生成数据库修改文档。
摘要由CSDN通过智能技术生成

今天给大家介绍另外一款比较不错的数据库变更管理工具:Liquibase

本文将带着大家实操一个 SpringBoot 结合 Liquibase 的项目,看看如何新增数据表、修改表字段、初始化数据等功能,顺带使用一下 Liquibase 模版生成器插件。

本项目包含两个小项目,一个是 liquibase 模版生成器插件,项目名叫做 liquibase-changelog-generate,另一个项目是 liquibase 应用,叫做 springboot-liquibase。

Liquibase 模版生成器插件

创建一个 maven 项目 liquibase-changelog-generate,本项目具备生成 xml 和 yaml 两种格式的 changelog,个人觉得 yaml 格式的 changelog 可读性更高。

1、导入依赖

<dependencies>  <!-- https://mvnrepository.com/artifact/org.apache.maven/maven-plugin-api -->  <dependency>    <groupId>org.apache.maven</groupId>    <artifactId>maven-plugin-api</artifactId>    <version>3.8.6</version>  </dependency>  <dependency>    <groupId>org.apache.maven.plugin-tools</groupId>    <artifactId>maven-plugin-annotations</artifactId>    <version>3.6.4</version>    <scope>provided</scope>  </dependency>  <dependency>    <groupId>cn.hutool</groupId>    <artifactId>hutool-all</artifactId>    <version>5.8.5</version>  </dependency>
</dependencies>
<build>  <plugins>    <plugin>      <groupId>org.apache.maven.plugins</groupId>      <artifactId>maven-plugin-plugin</artifactId>      <version>3.6.4</version>      <!-- 插件执行命令前缀 -->      <configuration>        <goalPrefix>hresh</goalPrefix>        <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>      </configuration>    </plugin>    <plugin>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-maven-plugin</artifactId>      <version>2.6.3</version>    </plugin>    <!-- 编码和编译和JDK版本 -->    <plugin>      <groupId>org.apache.maven.plugins</groupId>      <artifactId>maven-compiler-plugin</artifactId>      <configuration>        <source>1.8</source>        <target>1.8</target>      </configuration>    </plugin>  </plugins></build>

2、定义一个接口,提前准备好公用代码,主要是判断 changelog id 是否有非法字符,并且生成 changelog name。

public interface LiquibaseChangeLog {
  
  default String getChangeLogFileName(String sourceFolderPath) {
      System.out.println("> Please enter the id of this change:");    Scanner scanner = new Scanner(System.in);    String changeId = scanner.nextLine();    if (StrUtil.isBlank(changeId)) {
        return null;    }
    String changeIdPattern = "^[a-z][a-z0-9_]*$";    Pattern pattern = Pattern.compile(changeIdPattern);    Matcher matcher = pattern.matcher(changeId);    if (!matcher.find()) {
        System.out.println("Change id should match " + changeIdPattern);      return null;    }
    if (isExistedChangeId(changeId, sourceFolderPath)) {
        System.out.println("Duplicate change id :" + changeId);      return null;    }
    Date now = new Date();    String timestamp = DateUtil.format(now, "yyyyMMdd_HHmmss_SSS");    return timestamp + "__" + changeId;  }
  default boolean isExistedChangeId(String changeId, String sourceFolderPath) {
      File file = new File(sourceFolderPath);    File[] files = file.listFiles();    if (null == files) {
        return false;    }
    for (File f : files) {
        if (f.isFile()) {
          if (f.getName().contains(changeId)) {
            return true;        }      }    }    return fal
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值