SpringBoot学习笔记(十二:Liquibase )

网友投稿 966 2022-05-30

文章目录

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

例如:

添加列

1

2

3

4

5

创建表

1

2

3

4

5

6

7

8

9

10

11

12

13

14

二、SpringBoot整合Liquibase

1、依赖

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

org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java

1

2

3

4

5

6

7

8

9

10

11

12

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

1

2

3

4

5

6

7

更多配置:

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

1

2

3

4

5

6

7

8

9

10

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

3、test.xml

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

init schema

1

2

3

4

5

6

7

8

9

10

11

12

13

SpringBoot学习笔记(十二:Liquibase )

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

4、运行效果

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

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

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

参考:

【1】:数据库版本管理工具Liquibase

【2】:让开发自动化实现自动化数据库迁移

【3】:3.5 springboot与liquibase整合

【4】:Liquibase官方文档

【5】:一起来学SpringBoot | 第二十四篇:数据库管理与迁移(Liquibase)

Spring Boot 数据库

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:从零开始造Spring05---实现spring注解-1
下一篇:ModelArts模型导入tensorflow frozenGraph格式模型
相关文章