一、使用Liquibase的原因
不依赖于特定的数据库,目前支持包括Oracle/Sql Server/DB2/MySql/Sybase/PostgreSQL/Caché等12种数据库,这样在数据库的部署和升级环节可帮助应用系统支持多数据库。
提供数据库比较功能,比较结果保存在XML中,基于该XML你可用Liquibase轻松部署或升级数据库。
以XML存储数据库变化,其中以作者和ID唯一标识一个变化(ChangSet),支持数据库变化的合并,因此支持多开发人员同时工作。
提供变化应用的回滚功能,可按时间、数量或标签(tag)回滚已应用的变化。通过这种方式,开发人员可轻易的还原数据库在任何时间点的状态。
可生成数据库修改文档(HTML格式)
提供数据重构的独立的IDE和Eclipse插件
二、环境配置
加入依赖:
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
在application.properties中加入如下配置:
\# liquibase 主配置文件的路径,spring boot1.5版本
liquibase.change-log=classpath:config/liquibase/master.xml
\# liquibase 主配置文件的路径,spring boot2.0版本需再前面加上spring前缀
\#spring.liquibase.change-log=classpath:config/liquibase/master.xml
liquibase.user=xxx
liquibase.password=xxx
liquibase.url=你的数据库连接
\# 如果配置为true,则会每次执行时都会把对应的数据库drop掉,默认为false
liquibase.drop-first=false
三、使用方法
在主配置文件路径下的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.4.xsd">
<include file="classpath:config/liquibase/table.xml" relativeToChangelogFile="false"/>
</databaseChangeLog>
在主配置文件中配置其他配置文件的路径,其中relativeToChangelogFile = false,说明配置的file路径为绝对路径,不需要通过相对路径去查找文件
config/liquibase/table.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">
<property name="now" value="now()" dbms="mysql"/>
<changeSet id="20171202210" author="WATER">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="tb_member"/>
</not>
</preConditions>
<createTable tableName="tb_member">
<column name="id" type="bigint" remarks="主键" >
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="mobile" type="varchar(50)" remarks="手机号" >
<constraints nullable="false" />
</column>
<column name="real_name" type="varchar(50)" remarks="姓名" >
<constraints nullable="false" />
</column>
<column name="introduce" type="varchar(255)" remarks="说明" >
<constraints nullable="false" />
</column>
<column name="status" type="varchar(10)" remarks="账号状态" />
<column name="created_by" type="varchar(50)" remarks="创建人" >
<constraints nullable="false"/>
</column>
<column name="created_date" type="timestamp" defaultValueDate="${now}" remarks="创建时间" >
<constraints nullable="false"/>
</column>
<column name="last_modified_by" type="varchar(50)"/>
<column name="last_modified_date" type="timestamp"/>
</createTable>
</changeSet>
</databaseChangeLog>
启动spring boot系统后,数据库中会出现三个表,内容如下:
1.databasechangelog –> liquibase 自动创建,用于保存每次变更的记录
2.databasechangeloglock–> liquibase 自动创建
3.tb_member —> 我们创建
注:要多次修改数据库表格时,因为liquibase的存储数据库存储功能,配置文件的 中的id必须不同,否则不能进行修改,而且会报错
要实现liquibase的其他功能时,可以使用liquibase的插件,使用命令行操作,常用的命令行说明:
update(将xml的改变更新到数据库)
rollback(回滚到某一版本或者某一时刻,必须要带上rollbackTag参数)
dbDoc (生成数据库文档)
dropAll(慎用,清空当前数据库,包括liquibase的版本信息)
generateChangeLog(根据数据库反向生成changeLog文件)
tag(为当前数据库打上标签)
与上篇Mybatis的actable相比:
actable | liquibase | |
---|---|---|
配置文件格式 | 使用标签 | 使用xml文件 |
版本记录以及版本回滚 | 无 | 有 |
适用于开发人数 | 个人 | 多人 |
总的来说,catable能够做到的事情,liquibase都能够做到,但是是catable比liquibase方便,liquibase比actable功能更多