原创

Spring Boot集成liquibase快速入门Demo

1.什么是liquibase?

Liquibase是一个用于跟踪、管理和应用数据库变化的开源的数据库重构工具。它将所有数据库的变化(包括结构和数据)都保存在 changelog 文件中,便于版本控制,它的目标是提供一种数据库类型无关的解决方案,通过执行 schema 类型的文件来达到迁移。
Liquibase 特性
  • 支持几乎所有主流的数据库,如 MySQL, PostgreSQL, Oracle, Sql Server, DB2 等;
  • 支持多开发者的协作维护;
  • 日志文件支持多种格式,如 XML, YAML, JSON, SQL等;
  • 支持上下文相关逻辑
  • 生成数据库变更文档
  • 支持多种运行方式,如命令行、Spring 集成、Maven 插件、Gradle 插件等。

2.环境准备

version: '3'
services:
  mysql:
    image: registry.cn-hangzhou.aliyuncs.com/zhengqing/mysql:5.7
    container_name: mysql_3306
    restart: unless-stopped
    volumes:
      - "./mysql/my.cnf:/etc/mysql/my.cnf"
      - "./mysql/init-file.sql:/etc/mysql/init-file.sql"
      - "./mysql/data:/var/lib/mysql"
#      - "./mysql/conf.d:/etc/mysql/conf.d"
      - "./mysql/log/mysql/error.log:/var/log/mysql/error.log"
      - "./mysql/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d" # init sql script directory -- tips: it can be excute  when `/var/lib/mysql` is empty
    environment:                        # set environment,equals docker run -e
      TZ: Asia/Shanghai
      LANG: en_US.UTF-8
      MYSQL_ROOT_PASSWORD: root         # set root password
      MYSQL_DATABASE: root              # init database name
    ports:                              # port mappping
      - "3306:3306"
注意my.cnf在windows系统必须为只读,否则忽略
docker-compose -f docker-compose.yml -p mysql57 up -d

3.代码工程

实验目标

实验mysql数据初始化

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Liquibase</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <dependencies>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <configuration>
                    <!--生成Changelog的输出目录-->
                    <outputChangeLogFile>${basedir}/src/main/resources/liquibase/generate/changeLog.xml</outputChangeLogFile>
                    <!--DB连接信息-->
                    <driver>com.mysql.jdbc.Driver</driver>
                    <url>jdbc:mysql://localhost:3306/demo?useSSL=false</url>
                    <username>root</username>
                    <password>root</password>
                    <dropFirst>false</dropFirst>
                    <verbose>true</verbose>
                    <logging>debug</logging>
                    <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
                    <outputFileEncoding>UTF-8</outputFileEncoding>
                    <propertyFileWillOverride>true</propertyFileWillOverride>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

applocation.properties

spring.datasource.url = jdbc:mysql://127.0.0.1:3306/demo?createDatabaseIfNotExist=true&allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver

spring.liquibase.change-log = classpath:liquibase/master.xml
spring.liquibase.contexts = dev
400928528

4.测试

启动Spring Boot应用程序,查看启动日志
2024-08-05T14:36:52.691+08:00 INFO 26572 --- [ main] liquibase.changelog : Reading from demo.databasechangelog
Database is up to date, no changesets to execute
2024-08-05T14:36:52.730+08:00 INFO 26572 --- [ main] liquibase.changelog : Reading from demo.databasechangelog
2024-08-05T14:36:52.737+08:00 INFO 26572 --- [ main] liquibase.util : UPDATE SUMMARY
2024-08-05T14:36:52.737+08:00 INFO 26572 --- [ main] liquibase.util : Run: 0
2024-08-05T14:36:52.737+08:00 INFO 26572 --- [ main] liquibase.util : Previously run: 1
2024-08-05T14:36:52.737+08:00 INFO 26572 --- [ main] liquibase.util : Filtered out: 0
2024-08-05T14:36:52.737+08:00 INFO 26572 --- [ main] liquibase.util : -------------------------------
2024-08-05T14:36:52.737+08:00 INFO 26572 --- [ main] liquibase.util : Total change sets: 1
2024-08-05T14:36:52.738+08:00 INFO 26572 --- [ main] liquibase.util : Update summary generated
2024-08-05T14:36:52.755+08:00 INFO 26572 --- [ main] liquibase.lockservice : Successfully released change log lock
2024-08-05T14:36:52.757+08:00 INFO 26572 --- [ main] liquibase.command : Command execution complete
因为我之前已经启动过一次,sql已经被执行了,所以第二次不会再执行。 03394071 Liquibase默认会生成databasechangelog和databasechangeloglock表(记录变更日志)

5.引用

 
正文到此结束
Loading...