SpringBoot+Maven+liquibase
配置文件中
spring:
liquibase:
change-log: classpath:conf/liquibase/master.xml
enabled: true
drop-first: false
contexts: dev,prod
1
2
3
4
5
6
代碼中
import liquibase.integration.spring.SpringLiquibase;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class LiquibaseConfig {
@Value("${spring.liquibase.enabled}")
private Boolean enable;
@Value("${spring.liquibase.drop-first}")
private Boolean deopFirst;
@Value("${spring.liquibase.contexts}")
private String contexts;
@Bean(name = "liquibase")
public SpringLiquibase springLiquibase(DataSource dataSource) {
SpringLiquibase springLiquibase = new SpringLiquibase();
springLiquibase.setDataSource(dataSource);
springLiquibase.setChangeLog("classpath:/conf/liquibase/master.xml");
springLiquibase.setShouldRun(enable);
springLiquibase.setDropFirst(deopFirst);
springLiquibase.setContexts(contexts);
return springLiquibase;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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.6.xsd">
<property name="now" value="now()" dbms="h2"/>
<property name="now" value="now()" dbms="mysql"/>
<property name="floatType" value="float4" dbms="postgresql, h2"/>
<property name="floatType" value="float" dbms="mysql, oracle, mssql, mariadb"/>
<include file="classpath:conf/liquibase/changelog/20190626reportdata_addcloumn.xml" relativeToChangelogFile="false"/>
</databaseChangeLog>
后續加文件時,繼續追加就行了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
20190626reportdata_addcloumn.xml
<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
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.6.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<property name="autoIncrement" value="true"/>
<changeSet id="20190626reportdata_addcloumn" author="zzh">
<addColumn tableName="report_data">
<column name="status" type="int" remarks="狀態" ></column>
</addColumn>
<addColumn tableName="report_data">
<column name="create_user_id" type="int" remarks="創建人id" ></column>
</addColumn>
<addColumn tableName="report_data">
<column name="check_user_id" type="int" remarks="確認人id" ></column>
</addColumn>
<addColumn tableName="report_data">
<column name="check_time" type="datetime" remarks="確認時間" ></column>
</addColumn>
</changeSet>
</databaseChangeLog>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
---------------------