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. python运行错误------Non-UTF-8 code

    1.安装-----见:https://www.cnblogs.com/weven/p/7252917.html 本文转载于:http://blog.csdn.net/youyuyixiu/articl ...

  2. UVALive-3487 Duopoly(最小割)

    题目大意:有两家公司都想向政府申请某些资源的使用权,并且他们都提供了一些申请列表,列表中含有申请费用和资源种类,同一家公司的申请列表之间不含有重复的资源.政府只可以完整地接受和拒绝谋一份申请列表,问政 ...

  3. Mybatis generator 配置

    mybatis-generator.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

  4. (C#基础)深浅拷贝理解

    一方面感觉实在无趣,不知道做什么了,纯粹来个打字练习,于是有了这个. 二方面深感自己C#基础之薄弱,于是对着园友的文章一边看,一边练习,因为很多次看了,没有多久就忘了,还有练习过程中会出现一些问题,这 ...

  5. centos7.3安装配置vsftp

    首先使用命令查看,系统内是否安装了vsftp [root@instance_290388 down]# rpm -qa |grep vsftp 如果没有安装,使用命令,进行安装 [root@insta ...

  6. resin WED服务器初用遇到的问题和解决方法 java.lang.RuntimeException: java.net.SocketException: Unrecognized Windows Socke ts error: 0: JVM_Bind

    开启resin 服务器以后提示如下:(控制台不断的循环循环打印如下错误提示) java.lang.RuntimeException: java.net.SocketException: Unrecog ...

  7. Monkey测试练习

    1.下载Android SDK 2.打开SDK Manager.exe自动下载 3.配置环境变量 将platform-tools的路径(如: C:\001myWorkspace\eclipse(MAV ...

  8. HDU1501 dfs

    像这样有维度的一定要记忆化啊........... #include<cstdio> #include<cstdlib> #include<iostream> #i ...

  9. ACdream区域赛指导赛之手速赛系列(2)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/DaiHaoC83E15/article/details/26187183        回到作案现场 ...

  10. zipkin:HttpClient和struts

    因为要和老系统集成zipkin,意外的发现老系统使用的httpClient来发送信息.zipkin的官方demo可都是retstTemplate啊!有的搞头. 在看Demo的时候意外的发现其实其实2. ...