SpringBoot学习笔记(十二:Liquibase )

一、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 种数据库重构支持。

例如:

  • 添加列
  • 创建表

二、SpringBoot整合Liquibase

=========================================================================================

1、依赖


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

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-jdbc

mysql

mysql-connector-java

依赖 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">

官方是支持 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">

init schema

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值