SpringBoot整合Liquibase的示例代码

网友投稿 667 2022-11-01

SpringBoot整合Liquibase的示例代码

SpringBoot整合Liquibase的示例代码

目录整合1整合2

SpringBoot整合Liquibase虽然不难但坑还是有一点的,主要集中在配置路径相关的地方,在此记录一下整合的步骤,方便以后自己再做整合时少走弯路,当然也希望能帮到大家~

整合有两种情况

在启动项目时自动执行脚本,若新添加了Liquibase脚本需要重启项目才能执行脚本在不启动项目时也能通过插件或指令手动让它执行脚本

整合要么只整合1,要么1、2一起整合

只整合2不整合1的话,项目启动时会生成liquibase相关的bean时报错

整合1

引入Maven依赖

这里导入了Liquibase的包和连接mysql数据库的包

org.liquibase

liquibase-core

3.6.2

mysql

mysql-connector-java

runtime

在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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns="http://liquibase.org/xml/ns/dbchangelog"

xsi:schemaLocation="http://liquibase.org/xml/ns/dbchangelog

http://liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns="http://liquibase.org/xml/ns/dbchangelog"

xsi:schemaLocation="http://liquibase.org/xml/ns/dbchangelog

http://liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">

配置changeLog.xml

按照上述的配置的话,changeLog文件应该放在src/main/resources/db/changelog/下面简单写一个changelog来测试

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns="http://liquibase.org/xml/ns/dbchangelog"

xsi:schemaLocation="http://liquibase.org/xml/ns/dbchangelog

http://liquibase.org/xml/ns/dbchangelog/dbchangeWVyZelog-3.4.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns="http://liquibase.org/xml/ns/dbchangelog"

xsi:schemaLocation="http://liquibase.org/xml/ns/dbchangelog

http://liquibase.org/xml/ns/dbchangelog/dbchangeWVyZelog-3.4.xsd">

整合情况:

按照上面的流程配置完,在启动项目时,它就会执行未执行过的Lisquibase脚本了~

整合2

有时候想不启动或不重启项目时执行Liquibase脚本,此时就应该整合2

eWVyZ

引入Maven插件

引入的插件版本和上面的依赖包的版本号保持一致,不过我也没有考究过不一致会怎样,有兴趣的小伙伴可以试试 ,不过不一致看得不会难受吗在这里的configuration标签中,配置好要读取的liquibase相关信息的配置文件,并且注意好路径

org.liquibase

liquibase-maven-plugin

3.6.2

src/main/resources/liquibase.properties

false

配置Liquibase.properties

注意配置文件的路径应跟在配置Maven插件时声明的一致,即src/main/resources/liquibase.properties配置好如下的五个属性,注意url需要加上时区serverTimezone注意changeLogFile项的路径,它是一个相对路径,该配置会在下面展示

#配置都整合在yml配置文件里了,若想不启动时执行liquibase脚本该配置不能省!!!!

driver-class-name=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/test_database?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai

username=root

password=root

changeLogFile=db/liquibaseChangeLogFile.xml

配置liquibaseChangeLogFile.xml

在整合1的时候就已经配置好了~

配置changeLog.xml

按照上述的配置的话,changeLog文件应该放在src/main/resources/db/changelog/下面再写一个liquibase脚本

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns="http://liquibase.org/xml/ns/dbchangelog"

xsi:schemaLocation="http://liquibase.org/xml/ns/dbchangelog

http://liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns="http://liquibase.org/xml/ns/dbchangelog"

xsi:schemaLocation="http://liquibase.org/xml/ns/dbchangelog

http://liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">

测试整合情况

先mvn clean和install一下!!! 否则不能找到我们写的xml文件在maven插件中找到liquibase:update,双击即可~

补充:下面给大家分享一段示例代码讲解下spring boot 整合 liquibase

1.添加依赖

org.liquibase

liquibase-core

3.8.3

org.liquibase

liquibase-maven-plugin

3.8.3

org.liquibase

liquibase-maven-plugin

3.8.3

src/main/resources/db/liquibase.properties

2.创建liquibase.properties 内容如下

url=jdbc:postgresql://127.0.0.1:5432/ems_factorymodeldb

username=name

password=password

driver=org.postgresql.Driver

outputChangeLogFile=src/main/resources/liquibase-outputChangeLog.xml

3.创建 db.changelog-master.xml

xmlns="http://liquibase.org/xml/ns/dbchangelog"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://liquibase.org/xml/ns/dbchangelog

http://liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

xmlns="http://liquibase.org/xml/ns/dbchangelog"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://liquibase.org/xml/ns/dbchangelog

http://liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

4.增加配置

# liquibase

spring.liquibase.enabled=true

spring.liquibase.change-log=classpath:db/db.changelog-master.xml

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:DeepLearningKit 是针对 iOS、OS X 和 tvOS 的开源深度学习框架
下一篇:python下的极简orm框架,核心思想,领域对象+仓库
相关文章

 发表评论

暂时没有评论,来抢沙发吧~