第三篇: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.尽量避免在数据插入.删除和更新异常 例如:有一张设计不得当的学生选 ...
随机推荐
- bzoj1123 [POI2008]BLO——求割点子树相乘
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1123 思路倒是有的,不就是个乘法原理吗,可是不会写...代码能力... 写了一堆麻麻烦烦乱七 ...
- selenium3 + python - expected_conditions判断元素
expected_conditions 类 title_is: 判断当前页面的title是否完全等于(==)预期字符串,返回布尔值 title_contains : 判断当前页面的title是否包含预 ...
- selenium3 + Python - 处理浏览器弹窗(转载)
作者:Real_Tino 转载链接:https://blog.csdn.net/real_tino/article/details/59068827 我们在浏览网页时经常会碰到各种花样的弹窗,在做UI ...
- 有关于dict(字典)的特性与操作方法
有关于dict(字典)的特性与操作方法 1.字典的特性 语法: dic = {key1 : value1,key2 : value2,key3 : value3............} 注:字典中k ...
- Quartz实现执行任务记录数据库,方便计算任务的执行次数以及成功次数
任务执行实体 /** * 任务执行情况详情 */ public class JobExecuteDetail implements Serializable{ /** * */ private sta ...
- .net core2.0 中使用aspectcore实现aop
一.新建一个web application项目 1.1.添加AspectCore.Extensions.DependencyInjection引用 二.实现AbstractInterceptorAtt ...
- 关于jquery的clone()和javascript的cloneNode()
区别: jquery的clone( ),如果是true就是深克隆,把事件都会克隆过去:如果是false,则仅仅克隆的是结构: javascript的cloneNode( ),如果是true,会将子节点 ...
- Leetcode0002--Add Two Numbers 链表求和
[转载请注明]http://www.cnblogs.com/igoslly/p/8672467.html 来看一下题目: You are given two non-empty linked list ...
- VHDL之concurrent之generate
GENERATE It is another concurrent statement (along with operators and WHEN). It is equivalent to the ...
- SQL Server对数据进行添加
下面介绍一下SQL Server如何往数据库中加入数据. <!-- 添加主人信息 --> <form action="add_Admin.jsp" method= ...