zoukankan      html  css  js  c++  java
  • liquibase使用教程

    在项目中引入liquibase过程:

    1、父项目 pom.xml 中添加依赖

    <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.30</version>
            </dependency>
            <dependency>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-core</artifactId>
                <version>3.5.5</version>
            </dependency>
    </dependencies>

    2、添加liquibase.properties文件

    #liquibase
    changeLogFile=src/main/resources/liquibase/db.changelog-master.xml
    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8
    username=root
    password=egova

    3、父项目 pom中添加liquibase插件

    <build>
            <plugins>
                <plugin>
                    <groupId>org.liquibase</groupId>
                    <artifactId>liquibase-maven-plugin</artifactId>
                    <version>3.5.1</version>                
                    <configuration>
                    <!--properties文件路径,该文件记录了数据库连接信息等-->
                    <propertyFile>src/main/resources/liquibase.properties</propertyFile>
                        <propertyFileWillOverride>true</propertyFileWillOverride>
                        <!--生成文件的路径-->
                        <outputChangeLogFile>src/main/resources/liquibase/changelog_dev.xml</outputChangeLogFile>      
                        <!-- <driver>com.mysql.jdbc.Driver</driver>
                        <url>jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8</url>                    
                        <username>root</username>
                        <password>egova</password>-->
                    </configuration>
                </plugin>
            </plugins>
     </build>

    4、新建changelog主文件入口: 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-3.1.xsd">
    
        <include file="src/main/resources/liquibase/changelog_dev.xml"/>
       <!-- <include file="com/example/db/changelog/db.changelog-1.1.xml"/>
        <include file="com/example/db/changelog/db.changelog-2.0.xml"/>-->
    </databaseChangeLog> 

    5、打开maven命令框:可以选择模块

     6、生成changelog

    liquibase:generateChangeLog

       (1) 对当前数据库状态生成 changlog:

       mvn liquibase:generateChangeLog


       (2)只对数据生成 changelog ,(先用别的方式往数据库创建数据后再用此方式生成changelog)
       mvn liquibase:generateChangeLog -Dliquibase.diffTypes=data
       区别:前者是在changelog中追加表创建语句,生成建表语句和数据插入语句,如果表语句已存在,则只生成建表语句:如图

      

    <?xml version="1.1" encoding="UTF-8" standalone="no"?>
    <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-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
        <changeSet author="zhaoyanhao" id="1565662890214-1" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
            <createTable tableName="test1">
                <column name="id" type="BIGINT">
                    <constraints nullable="false"/>
                </column>
                <column name="name" type="VARCHAR(15)">
                    <constraints nullable="false"/>
                </column>
                <column name="email" type="VARCHAR(100)">
                    <constraints nullable="false"/>
                </column>
                <column name="password" type="VARCHAR(8)">
                    <constraints nullable="false"/>
                </column>
                <column name="phone" type="VARCHAR(50)">
                    <constraints nullable="false"/>
                </column>
                <column name="address" type="VARCHAR(255)">
                    <constraints nullable="false"/>
                </column>
                <column name="description" type="LONGTEXT"/>
                <column defaultValueComputed="CURRENT_TIMESTAMP" name="create_time" type="TIMESTAMP">
                    <constraints nullable="false"/>
                </column>
            </createTable>
        </changeSet>
        <changeSet author="zhaoyanhao" id="1565662890214-2" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
            <addPrimaryKey columnNames="id" constraintName="PRIMARY" tableName="test1"/>
        </changeSet>
        <changeSet author="zhaoyanhao" id="1565662890214-3" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
            <createTable tableName="test2">
                <column name="id" type="BIGINT">
                    <constraints nullable="false"/>
                </column>
                <column name="name" type="VARCHAR(15)">
                    <constraints nullable="false"/>
                </column>          
                <column name="password" type="VARCHAR(8)">
                    <constraints nullable="false"/>
                </column>
                <column name="phone" type="VARCHAR(50)">
                    <constraints nullable="false"/>
                </column>
                <column name="address" type="VARCHAR(255)">
                    <constraints nullable="false"/>
                </column>
                <column name="description" type="LONGTEXT"/>
                <column defaultValueComputed="CURRENT_TIMESTAMP" name="create_time" type="TIMESTAMP">
                    <constraints nullable="false"/>
                </column>
            </createTable>
        </changeSet>
        <changeSet author="zhaoyanhao" id="1565662890214-4" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
            <addPrimaryKey columnNames="id" constraintName="PRIMARY" tableName="test2"/>
        </changeSet>
    </databaseChangeLog>
    View Code

    7、执行changelog,写入数据库

    在changeset中添加createTable语句,执行liquibase,使用命令: 

    liquibase:update  将changelog变化的内容写入数据库

    liquibase:updateSQL  检查changelog语法的合法性

    8、在项目中使用liquibase

    @Bean
    public SpringLiquibase liquibase(DataSource dataSource, LiquibaseProperties liquibaseProperties) {
     
            SpringLiquibase liquibase = new SpringLiquibase(taskExecutor, env);
            liquibase.setDataSource(dataSource);
            //制定changelog的位置,这里使用的一个master文件引用其他文件的方式
            liquibase.setChangeLog("classpath:config/liquibase/db.changelog-master.xml");
            liquibase.setContexts(liquibaseProperties.getContexts());
            liquibase.setDefaultSchema(liquibaseProperties.getDefaultSchema());
            liquibase.setDropFirst(liquibaseProperties.isDropFirst());
            return liquibase;
    }

    第三方调用liquibase可以参考:https://blog.csdn.net/weixin_34087307/article/details/91397914

     liquibase命令大全

    命令名称命令描述
    update 更新数据库到当前版本
    updateSQL 写入SQL将数据库更新到currentversion或STDOUT
    updateCount <num> 将下一个NUM更改应用到数据库
    updateCountSQL <num> 写入SQL以将下一个NUM更改应用到数据库
    updateToTag <tag> 使用指定的标记将数据库更新到变更集
    updateToTagSQL <tag> 使用指定的标记将SQL写入(到标准输出)到更改集
    rollback <tag> 将数据库回滚到指定标签的状态is was
    rollbackSQL <tag> 生成数据库回滚到指定标签的sql
    rollbackToDate <date/time> 将数据库回滚到给定日期/时间的状态is was。日期格式:yyyy-MM-dd 'HH: mm: ss
    rollbackToDateSQL <date/time> 写入SQL以将数据库回滚到给定日期/时间版本的状态到STDOUT
    rollbackCount <value> 回滚应用于数据库的最后一个<值>更改集
    rollbackCountSQL <value> 写入SQL以回滚最后一个<值>更改集到应用于数据库的stdoutapply
    futureRollbackSQL 写入SQL,以便在更改日志中的更改完成后将数据库回滚到当前状态
    futureRollbackSQL <value> 在更改日志中的<值>更改完成后,写入SQL以将数据库回滚到当前状态
    futureRollbackFromTagSQL <tag> 写入(到标准输出)SQL,以便在更改后将数据库回滚到其当前状态
    updateTestingRollback 更新数据库,然后再次回滚更改。用于测试回滚支持
    generateChangeLog 写入更改日志XML以将数据库的当前状态复制到标准输出
    snapshot 将数据库的当前状态写入标准输出
    snapshotReference 将referenceUrl数据库的当前状态写入标准输出
    Diff Commands 数据库对比命令
    diff [diff parameters] 数据库对比命令
    diffChangeLog [diff parameters] 数据库对比日志
    Documentation Commands 文档命令
    dbDoc <outputDirectory> 基于当前数据库和更改日志生成类似javadoc的文档
    Maintenance Commands 维护命令
    tag <tag string> 给当前的数据库打标签,方便日后回滚
    tagExists <tag string> 检查对应的标签是否存在
    status [--verbose] 输出为执行changeset的行数
    unexpectedChangeSets[--verbose] 输出本地不存在changeset 行数
    validate 检查是否有错误的changelog
    calculateCheckSum <id> 检查指定changeset id 的checksum值 格式为 filepath::id::author
    clearCheckSums 从数据库日志中删除所有保存的校验和
    changelogSync 标记所有的更改已执行
    changelogSyncSQL 生成标记更改已执行的sql并输出到标准输出
    markNextChangeSetRan 将下一个变更标记为已执行
    markNextChangeSetRanSQL 生成将下一个变更标记为已执行的sql并输出到标准输出
    listLocks 列出liquibase数据库锁
    releaseLocks 释放所有的liquibase数据库锁
    dropAll 删除数据库表(慎用!)



  • 相关阅读:
    Linux 文件排序
    ubuntu18.04 美化桌面
    git clone 加速
    ubunutu下图像编辑器安装
    vue.js实战教程 https://www.jb51.net/Special/978.htm
    原生JS实现多条件筛选
    php结合js实现多条件组合查询
    js前端 多条件筛选查询
    JS 判断字符串是否全部为数字
    GET请求中URL的最大长度限制总结
  • 原文地址:https://www.cnblogs.com/zhaoyanhaoBlog/p/11344954.html
Copyright © 2011-2022 走看看