第三篇: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 演变而来的,官方是支持 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启动日志

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

日志表

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

SQL执行记录

原文地址:https://www.cnblogs.com/jianliang-Wu/p/8945316.html