项目结构

就是从io.spring.start下的干净的Springboot项目。
(一)创建表
CREATE TABLE `school` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) COLLATE utf8_bin DEFAULT NULL, `age` int(11) DEFAULT NULL, `title` varchar(255) COLLATE utf8_bin DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_bin

(二)配置pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.vincent</groupId>
<artifactId>liquibase</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>liquibase</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.15.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<db.driver>com.mysql.jdbc.Driver</db.driver>
<db.dropFirst>false</db.dropFirst>
<db.url>jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false</db.url>
<db.username>root</db.username>
<db.password>451179</db.password>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.5.3</version>
<dependencies>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.2-GA</version>
</dependency>
</dependencies>
<configuration>
<changeLogFile>src/main/resources/config/liquibase/master_changelog.xml</changeLogFile>
<diffChangeLogFile>
src/main/resources/config/liquibase/changelog/${maven.build.timestamp}_changelog.xml
</diffChangeLogFile>
<outputChangeLogFile>src/main/resources/config/liquibase/changelog/changelog_original.xml
</outputChangeLogFile>
<driver>${db.driver}</driver>
<url>${db.url}</url>
<dropFirst>false</dropFirst>
<defaultSchemaName/>
<username>${db.username}</username>
<password>${db.password}</password>
<referenceUrl>hibernate:spring:com.jaguar.myapp.domain?dialect=&hibernate.ejb.naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy</referenceUrl>
<verbose>true</verbose>
<logging>debug</logging>
<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
</configuration>
</plugin>
</plugins>
</build>
</project>
(三)application.yml文件
spring:
#数据库相关的配置
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false
username: root
password: root
liquibase:
enabled: true
change-log: classpath:/config/liquibase/master_changelog.xml
(四)mvn clean install
此刻会发现mvn报错了,主要错误信息如下

(五)创建这个缺少的文件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 -->
</databaseChangeLog>
(六)继续mvn clean install
此刻发现,终于可以不报错了。
(七)根据数据库反向生成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">
<changeSet author="vincent (generated)" id="1535544566470-1">
<createTable tableName="school">
<column autoIncrement="true" name="id" type="BIGINT">
<constraints primaryKey="true"/>
</column>
<column name="name" type="VARCHAR(255)"/>
<column name="age" type="INT"/>
<column name="title" type="VARCHAR(255)"/>
</createTable>
</changeSet>
</databaseChangeLog>
(四) 清空当前数据库,包括liquibase的版本信息
执行命令:mvn liquibase:dropAll
(五)将xml的改变更新到数据库
将刚刚的变更放到master_changelog.xml的目标执行中去

执行命令:mvn liquibase:update
这时候同样会报错

错误信息很明显,缺少changelog_original.xml
但是,咱们项目中不是已经存在changelog_original.xml了吗?为何还报他不存在呢?
原因很简单,因为没有编译

此刻执行mvn clean install

执行完可以发现文件编译进去了,再次执行mvn liquibase:update
此刻可以success了
我们看下数据库发送了什么变化,多了2个表DATABASECHANGELOG,DATABASECHANGELOGLOCK

至此,集成工作已经完成。
现在我们在数据库中新增一个student表
CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, `school_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=11123 DEFAULT CHARSET=utf8

将信息更新到changelog文件中去
执行命令:mvn liquibase:liquibase:dbDoc
打开changelog_original.xml文件
最常用的命令说明:
update(将xml的改变更新到数据库)
rollback(回滚到某一版本或者某一时刻,必须要带上rollbackTag参数)
dbDoc (生成数据库文档)
dropAll(慎用,清空当前数据库,包括liquibase的版本信息)
generateChangeLog(根据数据库反向生成changeLog文件)
tag(为当前数据库打上标签)
项目github地址:https://github.com/MonthsEmpty/liquibase.git
参考文章:https://blog.csdn.net/xiang__liu/article/details/80800205