Idea Spring-boot maven下使用liquibase
官方文档
https://docs.liquibase.com/tools-integrations/maven/home.html通过liquibase使用mysql
https://docs.liquibase.com/workflows/database-setup-tutorials/mysql.html?Highlight=mysql 首先在pom.xml中引入liquibase的依赖<dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> </dependency>
--driver=com.mysql.cj.jdbc.Driver --classpath=mysql-connector-java-8.0.22.jar --url="jdbc:mysql://bj-cdb-nivnaury.sql.tencentcdb.com:60991/lianmai?autoReconnect=true&useSSL=FALSE" --changeLogFile=db.changelog-1.0.xml --username=*****--password=*****
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118082616063-1486305344.png)
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118082738280-669621903.png)
driver=com.mysql.cj.jdbc.Driver classpath=mysql-connector-java-8.0.22.jar url=jdbc:mysql://bj-cdb-nivnaury.sql.tencentcdb.com:*/lianmai?autoReconnect=true&useSSL=FALSE changeLogFile=src/main/resources/db/changelog/dbChangeLog.xml outputChangeLogFile=src/main/resources/db/changelog/outputChangeLog.xml username=* password=*
生成changelog命令
https://docs.liquibase.com/commands/community/generatechangelog.html?Highlight=generateChangeLog踩坑过程
首先要配置propertyFile属性,下面是我的目录结构以及propertyFile的属性配置![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118181335099-57286820.png)
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<configuration>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
</configuration>
</plugin>
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118181232511-42424103.png)
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118182857988-1277903053.png)
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118184259226-404418346.png)
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118181659937-1459423710.png)
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118181733918-1069717034.png)
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118181933305-1937267168.png)
outputChangeLogFile=src/main/resources/db/changelog/outputChangeLog.xml
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118182355868-205867596.png)
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118182505307-1359095370.png)
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118182610224-851406106.png)
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118182751768-1464434031.png)
通过liquibase update命令使用changeLogFile文件更新数据库
修改changeLog的路径![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118184446710-499713926.png)
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118184805817-631952137.png)
- 将changeset添加到变更日志。
- changeset由author和id属性唯一标识。
- Liquibase尝试执行事务中在最后提交的每个changeset。
<changeSet id="1" author="bob"> <createTable tableName="department"> <column name="id" type="int"> <constraints primaryKey="true" nullable="false"/> </column> <column name="name" type="varchar(50)"> <constraints nullable="false"/> </column> <column name="active" type="boolean" defaultValueBoolean="true"/> </createTable> </changeSet>
CREATE TABLE "department" ("id" number (*,0), "name" VARCHAR2 (50 BYTE), "active" NUMBER (1, 0) DEFAULT 1 );
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118184908445-1101359939.png)
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118184725739-1593496359.png)
DatabaseChangeLog 和 DatabaseChangeLogLock
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118185028137-1287108950.png)
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118185222759-435614314.png)
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118185341431-1427790843.png)
![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118185427877-1188567036.png)
因为Liquibase只是从DATABASECHANGELOG表中读取数据以确定需要运行哪些变更集
所以如果对同一个数据库同时执行多个Liquibase实例,则会发生冲突。
如果多个开发人员使用同一个数据库实例,或者集群中有多个服务器在启动时自动运行Liquibase,则可能发生这种情况。
所以这个表是用来锁数据库的,避免多个开发人员同一时刻操作数据库的时候造成冲突 下图是四个字段的用途![](https://img2020.cnblogs.com/blog/1905499/202101/1905499-20210118185904190-951885785.png)
回滚 maven Rollback
官方地址 https://docs.liquibase.com/tools-integrations/maven/commands/maven-rollback.html?Highlight=rollback 三种回滚模式liquibase.rollbackCount
- 按顺序还原指定数量的变更集,从最新的变更开始,向后操作,直到达到指定的值liquibase.rollbackTag
- 还原被标记的数据库所做的所有更改liquibase.rollbackDate
- 恢复数据库所做的所有更改到指定的日期和时间
mvn liquibase:rollback -Dliquibase.rollbackCount=3
mvn liquibase:rollback -Dliquibase.rollbackTag=version1
<liquibase.tag>v3.2.1</liquibase.tag>
<liquibase.rollbackTag>${liquibase.tag}</liquibase.rollbackTag>
mvn liquibase:rollback -Dliquibase.rollbackDate=2020-11-26
论读书
睁开眼,书在面前 闭上眼,书在心里
睁开眼,书在面前 闭上眼,书在心里