SpringBoot + Liquibase 简单学习过程

    技术2022-07-11  113

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

     

    1. 创建 SpringBoot 项目

     

    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+8&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>

     

    当前项目目录如下:

     

     

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

     liquibase 生成了两张表

    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. 执行查看表有没有建好

    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-20200701-03" author="liquibase"> <dropColumn tableName="order" columnName="order_file_new"/> </changeSet> <!-- 删除表 --> <changeSet id="liquibase-20200701-04" author="liquibase"> <dropTable tableName="order"/> </changeSet> </databaseChangeLog>

     

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

     

    Processed: 0.012, SQL: 9