本文主要是介绍SpringBoot学习笔记(十二:Liquibase ),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 一、Liquibase 简介
- 1、主要特点:
- 2、使用方式
- 3、变更集合
- 二、SpringBoot整合Liquibase
- 1、依赖
- 2、配置
- 2、master.xml
- 3、test.xml
- 4、运行效果
一、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"?>
<databaseChangeLogxmlns="http://www.liquibase.org/xml/ns/dbchangelog"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangeloghttp://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: 1author: Levinchanges:- createTable:tableName: personcolumns:- column:name: idtype: intautoIncrement: trueconstraints:primaryKey: truenullable: false- column:name: first_nametype: varchar(255)constraints:nullable: false- column:name: last_nametype: varchar(255)constraints:nullable: false- changeSet:id: 2author: Levinchanges:- insert:tableName: personcolumns:- column:name: first_namevalue: Marcel- column:name: last_namevalue: Overdijk
3、test.xml
在test.xml中写了建表的集合:
<databaseChangeLogxmlns="http://www.liquibase.org/xml/ns/dbchangelog"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangeloghttp://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)
这篇关于SpringBoot学习笔记(十二:Liquibase )的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!