第三篇:SpringBoot - 数据库结构版本管理与迁移
SpringBoot支持了两种数据库结构版本管理与迁移,一个是flyway,一个是liquibase。其本身也支持sql script,在初始化数据源之后执行指定的脚本,本章是基于 Liquibase 开展…
- Liquibase
开发人员将本地开发机器上的基于文本的文件中的数据库更改存储在本地数据库中。Changelog文件可以任意嵌套,以便更好地管理,每个变更集通常包含描述应用于数据库的更改/重构的更改。Liquibase支持对支持的数据库和原始SQL生成SQL的描述性更改。通常,每个变更集应该只有一个更改,以避免可能导致数据库处于意外状态的自动提交语句失败。
官方文档:http://www.liquibase.org/documentation/index.html
- 使用
在平时开发中,无可避免测试库增加字段或者修改字段以及创建表之类的,环境切换的时候如果忘记修改数据库那么肯定会出现
不可描述的事情,这个时候不妨考虑考虑Liquibase,闲话不多说,上代码
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<groupId>com.battcn</groupId>
<artifactId>battcn-boot-liquibase</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<mybatis-plugin.version>1.1.1</mybatis-plugin.version>
<mybatis-spring-boot.version>1.3.0</mybatis-spring-boot.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 数据库连接池,类似阿里 druid -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot.version}</version>
</dependency>
<!-- liquibase -->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
<!--<scope>runtime</scope>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
- application.yml
spring:
application:
name: battcn-boot-liquibase
datasource:
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/liquibase?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
#我这里使用的是默认路径,如果默认路径其实可以不配置,有兴趣可以看 LiquibaseProperties 的源代码
liquibase:
change-log: classpath:db/changelog/db.changelog-master.yaml
- liquibase.change-log 配置文件的路径,默认值为classpath:/db/changelog/db.changelog-master.yaml
- liquibase.check-change-log-location 是否坚持change log的位置是否存在,默认为true.
- liquibase.contexts 逗号分隔的运行时context列表.
- liquibase.default-schema 默认的schema.
- liquibase.drop-first 是否首先drop schema,默认为false
- liquibase.enabled 是否开启liquibase,默认为true.
- liquibase.password 目标数据库密码
- liquibase.url 要迁移的JDBC URL,如果没有指定的话,将使用配置的主数据源.
- liquibase.user 目标数据用户名
- db.changelog-master.yaml
databaseChangeLog:
- 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 - changeSet:
id: 3
author: Levin
changes:
- sqlFile:
encoding: utf8
path: classpath:db/changelog/sqlfile/test1.sql
- test1.sql
INSERT INTO `person` (`id`, `first_name`, `last_name`) VALUES ('2', '嘻嘻', '谔谔');
上面的yaml文件其实就是从下面的XML 演变而来的,官方是支持 xml,yaml,json 三种格式,写法也比较简单
传送门(官方给出了三种写法格式,依样画葫芦就可以了):http://www.liquibase.org/documentation/changes/sql_file.html
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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-2.0.xsd">
<changeSet id="1" author="Levin">
<sqlFile path="classpath:db/changelog/changelog/test1.sql"/>
</changeSet>
</databaseChangeLog>
- Application.java
package com.battcn; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* @author Levin
* @date 2017-08-19.
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 测试
1.启动Application.java中的main方法

从日志中可以看到Liquibase 在帮我们执行定义好的SQL,如果是第一次启动,那么数据库会存在databasechangelog 和 databasechangeloglock两种表,从名字就可以看出,故而不作过多解释

2.SQL中的语法是创建一张person表和 两次 INSERT 操作

第三篇:SpringBoot - 数据库结构版本管理与迁移的更多相关文章
- “MVC+Nhibernate+Jquery-EasyUI”信息发布系统 第二篇(数据库结构、登录窗口、以及主界面)
“MVC+Nhibernate+Jquery-EasyUI”信息发布系统 第二篇(数据库结构.登录窗口.以及主界面) 一.在上一篇文章中,主要说的就是把主框架搭建起来,并且Nhibernate能达到增 ...
- “MVC+Nhibernate+Jquery-EasyUI”信息发布系统 第二篇(数据库结构、登录窗口、以及主界面)
一.在上一篇文章中,主要说的就是把主框架搭建起来,并且Nhibernate能达到增删改查的地步.测试好之后再来看这篇文章,我的主框架相对来说简答一点,重点还是实现系统的功能,以及对Jquery-Eas ...
- ASP.NET MVC4 新手入门教程特别篇之一----Code First Migrations更新数据库结构(数据迁移)修改Entity FrameWork 数据结构(不删除数据)
背景 code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建(DropCreateDatabaseIfModelChanges),此时就会产生一个问题,当我们的 ...
- 第三篇 SpringBoot整合log4j2详解
源代码:https://pan.baidu.com/s/1d1Lwv1gIvVNltIKVWeEseA 提取码:wff0 SpringBoot整合Log4j2步骤: 1.删除spring-boot-s ...
- 第三篇 -- SpringBoot打包成jar包
本篇介绍怎么将SprintBoot项目打包成jar包. 第一步:点击IDEA右侧的maven. 第二步:双击package,然后就会开始打包,当出现build success时,就打包成功了,一般在t ...
- Code First Migrations更新数据库结构(数据迁移)
背景 code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建 (DropCreateDatabaseIfModelChanges),此时就会产生一个问题,当我们 ...
- 使用Code first 进行更新数据库结构(数据迁移)
CodeFirst 背景 code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建(DropCreateDatabaseIfModelChanges),此时就会 ...
- Code First Migrations更新数据库结构(数据迁移) 【转】
注意:一旦正常后,每次数据库有变化,做如下两步: 1. Enable-Migrations 2.update-database 背景 code first起初当修改model后,要持久化至数据库中时, ...
- MySQL系列(三)--数据库结构优化
良好的数据库逻辑设计和物理设计是数据库高性能的基础,所以对于数据库结构优化是很有必要的 数据库结构优化目的: 1.减少数据的冗余 2.尽量避免在数据插入.删除和更新异常 例如:有一张设计不得当的学生选 ...
随机推荐
- 【POJ 3460】 Booksort
[题目链接] http://poj.org/problem?id=3460 [算法] IDA* 注意特判答案为0的情况 [代码] #include <algorithm> #include ...
- C++调用shell脚本
调用函数时候,传入脚本路径名称或者具体命令. int shell_call(std::string &cmdstr) { }; char line[maxline]; FILE *fpin; ...
- Coursera Algorithms week3 归并排序 练习测验: Merging with smaller auxiliary array
题目原文: Suppose that the subarray a[0] to a[n-1] is sorted and the subarray a[n] to a[2*n-1] is sorted ...
- go之switch
switch 条件语句一 格式 switch initialization{ case v1: // do something case v2: // do something case v2: // ...
- SyntaxError: EOL while scanning string literal的解决
2281 python中字符串的最后一个字符是斜杠会导致出错:SyntaxError: EOL while scanning string literal [背景] Python 2.7.2 中想要通 ...
- IDEA 使用 git (码市)
1.下载 git,并安装(一直下一步) 2.使用IDEA,检出项目,检出方式选择:git, 3.如果项目有修改,上传修改的文件 4.下载 SourceTree(git的图形化工具),并安装(一直下一步 ...
- matplotlib之pyplot 学习示例
现在通过numpy和matplotlib.pyplot 在Python上实现科学计算和绘图,而且和matlab极为相像(效率差点,关键是方便简单) 这里有大量plots代码例子. 1. 简单的绘图( ...
- CentOS 安装dotNetCore
如果要在CentOS上运行.net Core程序,必须安装.net Core Sdk 具体安装 方法,可以参考微软官方站点说明,非常详细: 1)百度搜索 .Net Core 2)先择CentOS版本: ...
- 酷派改变者S1(C105/C105-6/C105-8) 解锁BootLoader 并刷入recovery root
首先下载好工具链接:https://pan.baidu.com/s/1qZjOCUw 密码:u2dr 备用下载链接:https://pan.baidu.com/s/1pMlmAef 本篇教程教你如何傻 ...
- dubbo之启动时检查
启动时检查 Dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成,以便上线时,能及早发现问题,默认 check="true".所以可以通过 ...