springboot 集成liquibase框架

Liquibase是什么?

Liquibase主要是做数据库迁移及DDL语句自动同步的框架,可以有效地帮助项目部署的时候保证数据表的一致性

如何在Springboot 项目中集成?

  1. 添加依赖
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>4.23.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.23</version>
        </dependency>
  1. 增加配置文件信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/lqb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=123456

spring.liquibase.enabled=true
spring.liquibase.change-log=classpath:db/changelog-master.xml
  1. 增加xml 文件
    在spring.liquibase.change-log对应的位置增加changelog-master.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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-4.2.xsd">


    <include file="db/changelog/db.changelog-1.0.xml"/>
    <include file="db/changelog/db.changelog-1.1.xml"/>
<!--    <include file="db/changelog/db.changelog-2.0.xml"  />-->
</databaseChangeLog>
  1. 在db/changelog下增加对应的db.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-4.1.xsd">

    <changeSet id="1" author="your_name">
        <createTable tableName="example_table">
            <column name="id" type="BIGINT" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="VARCHAR(255)"/>
        </createTable>
    </changeSet>
   
    <changeSet id="2" author="your_name">
        <createTable tableName="example_table2">
            <column name="id" type="BIGINT" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="VARCHAR(255)"/>
        </createTable>
    </changeSet>

</databaseChangeLog>

  1. 启动
    没有意外,服务启动后你的数据库就会增加数据表进来

反向处理数据库,生成xml的changelog

  1. 需要集成maven插件
    <build>
        <plugins>
            <!-- 添加Liquibase插件 -->
            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>4.4.2</version>
                <configuration>
<!--                    需要商业版 才能使用rollback-->
                    <changeLogFile>${basedir}/src/main/resources/db/changelog/db.changelog-1.1.xml</changeLogFile>
                    <propertyFileWillOverride>true</propertyFileWillOverride>
        <!--                   输出的文件位置-->            <outputChangeLogFile>${basedir}/src/main/resources/db/changelog/expsChangelog.xml</outputChangeLogFile>
                    <driver>com.mysql.cj.jdbc.Driver</driver>
                    <url>jdbc:mysql://127.0.0.1:3306/lqb?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=true</url>
                    <username>root</username>
                    <password>123456</password>
                    <outputFileEncoding>UTF-8</outputFileEncoding>
                    <verbose>true</verbose>
                    <diffTypes>tables, views, columns, indexs,foreignkeys, primarykeys, uniqueconstraints, data</diffTypes>
                </configuration>
                <dependencies>
                    <!-- 可选:如果您需要支持特定的数据库驱动程序,可以在此处添加依赖 -->
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  1. 在idea maven插件中找到liquibase相关插件


    image.png
  2. 执行成功会在指定的位置看到文件

XML样例

  1. 创建表
<changeSet id="createTableExample" author="your_name">
    <createTable tableName="example_table">
        <column name="id" type="INT" autoIncrement="true" primaryKey="true"/>
        <column name="name" type="VARCHAR(255)"/>
        <column name="age" type="INT"/>
        <!-- Add more columns as needed -->
    </createTable>
</changeSet>

  1. 添加列
<changeSet id="addColumnExample" author="your_name">
    <addColumn tableName="example_table">
        <column name="email" type="VARCHAR(255)"/>
    </addColumn>
</changeSet>

  1. 修改列
<changeSet id="modifyColumnExample" author="your_name">
    <modifyColumn tableName="example_table" columnName="age" newDataType="INT"/>
</changeSet>

  1. 删除列
<changeSet id="dropColumnExample" author="your_name">
    <dropColumn tableName="example_table" columnName="email"/>
</changeSet>

已有数据表和数据的数据库如何处理呢

  1. 反向生成changelog.xml
  2. 备份有数据的库表数据
  3. 删除所有表,让项目启动时先自动加载所有库表
  4. 通过转储文件恢复数据库
  5. 按照规范流程使用Liquibase即可
  6. 类似流程引擎的表最好手动添加 不要反向生成changelog.xml
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容