SpringBoot学习笔记(十二:Liquibase )

举报
三分恶 发表于 2021/04/28 01:14:05 2021/04/28
【摘要】 文章目录 一、Liquibase 简介1、主要特点:2、使用方式3、变更集合 二、SpringBoot整合Liquibase1、依赖2、配置2、master.xml3、test.xml4、运行效果 一、Liquibase 简介 LiquiBase(从 2006 年开始投入使用)是一种免费开源的工具,可以实现不同数据库版本之间的...



一、Liquibase 简介


在这里插入图片描述

LiquiBase(从 2006 年开始投入使用)是一种免费开源的工具,可以实现不同数据库版本之间的迁移。本质上是一个执行sql脚本的工具。

1、主要特点:

  • 支持几乎所有主流的数据库,如MySQL、PostgreSQL、Oracle、Sql Server、DB2等
    • 支持多开发者的协作维护;
    • 日志文件支持多种格式;如XML、YAML、SON、SQL等
    • 支持多种运行方式;如命令行、Spring 集成、Maven 插件、Gradle 插件等

2、使用方式

要开始使用 LiquiBase,需要以下四个步骤:

  • 创建一个数据库 变更日志(change log)文件。
  • 在变更日志文件内部创建一个 变更集(change set)。
  • 通过命令行或构建脚本对数据库运行变更集。
  • 检验数据库中的变更。

3、变更集合

随着新特性添加到了应用程序中,经常需要变更数据库的结构或修改表约束。LiquiBase 提了超过 30 种数据库重构支持。

例如:

  • 添加列
<changeSet id="4" author="joe"> 
 <addColumn tableName="distributor"> <column name="phonenumber" type="varchar(255)"/> 
 </addColumn> 
</changeSet>

  
 
  • 创建表
<changeSet id="3" author="betsey"> 
 <createTable tableName="distributor"> <column name="id" type="int"> <constraints primaryKey="true" nullable="false"/> </column> <column name="name" type="varchar(255)"> <constraints nullable="false"/> </column> <column name="address" type="varchar(255)"> <constraints nullable="true"/> </column> <column name="active" type="boolean" defaultValue="1"/> 
 </createTable> 
</changeSet>

  
 

二、SpringBoot整合Liquibase


1、依赖

springboot内置了对liquibase整合的支持,只需要在项目中引入liquibase的依赖,进行配置即可:

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>

  
 

依赖 spring-boot-starter-jdbc 目的是为了让 liquibase 能够获得 datasource。


2、配置

appllication.properties:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/liquibase_test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
# spring.liquibase.enabled=true
spring.liquibase.change-log=classpath:/liquibase/master.xml
#  spring.liquibase.change-log=classpath:/liquibase/changelog-master.yaml

  
 

更多配置:

  • spring.liquibase.change-log 配置文件的路径,默认值为 classpath:/db/changelog/db.changelog-master.yaml
  • spring.liquibase.check-change-log-location 检查 change log的位置是否存在,默认为true.
  • spring.liquibase.contexts 用逗号分隔的运行环境列表。
  • spring.liquibase.default-schema 默认数据库 schema
  • spring.liquibase.drop-first 是否先 drop schema(默认 false)
  • spring.liquibase.enabled 是否开启 liquibase(默认为 true)
  • spring.liquibase.password 数据库密码
  • spring.liquibase.url 要迁移的JDBC URL,如果没有指定的话,将使用配置的主数据源.
  • spring.liquibase.user 数据用户名
  • spring.liquibase.rollback-file 执行更新时写入回滚的 SQL文件

2、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:liquibase/change_log/test.xml" relativeToChangelogFile="false"/>

</databaseChangeLog>

  
 

官方是支持 xml,yaml,json 三种格,这里也可以写成yaml格式或者json格式,配置文件里修改 spring.liquibase.change-log 即可。官方对比: https://www.liquibase.org/documentation/changes/sql_file.html

databaseChangeLog:
  # 支持 yaml 格式的 SQL 语法
  - changeSet: id: 1 author: Levin changes: - createTable: tableName: person columns: - column: name: id type: int autoIncrement: true constraints: primaryKey: true nullable: false - column: name: first_name type: varchar(255) constraints: nullable: false - column: name: last_name type: varchar(255) constraints: nullable: false - changeSet: id: 2 author: Levin changes: - insert: tableName: person columns: - column: name: first_name value: Marcel - column: name: last_name value: Overdijk
  
 

3、test.xml

在test.xml中写了建表的集合:

<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"/> <changeSet id="init-schema" author="tianshouzhi" > <comment>init schema</comment> <createTable tableName="user"> <column name="id" type="bigint" autoIncrement="${autoIncrement}"> <constraints primaryKey="true" nullable="false"/> </column> <column name="nick_name" type="varchar(255)"> <constraints  nullable="false"/> </column> <column name="email" type="varchar(255)"> <constraints  nullable="false"/> </column> <column name="register_time" type="timestamp"  defaultValueComputed="CURRENT_TIMESTAMP"> <constraints nullable="false"/> </column> </createTable> <modifySql dbms="mysql"> <append value="ENGINE=INNODB DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci"/> </modifySql> </changeSet>
</databaseChangeLog>

  
 

4、运行效果

运行主类,从日志中可以看到Liquibase 在帮我们执行定义好的SQL,如果是第一次启动,那么数据库会存在databasechangelog 和 databasechangeloglock两个表

在这里插入图片描述


test.xml中的sql是创建了一个user表:

在这里插入图片描述




本文为学习笔记类博客,学习资料来源见参考!



参考:

【1】:数据库版本管理工具Liquibase
【2】:让开发自动化实现自动化数据库迁移
【3】:3.5 springboot与liquibase整合
【4】:Liquibase官方文档
【5】:一起来学SpringBoot | 第二十四篇:数据库管理与迁移(Liquibase)

文章来源: blog.csdn.net,作者:三分恶,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/sinat_40770656/article/details/105315790

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。