该笔记记录了springboot整合liquibase之后,如何根据liquibase ChangeLogFile对数据库进行修改以及回滚操作

参考:

baeldung.com

JHipster

1. 利用changeLog文件对数据库进行修改

  1. 引入liquibase依赖
  2. 在resource目录下新建db.changelog-master.xml作为变更集文件
  3. 修改application加入liquibase的配置,主要配置变更集文件位置
  4. 运行项目,即可根据变更集文件的内容对数据库进行修改

master.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:pro="http://www.liquibase.org/xml/ns/pro" 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/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-3.10.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.10.xsd">
<changeSet author="chunmiaoz (generated)" id="1604474279717-1">
<createTable tableName="user">
<column name="id" type="BIGINT">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="phone" type="VARCHAR(11)"/>
<column name="username" type="VARCHAR(255)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
</databaseChangeLog>

2. 从现有数据库表生成changeLog文件

  1. 在数据库中建表
  2. 新建一个liquibase.properties并配置数据库地址、用户名密码、驱动
  3. 在有liquibase.properties的目录下打开cmd,输入命令
liquibase --changeLogFile=dbchangelog.xml generateChangeLog

会自动生成一个dbchangelog.xml的目标文件

Liquibase可根据目标文件后缀名生成对应类型的变更集文件,如.yaml会生成yaml格式的

liquibase.properties:

# Enter the path for your changelog file.
changeLogFile=dbchangelog.xml #### Enter the Target database 'url' information ####
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC&createDatabaseIfNotExist=true # Enter the username for your Target database.
username: root # Enter the password for your Target database.
password: 123456

3. 从使用JPA导出的jar包生成changeLog文件

  1. 新建一个项目,引入jpa依赖
  2. 修改application,配置jpa的ddl-auto属性为create
  3. 新建实体类
  4. 将项目打包成jar文件
  5. 在jar文件目录下,新建liquibase.properties并配置相应的属性
  6. 在该目录打开cmd,输入命令
Liquibase --logLevel=INFO --defaultsFile=liquibase.properties generateChangeLog

即可自动生成dbchangelog.xml文件

Liquibase.properties:

# jar包地址
classpath=liquibase-demo-0.0.1-SNAPSHOT.jar # Enter the path for your changelog file.
changeLogFile=dbchangelog.xml #### Enter the Target database 'url' information ####
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC&createDatabaseIfNotExist=true # Enter the username for your Target database.
username: root # Enter the password for your Target database.
password: 123456

4. 回滚Liquibase生成的数据库

利用liquibase的命令

liquibase update

生成的数据库表单,可以利用命令使数据库回滚

liquibase rollbackCount 1 //回滚一条记录
liquibase rollbackToDate YYYY-MM-DD HH:MM:SS //回滚到指定日期

5. Springboot集成的liquibase回滚操作

通过借鉴jhipster的liquibase配置结构完成此功能

前提:liquibase回滚时,要求databasechangelog和更改集文件一致。

  1. 在项目中,resource目录下配置master.xml主变更集文件,目的是将子更改集include进来
<?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:pro="http://www.liquibase.org/xml/ns/pro" 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/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-3.10.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.10.xsd">
<include file="db/changelog/202011051610-added-person.xml" relativeToChangelogFile="false"></include>
</databaseChangeLog>
  1. 新建202011051610-added-person.xml子变更集文件,位置为src\main\resources\db\changelog\db.changelog-master.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:pro="http://www.liquibase.org/xml/ns/pro" 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/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-3.10.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.10.xsd">
<changeSet author="chunmiaoz (generated)" id="1604474279717-1">
<createTable tableName="user">
<column name="id" type="BIGINT">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="phone" type="VARCHAR(11)"/>
<column name="username" type="VARCHAR(255)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet> <changeSet id="202011041617-added-person" author="Chunmiao">
<createTable tableName="person">
<column name="id" type="INT">
<constraints primaryKey="true" nullable="false" unique="true" ></constraints>
</column>
<column name="name" type="VARCHAR(255)">
<constraints nullable="false"></constraints>
</column>
<column name="address" type="VARCHAR(255)"></column>
<column name="city" type="VARCHAR(255)"></column>
</createTable>
</changeSet> <changeSet id="202011041621-added-company" author="Chunmiao">
<createTable tableName="company">
<column name="id" type="INT">
<constraints primaryKey="true" nullable="false" unique="true"></constraints>
</column>
<column name="name" type="VARCHAR(255)">
<constraints nullable="false"></constraints>
</column>
<column name="address" type="VARCHAR(255)"></column>
<column name="city" type="VARCHAR(255)"></column>
</createTable>
</changeSet> <changeSet id="202011041621-alert-person" author="Chunmiao">
<addColumn tableName="person">
<column name="country" type="VARCHAR(2)">
</column>
</addColumn> <rollback>
<dropColumn tableName="person" columnName="country"></dropColumn>
</rollback>
</changeSet>
</databaseChangeLog>
  1. 修改pom.xml,为maven增加liquibase插件
        <plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<configuration>
<changeLogFile>${basedir}/src/main/resources/db/changelog/db.changelog-master.xml</changeLogFile>
<url>jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8</url>
<username>root</username>
<password>123456</password>
</configuration>
</plugin>
</plugins>
  1. 运行项目,可以发现liquibase会自动创建数据库表
  2. 执行回滚操作,在命令行中利用maven命令

    mvn liquibase:rollback -Dliquibase.rollbackCount=1
  3. 利用maven执行变更集任务可以使用命令

    mvn liquibase:update

已知问题:

在springBoot中,由于liquibase update是由springBoot完成的,手动在命令行中对该changeLog执行回滚操作,虽然会提示成功,但是数据库中的数据实际没有发生回滚。

原因:

springBoot中changelogFile位置为classpath:db/changelog/db.changelog-master.xml

而在命令行中,无法将文件位置表示为classpath:的形式,文件名字不同会被liquibase认为是不同的版本管理

解决方法:

将changeSet定义在子变更集中,在主变更集中引入子变更集,这样不管在什么环境里,changeSet的路径都被表示为在主变更集中配置的一样,从而避免了路径不同的冲突

Liquibase+SpringBoot的简单使用笔记!update+rollback的更多相关文章

  1. Element ui结合springboot的简单实战

    Eelment UI简单实战 前端开发 1 创建项目,导入element ui(略) 2 大致设计出想要的效果,如下 3 创建包 根据设计的大致模样在项目的components中创建对应的包,方便以后 ...

  2. 3.2 配置构建Angular应用——简单的笔记存储应用

    本节我们会通过构建一个简单的笔记存储应用(可以载入并修改一组简单的笔记)来学习如何应用Angular的特性.这个应用用到的特性有: 在JSON文件中存储笔记 展示.创建.修改和删除笔记 在笔记中使用M ...

  3. Log4j简单学习笔记

    log4j结构图: 结构图展现出了log4j的主结构.logger:表示记录器,即数据来源:appender:输出源,即输出方式(如:控制台.文件...)layout:输出布局 Logger机滤器:常 ...

  4. SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能

    在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...

  5. SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证

    整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...

  6. SpringBoot整合Mybatis注解版---update出现org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1, param2]

    SpringBoot整合Mybatis注解版---update时出现的问题 问题描述: 1.sql建表语句 DROP TABLE IF EXISTS `department`; CREATE TABL ...

  7. springboot+thymeleaf简单使用

    关于springboot想必很多人都在使用,由于公司项目一直使用的是SpringMVC,所以自己抽空体验了一下springboot的简单使用. 环境搭建 springbooot的环境搭建可以说很灵活, ...

  8. Linux——帮助命令简单学习笔记

    Linux帮助命令简单学习笔记: 一: 命令名称:man 命令英文原意:manual 命令所在路径:/usr/bin/man 执行权限:所有用户 语法:man [命令或配置文件] 功能描述:获得帮助信 ...

  9. Springboot接口简单实现生成MySQL插入语句

    Springboot接口简单实现调用接口生成MySQL插入语句 在实际测试中,有这样一个需求场景,比如:在性能压力测试中,可能需要我们事先插入数据库中一些相关联的数据. 我们在实际测试中,遇到问题,需 ...

随机推荐

  1. Python练习题 030:Project Euler 002:偶数斐波那契数之和

    本题来自 Project Euler 第2题:https://projecteuler.net/problem=2 # Each new term in the Fibonacci sequence ...

  2. spring-boot-route(五)整合Swagger生成接口文档

    目前,大多数公司都采用了前后端分离的开发模式,为了解决前后端人员的沟通问题,后端人员在开发接口的时候会选择使用swagger2来生成对应的接口文档,swagger2提供了强大的页面调试功能,这样可以有 ...

  3. Dell XPS 7590 Hackintosh

    网上主流引导Hackintosh的工具有Chameleon, Clover和OpenCore. 但是随着Hackintosh重要驱动开发团队acidanthera逐渐转向OpenCore,后者显然才是 ...

  4. 学习git这一篇就够了!!!

    git命令操作 本地库操作 初始化本地仓库 初始化命令 git init $ work % cd workspace $ workspace % mkdir WebService //创建文件夹 $ ...

  5. Java知识系统回顾整理01基础05控制流程08综合练习

    一.练习--黄金分割点 题目: 寻找某两个数相除,其结果 离黄金分割点 0.618最近 分母和分子不能同时为偶数 分母和分子 取值范围在[1,20] (即1到20) 要求效果: public clas ...

  6. visual studio 2015 Opencv4.0.1配置

    最近由于工作需要,要配置opencv,我的电脑vs的version是2015,在网上下载了最新的opencv 4.0.1 自己摸索总是很困难,网上的例子也比较多,但版本比较低,也不确定适不适合vs20 ...

  7. HTML & CSS & JavaScript 从一个表格到一个灰阶颜色表 03

    工具1:HBuilder X 1.9.9.20190522 工具2:火狐浏览器 67.0.4 (64 位) 其实,我还想使用表格,做一个这样的颜色表,如下图所示: 如果按照之前的做法,把每一种颜色都列 ...

  8. LiteOS-任务篇-源码分析-系统启动函数

    目录 前言 链接 参考 开启调度 LOS_Start 函数源码 osTickStart 函数源码 LOS_StartToRun 函数源码 前言 20201009 移植好内核后,开始实战内核. 源码分析 ...

  9. 解决SpringBoot 定时计划 quartz job 任务重复执行多次(10次)

    上一篇:SpringBoot多任务Quartz动态管理Scheduler,时间配置,页面+源 设置了多个 任务,本应该是各司其职的,任务调用多线程处理任务,but这个定时任务竟然同时跑了10次???如 ...

  10. 多测师讲解selenium_运行报告相出错归纳_高级讲师肖sir

    <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'> EETraceback (most recent c ...