首页 > 技术文章 > 数据变更版本管理工具---liquibase

agnesFlower 2020-12-04 09:51 原文

1、springboot项目中配置liquibase:

maven依赖:

<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>3.5.3</version>
</dependency>


配置类:
import liquibase.integration.spring.SpringLiquibase;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


@Configuration
public class LiquibaseConfig {

@Bean
public SpringLiquibase liquibase(DataSource dataSource) {
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setDataSource(dataSource);
liquibase.setChangeLog("classpath:liquibase/db.changelog-master.xml");
liquibase.setContexts("development,test,production");
liquibase.setShouldRun(true);
return liquibase;
}
}
db.changelog-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.1.xsd">

<!-- <include file="src/main/resources/liquibase/changelog_dev.xml"/> -->
<!--<include file="classpath:/liquibase/db.changelog-1.1.xml"/>-->
<include file="classpath:/liquibase/db.changelog-1.2.xml"/>
<!-- <include file="com/example/db/changelog/db.changelog-2.0.xml"/>-->
</databaseChangeLog>
db.changelog-1.2.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.1.xsd">


<changeSet id="2" author="author2">
<sql>
CREATE TABLE T_Person(FName VARCHAR(20),FAge INT DEFAULT 20,FId INT NOT NULL,PRIMARY KEY(FId));
</sql>
</changeSet>

<!--<changeSet id="3" author="panli">
<sql>
alter TABLE T_Person add phone varchar(255) comment '电话';
</sql>
</changeSet>-->

<changeSet id="4" author="author2">
<sql>
insert into T_Person(FName,FAge ,FId,phone) values('吴凌胜',22,3,'1234444');
insert into T_Person(FName,FAge ,FId,phone) values('吴凌胜',22,4,'1234444');
</sql>
</changeSet>
</databaseChangeLog>
 

运行日志:

2020-10-27 14:46:30.200  INFO 20160 --- [           main] liquibase                                : Successfully acquired change log lock
2020-10-27 14:46:30.631  INFO 20160 --- [           main] liquibase                                : Reading from test.DATABASECHANGELOG
2020-10-27 14:46:30.637  INFO 20160 --- [           main] liquibase                                : classpath:liquibase/db.changelog-master.xml: classpath:/liquibase/db.changelog-1.2.xml::4::author2: Custom SQL executed
2020-10-27 14:46:30.639  INFO 20160 --- [           main] liquibase                                : classpath:liquibase/db.changelog-master.xml: classpath:/liquibase/db.changelog-1.2.xml::4::author2: ChangeSet classpath:/liquibase/db.changelog-1.2.xml::4::panli2 ran successfully in 5ms
2020-10-27 14:46:30.645  INFO 20160 --- [           main] liquibase                                : Successfully released change log lock

两张日志表:

 

liquibase.util.grammar.TokenMgrError: Lexical error at line 1, column 40. En:报错,sql文件格式有问题,有中文符号

2、spring配置liquibase:

<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
      <property name="dataSource" ref="dataSource" />
      <property name="changeLog" value="classpath:liquibase/db.changelog-master.xml" />
      <property name="contexts" value="test, production" />
 </bean>

学习链接:

https://www.cnblogs.com/liyanyan665/p/11183019.html

https://www.cnblogs.com/itrena/p/5927145.html

https://blog.csdn.net/jianyi7659/article/details/7804144

https://www.zhihu.com/question/20080857

https://www.ibm.com/developerworks/cn/java/j-ap08058/index.html

https://www.jianshu.com/p/07a45b6722fd

http://www.manongjc.com/article/35945.html

https://blog.csdn.net/weixin_34087307/article/details/91397914

 

推荐阅读