1. 创建表

drop database if exists mybatis;
create database mybatis;
use mybatis; create table mybatis.CUSTOMERS (
ID bigint not null primary key,
NAME varchar(15) not null,
EMAIL varchar(128) not null,
PASSWORD varchar(8) not null,
PHONE int ,
ADDRESS varchar(255),
SEX char(1) ,
IS_MARRIED bit,
DESCRIPTION text,
IMAGE blob,
BIRTHDAY date,
REGISTERED_TIME timestamp
); select * from mybatis.CUSTOMERS;

2. 配置pom.xml

<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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>middleware</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>liquibase</artifactId>     <properties>
        <jdbc.driver>com.mysql.cj.jdbc.Driver</jdbc.driver>
        <jdbc.url>jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=GMT&amp;useSSL=false&amp;allowPublicKeyRetrieval=true</jdbc.url>
        <jdbc.username>root</jdbc.username>
        <jdbc.password>1234</jdbc.password>
    </properties>     <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>             <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>3.5.3</version>
                <configuration>
                    <!--指定执行主文件 -->
                    <changeLogFile>${basedir}/src/main/resources/conf/liquibase/master_changelog.xml</changeLogFile>
                    <diffChangeLogFile>${basedir}/src/main/resources/conf/liquibase/changelog/${maven.build.timestamp}_changelog.xml</diffChangeLogFile>
                    <outputChangeLogFile>${basedir}/src/main/resources/conf/liquibase/changelog/changelog_original.xml</outputChangeLogFile>                     <driver>${jdbc.driver}</driver>
                    <url>${jdbc.url}</url>
                    <username>${jdbc.username}</username>
                    <password>${jdbc.password}</password>                     <dropFirst>false</dropFirst>
                    <defaultSchemaName />
                    <referenceUrl>hibernate:spring:com.jaguar.myapp.domain?dialect=&amp;hibernate.ejb.naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy</referenceUrl>
                    <verbose>true</verbose>
                    <logging>debug</logging>                     <!-- 是否需要弹出确认框 -->
                    <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
                    <!--输出文件的编码 -->
                    <outputFileEncoding>UTF-8</outputFileEncoding>
                    <!--执行的时候是否显示详细的参数信息 -->
                    <verbose>true</verbose>
                    <!--是否每次都重新加载properties -->
                    <propertyFileWillOverride>true</propertyFileWillOverride>
                    <rollbackTag>${project.version}</rollbackTag>
                    <tag>${project.version}</tag>
                </configuration>
            </plugin>
        </plugins>
    </build>     <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>         <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>3.6.1</version>
        </dependency>
    </dependencies>
</project>

3. 根据数据库反向生成changeLog文件    mvn liquibase:generateChangeLog

创建空changelog_original.xml

<?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">
</databaseChangeLog>

执行 mvn liquibase:generateChangeLog

changelog_original.xml

<?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="kd (generated)" id="1529903520054-1">
<createTable tableName="customers">
<column name="ID" type="BIGINT">
<constraints nullable="false"/>
</column>
<column name="NAME" type="VARCHAR(15)">
<constraints nullable="false"/>
</column>
<column name="EMAIL" type="VARCHAR(128)">
<constraints nullable="false"/>
</column>
<column name="PASSWORD" type="VARCHAR(8)">
<constraints nullable="false"/>
</column>
<column name="PHONE" type="INT"/>
<column name="ADDRESS" type="VARCHAR(255)"/>
<column name="SEX" type="CHAR(1)"/>
<column name="IS_MARRIED" type="BIT(1)"/>
<column name="DESCRIPTION" type="TEXT"/>
<column name="IMAGE" type="BLOB"/>
<column name="BIRTHDAY" type="date"/>
<column name="REGISTERED_TIME" type="TIMESTAMP(26)"/>
</createTable>
</changeSet>
<changeSet author="kd (generated)" id="1529903520054-2">
<addPrimaryKey columnNames="ID" constraintName="PRIMARY" tableName="customers"/>
</changeSet>
</databaseChangeLog>

4. 清空当前数据库,包括liquibase的版本信息 mvn liquibase:dropAll

5. 将xml的改变更新到数据库     mvn liquibase:update

(1) 指定执行主文件 master_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.5.xsd"> <!-- mvn liquibase:update -->
<include file="conf/liquibase/changelog/00000000000000_initial_common.xml" relativeToChangelogFile="false"/>
</databaseChangeLog>

(2) 初始化的文件 00000000000000_initial_common.xml

拷贝自changelog_original.xml

两个重要改动

a) 定义了autoIncrement, 关联

b) 将TIMESTAMP(26) 改为TIMESTAMP

<?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.5.xsd"> <property name="now" value="now()" dbms="mysql,h2"/>
<property name="now" value="current_timestamp" dbms="postgresql"/>
<property name="now" value="sysdate" dbms="oracle"/> <property name="autoIncrement" value="true" dbms="mysql,h2,postgresql,oracle"/> <changeSet author="kd (generated)" id="1529903520054-1">
<createTable tableName="customers">
<column name="ID" type="BIGINT" autoIncrement="${autoIncrement}">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="NAME" type="VARCHAR(15)">
<constraints nullable="false"/>
</column>
<column name="EMAIL" type="VARCHAR(128)">
<constraints nullable="false"/>
</column>
<column name="PASSWORD" type="VARCHAR(8)">
<constraints nullable="false"/>
</column>
<column name="PHONE" type="INT"/>
<column name="ADDRESS" type="VARCHAR(255)"/>
<column name="SEX" type="CHAR(1)"/>
<column name="IS_MARRIED" type="BIT(1)"/>
<column name="DESCRIPTION" type="TEXT"/>
<column name="IMAGE" type="BLOB"/>
<column name="BIRTHDAY" type="date"/>
<column name="REGISTERED_TIME" type="TIMESTAMP"/>
</createTable>
</changeSet> <changeSet id="00000000000000-05" author="shj">
<sqlFile path="conf/liquibase/preloaddata/dml.sql"/>
</changeSet>
</databaseChangeLog>

(3) dml.sql

Insert into mybatis.CUSTOMERS (NAME,EMAIL,PASSWORD, PHONE,  ADDRESS,SEX,IS_MARRIED,DESCRIPTION,IMAGE,BIRTHDAY,REGISTERED_TIME)
values ('customer','customer@customer.com','1234',123,'customer address','女',1,'customer description',null,now(),now());

执行结果

alter table mybatis.CUSTOMERS add test varchar(25);

再次update...

6. 根据数据库反向生成changeLog文件

执行 mvn liquibase:dbDoc

最常用的命令说明:

update(将xml的改变更新到数据库)

rollback(回滚到某一版本或者某一时刻,必须要带上rollbackTag参数)

dbDoc (生成数据库文档)

dropAll(慎用,清空当前数据库,包括liquibase的版本信息)

generateChangeLog(根据数据库反向生成changeLog文件)

tag(为当前数据库打上标签)

liquibase使用的更多相关文章

  1. liquibase的使用

    前言 liquibase是一个数据库持续集成插件.独立于数据库存在,oracle,mysql,db2,h2,sql server,postgresql都能使用.它使用配置文件来更新数据库结构,并加入版 ...

  2. Spring3+Mybatis3+Mysql+ivy+liquibase

    Spring3+Mybatis3+Mysql+ivy+liquibase 集成 近一周时间所学技术:整合Spring+MyBatis+MySql+ivy+liquibase Mybatis:是一个基于 ...

  3. liquibase之快速入门

    第一步: 创建一个Changelog File: 这个database  Changelog file列举了数据库中所有的改变情况,该文件是以xml为基础的,下面是一个空的xml文件: <?xm ...

  4. Liquibase的简单使用

    LiquiBase是一个用于数据库重构和迁移的开源工具,通过日志文件的形式记录数据库的变更,然后执行日志文件中的修改,将数据库更新或回滚到一致的状态.它的目标是提供一种数据库类型无关的解决方案,通过执 ...

  5. [心得] 如何利用liquibase進行資料庫版本控制 - 實際練習

    透過上一篇的基本觀念介紹,希望大家應該有一點點感覺了! 這篇我們就來做個簡單的版本演練,加深印象吧! 我使用的環境如下 System : Windows 7 Database : SQL Server ...

  6. flyway和liquibase的使用样例

    在代码上我们有svn和git等诸多的版本控制方法. 但是在数据库上却没有相应的工具.一度导致多环境见的数据库同步难以维持. flyway和liquibase都是常见的数据库版本控制工具. flyway ...

  7. [心得] 如何利用liquibase進行資料庫版本控制 - 基礎觀念

    前言 - 會寫這篇除了是要記錄一下使用的過程之外,也是發現到網路上找來的教學幾乎都是跟其它環境做結合 比較沒有單純利用command進行的流程.也沒有整體觀念的介紹,所以將我所理解的整理分享給大家. ...

  8. LiquiBase 学习

    preconditions mysql database is installed maven has been setted up properly add depedenceies apply p ...

  9. Liquibase使用入门

    1.LiquiBase简介 LiquiBase是一个用于数据库重构和迁移的开源工具,通过日志文件的形式记录数据库的变更,然后执行日志文件中的修改,将数据库更新或回滚到一致的状态.LiquiBase的主 ...

  10. eclipse liquibase 插件

    http://marketplace.eclipse.org/category/free-tagging/liquibase http://marketplace.eclipse.org/market ...

随机推荐

  1. fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突——我的解决方案

    本文转载于:http://blog.csdn.net/tfy1028/article/details/8660823 win7 下,安装的VS2010,然后搭配opencv2.4.3运行,报错为:fa ...

  2. bzoj-4887-dp+矩阵快速幂

    4887: [Tjoi2017]可乐 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 247  Solved: 170[Submit][Status][D ...

  3. 牛客网——F小牛再战(博弈,不懂)

    链接:https://www.nowcoder.net/acm/contest/75/F来源:牛客网 题目描述 共有N堆石子,已知每堆中石子的数量,两个人轮流取石子,每次只能选择N堆石子中的一堆取一定 ...

  4. 【hive】cube和rollup函数

    cube 数据立方体(Data Cube),是多维模型的一个形象的说法.(关于多维模型这里不讲述,在数据仓库设计过程中还挺重要的,有兴趣自行查阅) 立方体其本身只有三维,但多维模型不仅限于三维模型,可 ...

  5. 【LeetCode 8_字符串_实现】String to Integer (atoi)

    , INVALID}; int g_status; long long SubStrToInt(const char* str, bool minus) { ; : ; while (*str != ...

  6. Javascript实现重力弹跳拖拽运动效果

    声明: By:GenialX 个人主页:胡旭博客 - www.ihuxu.com QQ:2252065614 演示地址: http://www.ihuxu.com/project/gcdmove/ 调 ...

  7. C# 后台线程更新UI控件

    /********************************************************************************* * C# 后台线程更新UI控件 * ...

  8. 2018c语言第2次作业

    1 删除字符串中数字字符 1.设计思路 (1)主要描述题目算法 第一步:先用for循环比较每个数是否符合删除条件. 第二步:如果符合就把这个数利用交换把这个数提前一位. 2.实验代码 void del ...

  9. 使用rollup 开发专业js library

    rollup 是一个不错的javascript 模块打包器,一般我们用来构建library 安装 npm install -g rollup 参考集成jquey && shortid ...

  10. Rails Cookie和session使用

    Rails通过cookies方法来操作cookie.这和session的操作有点相似 class CommentsController < ApplicationController def n ...