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>
相关推荐
程序猿chen7 分钟前
JVM考古现场(二十五):逆熵者·时间晶体的永恒之战(进阶篇)
java·jvm·git·后端·程序人生·java-ee·改行学it
CopyLower15 分钟前
Spring Boot的优点:赋能现代Java开发的利器
java·linux·spring boot
细心的莽夫17 分钟前
Elasticsearch复习笔记
java·大数据·spring boot·笔记·后端·elasticsearch·docker
程序员阿鹏27 分钟前
实现SpringBoot底层机制【Tomcat启动分析+Spring容器初始化+Tomcat 如何关联 Spring容器】
java·spring boot·后端·spring·docker·tomcat·intellij-idea
大叔比较胖1 小时前
VSCode 用于JAVA开发的环境配置,JDK为1.8版本时的配置
java·ide·vscode·jdk·1.8
种时光的人1 小时前
多线程出bug不知道如何调试?java线程几种常见状态
java·python·bug
小尹哥-程序员1 小时前
springboot2.X创建maven多模块工程
java·maven
web安全工具库1 小时前
Python内存管理之隔代回收机制详解
java·jvm·算法
大学生亨亨2 小时前
蓝桥杯之递归
java·笔记·算法·蓝桥杯
不辉放弃2 小时前
MySQL存储过程
java·数据库·sql