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 演变而来的,官方是支持 xmlyamljson 三种格式,写法也比较简单

传送门(官方给出了三种写法格式,依样画葫芦就可以了):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,如果是第一次启动,那么数据库会存在databasechangelogdatabasechangeloglock两种表,从名字就可以看出,故而不作过多解释

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

第三篇:SpringBoot - 数据库结构版本管理与迁移的更多相关文章

  1. “MVC+Nhibernate+Jquery-EasyUI”信息发布系统 第二篇(数据库结构、登录窗口、以及主界面)

    “MVC+Nhibernate+Jquery-EasyUI”信息发布系统 第二篇(数据库结构.登录窗口.以及主界面) 一.在上一篇文章中,主要说的就是把主框架搭建起来,并且Nhibernate能达到增 ...

  2. “MVC+Nhibernate+Jquery-EasyUI”信息发布系统 第二篇(数据库结构、登录窗口、以及主界面)

    一.在上一篇文章中,主要说的就是把主框架搭建起来,并且Nhibernate能达到增删改查的地步.测试好之后再来看这篇文章,我的主框架相对来说简答一点,重点还是实现系统的功能,以及对Jquery-Eas ...

  3. ASP.NET MVC4 新手入门教程特别篇之一----Code First Migrations更新数据库结构(数据迁移)修改Entity FrameWork 数据结构(不删除数据)

    背景 code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建(DropCreateDatabaseIfModelChanges),此时就会产生一个问题,当我们的 ...

  4. 第三篇 SpringBoot整合log4j2详解

    源代码:https://pan.baidu.com/s/1d1Lwv1gIvVNltIKVWeEseA 提取码:wff0 SpringBoot整合Log4j2步骤: 1.删除spring-boot-s ...

  5. 第三篇 -- SpringBoot打包成jar包

    本篇介绍怎么将SprintBoot项目打包成jar包. 第一步:点击IDEA右侧的maven. 第二步:双击package,然后就会开始打包,当出现build success时,就打包成功了,一般在t ...

  6. Code First Migrations更新数据库结构(数据迁移)

    背景 code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建 (DropCreateDatabaseIfModelChanges),此时就会产生一个问题,当我们 ...

  7. 使用Code first 进行更新数据库结构(数据迁移)

    CodeFirst 背景  code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建(DropCreateDatabaseIfModelChanges),此时就会 ...

  8. Code First Migrations更新数据库结构(数据迁移) 【转】

    注意:一旦正常后,每次数据库有变化,做如下两步: 1. Enable-Migrations 2.update-database 背景 code first起初当修改model后,要持久化至数据库中时, ...

  9. MySQL系列(三)--数据库结构优化

    良好的数据库逻辑设计和物理设计是数据库高性能的基础,所以对于数据库结构优化是很有必要的 数据库结构优化目的: 1.减少数据的冗余 2.尽量避免在数据插入.删除和更新异常 例如:有一张设计不得当的学生选 ...

随机推荐

  1. TensorFlow Lite demo——就是为嵌入式设备而存在的,底层调用NDK神经网络API,注意其使用的tf model需要转换下,同时提供java和C++ API,无法使用tflite的见后

    Introduction to TensorFlow Lite TensorFlow Lite is TensorFlow’s lightweight solution for mobile and ...

  2. Codeforces--633D--Fibonacci-ish(暴力搜索+去重)(map)

    Fibonacci-ish Time Limit: 3000MS   Memory Limit: 524288KB   64bit IO Format: %I64d & %I64u Submi ...

  3. nyoj--496--巡回赛(拓扑排序)

    巡回赛 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 世界拳击协会(WBA)是历史最悠久的世界性拳击组织,孕育了众多的世界冠军,尤其是重量级,几乎造就了大家耳熟能详的所 ...

  4. B1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 数学

    今天净做水题了,这个题还不到十五分钟就搞定了,思路特别简单,就是直接按照线性求因子个数的思路就行了. 题干: Description 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜 ...

  5. C/C++中的绝对值函数

    --------开始-------- 对于不同类型的数据对应的绝对值函数也不相同,在c和c++中分别在头文件math.h 和 cmath 中. int : x = abs( n ) double : ...

  6. Django day11(一) ajax 文件上传 提交json格式数据

    一: 什么是ajax? AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步Javascript和XML”.即使用Javascript语言与服务器进行异步交互 ...

  7. LocalDateTime查找最近的五分钟点

    /** * 最近的五分钟 * @param dateTime * @return */ public static LocalDateTime getNear5(LocalDateTime dateT ...

  8. java 微信api开发

    最近使用了一个很好的微信api框架,比较好使. 源码地址:https://github.com/chanjarster/weixin-java-tools/wiki 微信公众平台:微信公众平台开发文档 ...

  9. WebApi中对请求参数和响应内容进行URL编码解码

    项目经测试,发现从IE提交的数据,汉字会变成乱码,实验了网上很多网友说的给ajax加上contentType:"application/x-www-form-urlencoded; char ...

  10. C#方法的练习

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo ...