liquibase是一个升级脚本工具,且可以进行前置条件的约束
spring boot集成liquibase
步骤1、添加依赖
<!--liquibase-->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
步骤2:配置application.properties
#### datasource 不配置这个?serverTimezone=UTC 会报错如下错误
# The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone
server.port=9090
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
# LIQUIBASE (LiquibaseProperties)
spring.liquibase.check-change-log-location=true
spring.liquibase.drop-first=false
spring.liquibase.enabled=true
spring.liquibase.change-log=classpath:/liqubase/changeLog.xml
步骤3:在liqubase/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">
<property name="now" value="now()" dbms="mysql"/> <!-- runOnChange ="false"是如果id+author+内容 md5加密之后不改变是不变得--><changeSet id="init-schema" author="jhh" runOnChange="false">
<preConditions onFail="MARK_RAN"> <sqlCheck expectedResult="0"> select count(*) from user_name where user_name = 22; </sqlCheck> </preConditions> <comment>新增脚本</comment>
<sql> INSERT INTO `user_name` ( `user_name`, `phone`, `address`) VALUES ( '22', '33', '22'); </sql>
<rollback> DELETE from user_name where user_name = 22; </rollback>
</changeSet> </databaseChangeLog>