SpringBoot + Liquibase 简单学习过程

今天项目中需要用到数据库版本控制工具,因为之前项目用过一段时间,使用感觉不错,所以这次还是选择用 liquibase 。好了,接下来进入正题。

1. 创建 SpringBoot 项目

image.gif

image.gif

image.gif

image.gif

2. 添加 Maven 依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.3</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

3. application.properties 配置

spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
spring.datasource.driver=com.mysql.cj.jdbc.Driver
spring.liquibase.change-log=classpath:/db/changelog/master.xml
spring.liquibase.enabled=true
spring.liquibase.contexts=dev, faker

4. 创建 changelog 文件夹

根据 application.properties 中的 spring.liquibase.change-log=classpath:/db/changelog/master.xml 创建 liquibase 的 master.xml 文件。

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<!-- 用于导入子 changelog 文件 -->
    <include file="db/changelog/changelog-1.0.xml" relativeToChangelogFile="false"/>
</databaseChangeLog>

创建子文件 changelog-1.0.xml

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
</databaseChangeLog>

当前项目目录如下:

image.gif

5. 启动项目,查看数据库

 liquibase 生成了两张表

image.gif

databasechangelog :用于 Liquibase 操作记录,以行的形式跟踪每个变更集,并由“ id”,“ author”和“ filename”列的组合标识。

databasechangeloglock :来确保一次仅运行一个 Liquibase 实例。

6. 编写 changelog-1.0.xml 文件

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">


    <changeSet author="liquibase-2020" id="20200701-1">
        <createTable remarks="测试" tableName="order">
            <column name="id" type="VARCHAR(64)">
                <constraints primaryKey="true"/>
            </column>
            <column name="phone" remarks="电话号" type="VARCHAR(64)"/>
            <column name="template_type" remarks="模板类型" type="VARCHAR(18)"/>
            <column name="message" remarks="短信返回信息" type="VARCHAR(255)"/>
            <column defaultValueComputed="CURRENT_TIMESTAMP" name="create_date" remarks="创建时间" type="datetime"/>
            <column name="update_date" remarks="更新时间" type="datetime"/>
        </createTable>
    </changeSet>


</databaseChangeLog>

7. 执行查看表有没有建好

image.gif

order 表已经创建好了。

8. liquibase 相关使用

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">

    <changeSet author="liquibase-2020" id="20200701-1">
        <createTable remarks="测试" tableName="order">
            <column name="id" type="VARCHAR(64)">
                <constraints primaryKey="true"/>
            </column>
            <column name="phone" remarks="电话号" type="VARCHAR(64)"/>
            <column name="template_type" remarks="模板类型" type="VARCHAR(18)"/>
            <column name="message" remarks="短信返回信息" type="VARCHAR(255)"/>
            <column defaultValueComputed="CURRENT_TIMESTAMP" name="create_date" remarks="创建时间" type="datetime"/>
            <column name="update_date" remarks="更新时间" type="datetime"/>
        </createTable>
    </changeSet>


    <!-- 新增字段 -->
    <changeSet id="liquibase-20200701-01" author="liquibase">
        <addColumn tableName="order">
            <column name="order_file" type="VARCHAR(255)" remarks="工单文件"></column>
        </addColumn>
    </changeSet>

    <!-- 修改字段 -->
    <changeSet id="liquibase-20200701-02" author="liquibase">
        <renameColumn tableName="order" oldColumnName="order_file" newColumnName="order_file_new" columnDataType="VARCHAR(512)"/>
    </changeSet>

    <!--修改字段类型-->
    <changeSet id="liquibase-20220930-1" author="liquibase">
        <modifyDataType tableName="order" columnName="order_file" newDataType="text"/>
    </changeSet>

    <!-- 删除表字段 -->
    <changeSet id="liquibase-20200701-03" author="liquibase">
        <dropColumn tableName="order" columnName="order_file_new"/>
    </changeSet>

      <!-- 删除表 -->
    <changeSet id="liquibase-20200701-04" author="liquibase">
        <dropTable tableName="order"/>
    </changeSet>

</databaseChangeLog>

到此结束,欢迎大家下方留言互相学习~

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值