SpringBoot整合Liquibase虽然不难但坑还是有一点的,主要集中在配置路径相关的地方,在此记录一下整合的步骤,方便以后自己再做整合时少走弯路,当然也希望能帮到大家~
整合有两种情况
- 在启动项目时自动执行脚本,若新添加了Liquibase脚本需要重启项目才能执行脚本
- 在不启动项目时也能通过插件或指令手动让它执行脚本
整合要么只整合1,要么1、2一起整合
只整合2不整合1的话,项目启动时会生成liquibase相关的bean时报错
整合1
引入Maven依赖
这里导入了Liquibase的包和连接MySQL数据库的包
<!-- https://mvnrepository.com/artifact/org.liquibase/liquibase-core -->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>3.6.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
在SpringBoot配置文件中配置
配置中liquibase相关的只需要配置根changelog的位置,数据库相关配置liquibase会自己去拿datasource下面的,注意url需要加上时区serverTimezone
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/test_database?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
liquibase:
change-log: classpath:/db/liquibaseChangeLogFile.xml
配置liquibaseChangeLogFile.xml
同样需要注意xml文件的路径,按照上述配置的话该文件在src/main/resources/db/liquibaseChangeLogFile.xml
<?xml version="1.0" encoding