SpringBoot集成Liquibase

简介

上一篇mybatis-plus,讲完后,我们是不是该进入hibernate呢?不,我就不,我们这章先来看看Liquibase,这个东东,不要小看他数据库版本追溯,表迭代有它就可以了
解释:

Liquibase是一个用于跟踪、管理和应用数据库变化的开源的数据库重构工具。它将所有数据库的变化(包括结构和数据)都保存在XML文件中,便于版本控制。

Liquibase具备如下特性:

  • 不依赖于特定的数据库,目前支持包括Oracle/Sql Server/DB2/MySql/Sybase/PostgreSQL/Caché/h2等12种数据库,这样在数据库的部署和升级环节可帮助应用系统支持多数据库。
  • 提供数据库比较功能,比较结果保存在XML中,基于该XML你可用Liquibase轻松部署或升级数据库。
  • 以XML存储数据库变化,其中以作者和ID唯一标识一个变化(ChangSet),支持数据库变化的合并,因此支持多开发人员同时工作。
  • 在数据库中保存数据库修改历史(DatabaseChangeHistory),在数据库升级时自动跳过已应用的变化(ChangSet)。
  • 提供变化应用的回滚功能,可按时间、数量或标签(tag)回滚已应用的变化。通过这种方式,开发人员可轻易的还原数据库在任何时间点的状态。
  • 可生成数据库修改文档(HTML格式)
  • 提供数据重构的独立的IDE和Eclipse插件。

xml配置方式

依赖

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.liquibase</groupId>
			<artifactId>liquibase-core</artifactId>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

配置文件

#DataSource
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/gsong
spring.datasource.username = root
spring.datasource.password = 123456

#JPA
spring.jpa.properties.hibernate.hbm2ddl.auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.database = MYSQL
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.show-sql = true

spring.liquibase.change-log=classpath:db/changeLog.xml
spring.liquibase.enabled=true
spring.liquibase.drop-first=false
spring.liquibase.contexts=application

liquibase存储变化的changeLog.xml文件

<?xml version="1.0" encoding="utf-8"?>
<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-3.4.xsd">
    <changeSet id="20190813" author="gsong" >
        <preConditions onFail="MARK_RAN" onSqlOutput="TEST">
            <not>
                <tableExists tableName="myfirstgsong"></tableExists>
            </not>
        </preConditions>
        <sql>
            <![CDATA[
            CREATE TABLE myfirstgsong (
            `ID`  int NOT NULL ,
            `NAME`  varchar(255) NULL ,
            PRIMARY KEY (`ID`)
            )
            ]]>
        </sql>
    </changeSet>
</databaseChangeLog>

启动项目后将自动创建两张表用于记录迭代记录

在这里插入图片描述

配置类方式

依赖

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.liquibase</groupId>
			<artifactId>liquibase-core</artifactId>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>


配置文件

#DataSource
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/gsong?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
spring.datasource.username = root
spring.datasource.password = 123456
#JPA
spring.jpa.properties.hibernate.hbm2ddl.auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.database = MYSQL
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.show-sql = true


将application.yml liquibase配置部分通过代码配置

@Configuration
public class LiquibaseConfig
{
    @Bean
    public SpringLiquibase liquibase(DataSource dataSource) throws Exception{
        SpringLiquibase liquibase=new SpringLiquibase();
        liquibase.setDataSource(dataSource);
        liquibase.setChangeLog("classpath:db/master.xml");
        liquibase.setContexts("application");
        return liquibase;
    }
}

liquibase存储变化的changeLog.xml文件

changeLog.sql

--liquibase formatted sql

--changeset gaosong:20190221160102 failOnError:false
--comment  添加数据
--preconditions onFail:MARK_RAN onError:HALT
--precondition-sql-check expectedResult:0 select count(*) from myfirstgsong where name='gsong'
INSERT INTO myfirstgsong (id,name ) values('123','gsong')


changeLog.xml

<?xml version="1.0" encoding="utf-8"?>
<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-3.4.xsd">
    <changeSet id="20190813" author="gsong" >
        <preConditions onFail="MARK_RAN" onSqlOutput="TEST">
            <not>
                <tableExists tableName="myfirstgsong"></tableExists>
            </not>
        </preConditions>
        <sql>
            <![CDATA[
            CREATE TABLE myfirstgsong (
            `ID`  int NOT NULL ,
            `NAME`  varchar(255) NULL ,
            PRIMARY KEY (`ID`)
            )
            ]]>
        </sql>
    </changeSet>
</databaseChangeLog>

master.xml

<?xml version="1.0" encoding="utf-8"?>
<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-3.4.xsd">
    <!--relativeToChangelogFile如果为true,
    则表示file属性表示的文件路径是相对于根changelog而不是CLASSPATH的,默认为false。-->
    <include file="classpath:db/changeLog.xml" />
    <include file="classpath:db/changeLog.sql"/>

</databaseChangeLog>

启动后创建表并添加一条记录

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值