1.安装Liquibase
从Liquibase Github Releases下载最新版Liquibase。
wget -c https://github.com/liquibase/liquibase/releases/download/v4.15.0/liquibase-4.15.0.tar.gz
解压
mkdir liquibase
tar -zxf liquibase-4.15.0.tar.gz -C liquibase
移动到/opt目录
sudo mv liquibase /opt
配置PATH环境变量
echo 'export PATH="/opt/liquibase:$PATH"' >> ~/.bashrc
清理
rm liquibase-4.15.0.tar.gz
重启终端,验证安装是否成功
liquibase --version
关于如何卸载liquibase,只需删除liqubase的安装目录以及撤销环境变量配置即可。
2.安装MySQL驱动
下载Connector/J 8.0.30驱动。
wget -c https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-8.0.30.tar.gz
解压
tar -zxf mysql-connector-java-8.0.30.tar.gz
复制mysql-connector-java-8.0.30.jar到/opt/liquibase-4.15.0/lib目录
cd mysql-connector-java-8.0.30
sudo cp mysql-connector-java-8.0.30.jar /opt/liquibase-4.15.0/lib
清理
cd
rm mysql-connector-java-8.0.30.tar.gz
rm -rf mysql-connector-java-8.0.30
3.Spring Boot目录结构
假设Spring Boot项目目录名称为demo,位于用户目录下。
~/demo
/changelogs
20220825221808_InitialCreate.xml
db.changelog-master.xml
liquibase.properties
4.liquibase.properties
需提前手动创建好数据库。
changeLogFile=db.changelog-master.xml
url=jdbc:mysql://127.0.0.1:3306/springblog
username=root
password=Password-4-Root
5.db.changelog-master.xml
changelog文件格式(时间辍精确到秒)
<timestamp>_PascalName.cs
//示例
20220825221808_InitialCreate.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"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.15.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<include file="./changelogs/20220825221808_InitialCreate.xml"/>
</databaseChangeLog>
6.20220825221808_InitialCreate.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"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.15.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<changeSet id="1" author="Liquibase">
<createTable tableName="test_table">
<column name="test_id" type="int">
<constraints primaryKey="true"/>
</column>
<column name="test_column" type="varchar(255)"/>
</createTable>
</changeSet>
</databaseChangeLog>
原创文章,作者:huoxiaoqiang,如若转载,请注明出处:https://www.huoxiaoqiang.com/experience/javae/8681.html