SpringBoot整合Liquibase
1、在pom文件中添加依賴,然后在application.yml/application.properties定制配置信息(也可以不添加,走默認(rèn)配置),項(xiàng)目啟動(dòng)時(shí)就會(huì)去運(yùn)行指定目錄下的數(shù)據(jù)庫(kù)更改文件。
導(dǎo)入依賴
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
application.yml 文件中添加配置,指定去找那個(gè)配置文件
# Liquibase 配置
liquibase:
url: jdbc:mysql://localhost:3306/stusystem?useUnicode=true&characterEncoding=utf-8
driver: com.mysql.jdbc.Driver
password: root
username: root
enabled: true
change-log: classpath:config/master.xml
master.xml 文件中內(nèi)容
這個(gè)文件的作用是去找到具體的建表xml文件,執(zhí)行過(guò)以后就在數(shù)據(jù)庫(kù)的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">
<!-- 這里寫(xiě)清楚更改數(shù)據(jù)的xml文件路徑和名字 -->
<include file="classpath:config/liquibase/changelog/20200902140435678_add_column_ShopDrug.xml" relativeToChangelogFile="false"/>
</databaseChangeLog>
寫(xiě)<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.
-->
<!-- 這是一個(gè)創(chuàng)建表的語(yǔ)句 也可以直接寫(xiě)SQL語(yǔ)句 -->
<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="門(mén)牌號(hào)">
<constraints nullable="true"/>
</column>
<column name="address_lng" type="double" remarks="地址經(jīng)度">
<constraints nullable="true"/>
</column>
<column name="address_lat" type="double" remarks="地址緯度">
<constraints nullable="true"/>
</column>
……
<column name="lng" type="double" remarks="當(dāng)前經(jīng)度">
<constraints nullable="true"/>
</column>
<column name="lat" type="double" remarks="當(dāng)前緯度">
<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>
在寫(xiě)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;
}
}
然后,在項(xiàng)目中配置好數(shù)據(jù)庫(kù)信息,接著啟動(dòng)項(xiàng)目在第一次啟動(dòng)項(xiàng)目的時(shí)候就可以在數(shù)據(jù)庫(kù)中看到自動(dòng)創(chuàng)建一系列的表。
接著正常流程:Controller,Service…………