SpringBoot整合Liquibase

SpringBoot整合Liquibase


1、在pom文件中添加依赖,然后在application.yml/application.properties定制配置信息(也可以不添加,走默认配置),项目启动时就会去运行指定目录下的数据库更改文件。

导入依赖

 <dependency>
      <groupId>org.liquibase</groupId>
      <artifactId>liquibase-core</artifactId>
</dependency>

application.yml 文件中添加配置,指定去找那个配置文件

# Liquibase 配置
liquibase:
  url: jdbc:mysql://localhost:3306/stusystem?useUnicode=true&amp;characterEncoding=utf-8
  driver: com.mysql.jdbc.Driver
  password: root
  username: root
  enabled: true
  change-log: classpath:config/master.xml

master.xml 文件中内容
这个文件的作用是去找到具体的建表xml文件,执行过以后就在数据库的databasechangelog表中记录一条信息,id为<changeSet>的id值。

<?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.5.xsd">
         <!-- 这里写清楚更改数据的xml文件路径和名字 -->
        <include file="classpath:config/liquibase/changelog/20200902140435678_add_column_ShopDrug.xml" relativeToChangelogFile="false"/>
</databaseChangeLog>

<changeSet>xml文件

<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
    <property name="now" value="now()" dbms="h2"/>
    <property name="now" value="now()" dbms="mysql"/>
    <property name="autoIncrement" value="true"/>
    <property name="floatType" value="float4" dbms="postgresql, h2"/>
    <property name="floatType" value="float" dbms="mysql, oracle, mssql"/>
    <!--
        Added the entity orderDelivery.
    -->
    <!-- 这是一个创建表的语句 也可以直接写SQL语句 -->
    <changeSet id="202008211521-1" author="cxt">
        <createTable tableName="order_delivery" remarks="配送表">
            <column name="id" type="varchar(22)">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="order_id" type="varchar(22)" remarks="订单表ID">
                <constraints nullable="true"/>
            </column>
            <column name="address" type="varchar(255)" remarks="地址">
                <constraints nullable="true"/>
            </column>
            <column name="room" type="varchar(22)" remarks="门牌号">
                <constraints nullable="true"/>
            </column>
            <column name="address_lng" type="double" remarks="地址经度">
                <constraints nullable="true"/>
            </column>
            <column name="address_lat" type="double" remarks="地址纬度">
                <constraints nullable="true"/>
            </column>
            ……
            <column name="lng" type="double" remarks="当前经度">
                <constraints nullable="true"/>
            </column>
            <column name="lat" type="double" remarks="当前纬度">
                <constraints nullable="true"/>
            </column>
            <column name="receiving_date" type="timestamp">
                <constraints nullable="true" />
            </column>
            <column name="finish_date" type="timestamp">
                <constraints nullable="true" />
            </column>
        </createTable>
    </changeSet>
</databaseChangeLog>

在写java配置文件

package com.stu.stusystem.config;

import liquibase.integration.spring.SpringLiquibase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @date 2020/9/7
 * <p>
 * Liquibase 的配置文件
 */
@Configuration
public class LiquibaseConfig {

    @Qualifier("dataSource")
    private DataSource dataSource;

    @Bean
    public SpringLiquibase liquibase() {
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setDataSource(dataSource);
        liquibase.setChangeLog("classpath:config/master.xml");
        liquibase.setContexts("development,test,preproduction,production");
        liquibase.setShouldRun(true);
        return liquibase;
    }

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
}

然后,在项目中配置好数据库信息,接着启动项目在第一次启动项目的时候就可以在数据库中看到自动创建一系列的表。
接着正常流程:Controller,Service…………

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容