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. torchnet package (2)

    torchnet package (2) torchnet torch7 Dataset Iterators 尽管是用for loop语句很容易处理Dataset,但有时希望以on-the-fly m ...

  2. dva subscription的使用方法

    import { routerRedux } from 'dva/router' export default { namespace: 'notice', state: { notices:[], ...

  3. windows下的IO模型之完成端口

    本文整理于:http://blog.csdn.net/piggyxp/article/details/6922277 一. 完成端口的优点 完成端口会充分利用Windows内核来进行I/O的调度,是用 ...

  4. HDU 1241 Oil Deposits bfs 难度:0

    http://acm.hdu.edu.cn/showproblem.php?pid=1241 对每个还未访问的点bfs,到达的点都标为一块,最后统计有多少块即可 #include <cstdio ...

  5. bzoj1073

    题意: k短路 题解: A* 当然是抄了zzd的代码 然而需要特判 为什么把bool改成int爆空间!!! 代码: #include<bits/stdc++.h> using namesp ...

  6. bzoj1037

    题解: 定义f[i][j][a][b]表示已经排了i个人 还能拍j个男的(那么就还有m-i+j个是女的) 还能连续拍a个男的,b个女的 我是递推的 考虑后面一个拍男的还是女的 注意要判断边界 代码: ...

  7. Hadoop1.1.2伪分布式安装

    一.安装前准备设置Linux的静态IP修改VirtualBox的虚拟网卡地址修改主机名把hostname和ip绑定关闭防火墙:service iptables stop二.SSH免密码登陆生成秘钥文件 ...

  8. Redis学习笔记-数据操作篇(Centos7)

    一.基本操作 1.插入数据 127.0.0.1:6379> set name cos1eqlg0 OK 这样就在redis中设置了一个key-value键值对 2.查询数据 127.0.0.1: ...

  9. (转)关于fflush(stdin)清空输入缓存流(C/C++)

    来源:http://my.oschina.net/deanzhao/blog/79790 1. 为什么 fflush(stdin) 是错的?首先请看以下程序: #include <stdio.h ...

  10. 在Outlook中修改脱机文件(.ost)的保存位置

    方法一 少读者所在公司的邮箱客户端都在使用微软 Exchange Server 的“缓存 Exchange 模式”.Outlook会默认将脱机文件(.ost文件)保存在C盘上. 但很多读者不希望Out ...