Springboot集成Liquibase笔记整理

  1. 添加依赖

    text 复制代码
    <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-core</artifactId>
    </dependency>
  2. 添加配置

    yml 复制代码
    spring:
      liquibase:
        contexts: dev,test
        enabled: true
  3. 编写liquibase配置类

    java 复制代码
    @Configuration
    @EnableConfigurationProperties(LiquibaseProperties.class)
    public class LiquibaseConfig {
        @Bean
        public SpringLiquibase liquibase(DataSource dataSource, LiquibaseProperties properties) {
            SpringLiquibase liquibase = new SpringLiquibase();
            liquibase.setDataSource(dataSource);
            //指定changelog的位置,这里使用的一个master文件引用其他文件的方式
            liquibase.setChangeLog("classpath:liquibase/master.xml");
            liquibase.setContexts(properties.getContexts());
            liquibase.setDefaultSchema(properties.getDefaultSchema());
            liquibase.setDropFirst(properties.isDropFirst());
            liquibase.setShouldRun(properties.isEnabled());
            liquibase.setChangeLogParameters(properties.getParameters());
            liquibase.setRollbackFile(properties.getRollbackFile());
            liquibase.setShouldRun(true);
            return liquibase;
        }
    }
  4. 编写master.xml文件(注意includeAll配置的路径与changelog文件的路径)

    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-3.6.xsd">
        <property name="now" value="now()" dbms="h2"/>
        <property name="now" value="now()" dbms="mysql"/>
        <property name="floatType" value="float4" dbms="postgresql, h2"/>
        <property name="floatType" value="float" dbms="mysql, oracle, mssql, mariadb"/>
        <property name="clobType" value="clob" dbms="h2"/>
        <property name="clobType" value="clob" dbms="mysql, oracle, mssql, mariadb, postgresql"/>
        <property name="uuidType" value="varchar(36)" dbms="h2, mysql, mariadb"/>
        <includeAll path="liquibase/changelog/"/>
    </databaseChangeLog>
  5. 编写changelog文件 00000000000000_init_schema.xml

    xml 复制代码
    <?xml version="1.0" encoding="utf-8"?>
    <databaseChangeLog
            xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
            xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
            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.6.xsd
                            http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
        <property name="autoIncrement" value="true"/>
        <changeSet author="system" id="00000000000001" context="dev">
            <createTable tableName="hello_date_time_wrapper">
                <column  name="id" type="BIGINT">
                    <constraints primaryKey="true" primaryKeyName="hello_date_time_wrapperPK"/>
                </column>
                <column name="instant" type="timestamp"/>
                <column name="local_date_time" type="timestamp"/>
                <column name="offset_date_time" type="timestamp"/>
                <column name="zoned_date_time" type="timestamp"/>
                <column name="local_time" type="time"/>
                <column name="offset_time" type="time"/>
                <column name="local_date" type="date"/>
            </createTable>
        </changeSet>
    </databaseChangeLog>
相关推荐
Tony882 分钟前
热题100 - 394. 字符串解码
java·算法
cwtlw5 分钟前
JavaRedis和数据库相关面试题
java·数据库·笔记·面试
XF鸭6 分钟前
身份证正则表达式详解
java
Lecea_L8 分钟前
🔍 找到数组里的“节奏感”:最长等差子序列
java·算法
JojO_o24 分钟前
frida 在安卓模拟器上的使用
java
kill bert30 分钟前
第32周Java微服务入门 微服务基础
java·开发语言·微服务
小智疯狂敲代码32 分钟前
Java架构师成长之路-框架源码系列-整体认识Spring体系结构(2)
java
Determined_man34 分钟前
黑马点评,后端请求没问题,前端页面有时候请求超时,无响应数据?
java·后端
山山而川粤35 分钟前
SSM考研信息查询系统
java·大数据·运维·服务器·开发语言·数据库·考研
计算机-秋大田36 分钟前
基于Spring Boot的轻型卡车零部件销售平台的设计与实现(LW+源码+讲解)
java·vue.js·spring boot·后端·课程设计