Liquibase与Spring Boot集成指南 | AI生成和翻译

Home 2025.10

将 Liquibase 与 Spring Boot 集成

Liquibase 是一种流行的开源工具,用于以版本控制的方式管理数据库模式变更(迁移)。Spring Boot 为 Liquibase 提供了出色的自动配置支持,使其易于集成。本指南假设您使用的是 Spring Boot 3.x(截至 2025 年的最新版本)和 Maven,但这些概念同样适用于 Gradle。我们将逐步介绍基础知识。

前提条件

步骤 1:添加 Liquibase 依赖

在您的 pom.xml 中包含 Liquibase Spring Boot starter。这会引入 Liquibase 并将其无缝集成。

<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId> <!-- 用于数据库连接 -->
</dependency>

对于 Gradle,请添加到 build.gradle

implementation 'org.liquibase:liquibase-core'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'

运行 mvn clean install(或 ./gradlew build)以下载依赖项。

步骤 2:配置 Liquibase

如果您将变更日志文件放在默认位置,Spring Boot 会自动检测 Liquibase。通过 application.properties(或等效的 .yml 文件)进行自定义。

示例 application.properties

# 数据库设置(根据您的数据库进行调整)
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

# Liquibase 配置
spring.liquibase.change-log=classpath:db/changelog/db.changelog-master.xml
spring.liquibase.enabled=true  # 默认为 true
spring.liquibase.drop-first=false  # 在开发环境中设置为 true,以便在启动时删除模式

步骤 3:创建变更日志文件

Liquibase 使用“变更日志”来定义模式变更。在 src/main/resources 下创建目录结构:

src/main/resources/
└── db/
    └── changelog/
        ├── db.changelog-master.xml  # 包含其他文件的主文件
        └── changes/
            ├── 001-create-users-table.xml  # 单个变更
            └── 002-add-email-column.xml

主变更日志 (db.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.24.xsd">

    <include file="changes/001-create-users-table.xml"/>
    <include file="changes/002-add-email-column.xml"/>
</databaseChangeLog>

示例变更 (001-create-users-table.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.24.xsd">

    <changeSet id="001" author="yourname">
        <createTable tableName="users">
            <column name="id" type="bigint">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="varchar(255)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>
</databaseChangeLog>

步骤 4:运行和测试

常见技巧

有关高级功能(如标记回滚或自定义扩展),请参阅文档。

参考资料


Back

x-ai/grok-4-fast

Donate