liquibase特点:
支持代码分支和合并
支持多个开发人员
支持多种数据库类型
支持XML,YAML,JSON和SQL格式
支持与上下文相关的逻辑
集群安全的数据库升级
生成数据库更改文档
生成数据库“ 差异 ”
贯穿您的构建过程,嵌入您的应用程序或按需使用
自动生成用于DBA代码审查的SQL脚本
不需要实时数据库连接
liquibase配置文件结构:
master.xml内容:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <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:liquibase/changelog/table.xml" relativeToChangelogFile="false"/> <!--<include file="classpath:liquibase/changelog/data.xml" relativeToChangelogFile="true" />--> </databaseChangeLog>
table.xml文件内容(这个要根据自己需要创建的DDL或DML改写):
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <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"> <property name="autoIncrement" value="true" dbms="mysql"/> <property name="now" value="now()" dbms="mysql"/> <changeSet author="zhuyoulong" id="20180503-test" context="development"> <comment>测试</comment> <createTable tableName="department" remarks="部门"> <column name="id" type="bigint" autoIncrement="${autoIncrement}"> <constraints primaryKey="true" nullable="false"/> </column> <column name="name" type="varchar(50)" remarks="名称"> <constraints nullable="false"/> </column> <column name="title" type="varchar(50)" remarks="标题"> <constraints nullable="false"/> </column> <column name="active" type="boolean" defaultValueBoolean="true" remarks="激活"/> <column name="register_time" type="datetime" defaultValueComputed="${now}" remarks="注册时间"> <constraints nullable="false"/> </column> </createTable> <modifySql dbms="mysql"> <append value="ENGINE=INNODB DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci"/> </modifySql> </changeSet> </databaseChangeLog>
liquibase.properties:
url=xxx driver=xxx username=xxx password=xxx drop-first=false
maven中添加
1、依赖:
<!--liquibase依赖--> <dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> <version>3.5.5</version> </dependency>
2、插件(其实这个可以不要,如果手动执行可以用这个):
<plugin> <groupId>org.liquibase</groupId> <artifactId>liquibase-maven-plugin</artifactId> <version>3.5.1</version> <configuration> <!--指定数据库连接--> <propertyFile>src/main/resources/liquibase/liquibase.properties</propertyFile> <!--指定执行主文件--> <changeLogFile>src/main/resources/liquibase/master.xml</changeLogFile> <outputChangeLogFile>src/main/resources/liquibase/master.xml</outputChangeLogFile> <!-- 是否需要弹出确认框--> <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase> <!--输出文件的编码--> <outputFileEncoding>UTF-8</outputFileEncoding> <!--执行的时候是否显示详细的参数信息--> <verbose>true</verbose> <!--是否每次都重新加载properties--> <propertyFileWillOverride>true</propertyFileWillOverride> <rollbackTag>${project.version}</rollbackTag> <tag>${project.version}</tag> </configuration> </plugin>
用注解方式(笔者用这种方式):
package com.cp.config; import liquibase.integration.spring.SpringLiquibase; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; /** * * @author zhuyoulong * @date 2018年-05月-03日 * <p>Update Time: </p> * <p>Updater: </p> * <p>Update Comments: </p> */ @Configuration public class LiquibaseConfig { @Autowired @Qualifier("dataSource") private DataSource dataSource; @Bean public SpringLiquibase liquibase() { SpringLiquibase liquibase = new SpringLiquibase(); liquibase.setDataSource(espDataSource); liquibase.setChangeLog("classpath:liquibase/changelog/master.xml"); liquibase.setContexts("development,test,preproduction,production"); //如果设置为true:第一次执行不会报错,第二次将会报错,导致程序无法启动,所以第一次执行完后一定要改为:false liquibase.setShouldRun(false); return liquibase; } }
这样想起启动就会执行LiquibaseConfig.liquibase实例构造
更多资料请参考:
http://www.liquibase.org/