温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

liquibase怎么在SpringBoot中使用

发布时间:2021-04-19 17:57:15 来源:亿速云 阅读:272 作者:Leah 栏目:编程语言

这期内容当中小编将会给大家带来有关liquibase怎么在SpringBoot中使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

一、引入依赖

先在 pom 文件里引入依赖

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

二、指定配置文件位置

在代码中新建一个 LiquibaseConfig 类,用于配置 Liquibase ,指定配置文件的位置。

import javax.sql.DataSource;
import liquibase.integration.spring.SpringLiquibase;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LiquibaseConfig {

 @Bean
 public SpringLiquibase liquibase(DataSource dataSource) {
 SpringLiquibase liquibase = new SpringLiquibase();
 liquibase.setDataSource(dataSource);
 //指定changelog的位置,这里使用的一个master文件引用其他文件的方式
 liquibase.setChangeLog("classpath:liquibase/master.xml");
 liquibase.setContexts("development,test,production");
 liquibase.setShouldRun(true);
 return liquibase;
 }

}

三、编写配置文件

目录结构:

liquibase怎么在SpringBoot中使用

src/main/resources 下新建一个文件夹: liquibase ,用来存放跟 liquibase 相关的文件。

master.xml

然后在 liquibase 文件夹下新建 master.xml 作为主文件。

<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">

 <includeAll path="liquibase/changelogs/" relativeToChangelogFile="false"/>

</databaseChangeLog>

includeAll 标签可以把一个文件夹下的所有 changelog 都加载进来。如果单个加载可以用 include

includeAll 标签里有两个属性: pathrelativeToChangelogFile


AttributeDescription
fileName of the file to import required
relativeToChangelogFileIs the file path relative to the root changelog file rather than to the classpath. Defaults to "false" since 1.9

path (在 include 标签里是 file):指定要加载的文件或文件夹位置

relativeToChangelogFile :文件位置的路径是否相对于 root changelog 是相对路径,默认 false,即相对于 classpath 是相对路径。

changelog

另在 liquibase 文件夹下新建 changelogs 文件夹用来存放 changelog。

这里新建一个 changelog-1.0.xml

<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="20190713-01" author="solo">
  <createTable tableName="project_info">
   <column name="project_id" type="varchar(64)" encoding="utf8" remarks="项目id">
    <constraints primaryKey="true" nullable="false"/>
   </column>
   <column name="project_name" type="varchar(255)" encoding="utf8" remarks="项目名字"/>
   <column name="project_difficulty" type="float" encoding="utf8" remarks="项目难度"/>
   <column name="category_id" type="varchar(64)" encoding="utf8" remarks="项目类型类目编号"/>
   <column name="project_status" type="int(11)" encoding="utf8" remarks="项目状态, 0招募中,1 进行中,2已完成,3失败,4延期,5删除"/>
   <column name="project_desc" type="varchar(512)" encoding="utf8" remarks="项目简介"/>
   <column name="project_creater_id" type="varchar(64)" encoding="utf8" remarks="项目创建者id"/>
   <column name="team_id" type="varchar(64)" encoding="utf8" remarks="项目所属团队id"/>
   <column name="create_time" type="bigint(64)" encoding="utf8" remarks="创建时间"/>
   <column name="update_time" type="bigint(64)" encoding="utf8" remarks="更新时间"/>
  </createTable>
 </changeSet>
 
 <changeSet id="20190713-02" author="solo">
  <createTable tableName="project_category" remarks="项目类型表">
   <column name="id" type="varchar(64)" remarks="项目类型id">
    <constraints primaryKey="true" nullable="false"/>
   </column>
   <column name="name" type="varchar(255)" remarks="类目类型名称"/>
   <column name="status" type="int(11)" remarks="状态。1正常,2删除"/>
   <column name="remark" type="varchar(255)" remarks="备注"/>
  </createTable>
 </changeSet>

 <changeSet id="20190713-03" author="solo">
  <createTable tableName="project_like_user" remarks="项目点赞表">
   <column name="id" type="varchar(64)" remarks="主键id">
    <constraints primaryKey="true" nullable="false"/>
   </column>
   <column name="project_id" type="varchar(64)" remarks="项目id"/>
   <column name="user_id" type="varchar(64)" remarks="点赞的用户id"/>
   <column name="status" type="int(11)" remarks="点赞状态,0 取消点赞,1点赞"/>
   <column name="type" type="int(11)" remarks="类型 1点赞"/>
   <column name="create_time" type="bigint(64)" remarks="创建时间"/>
   <column name="update_time" type="bigint(64)" remarks="更新时间"/>
  </createTable>
 </changeSet>

 <changeSet id="20190713-04" author="solo">
  <createTable tableName="project_picture" remarks="项目图片表">
   <column name="id" type="varchar(64)" remarks="图片id">
    <constraints primaryKey="true" nullable="false"/>
   </column>
   <column name="project_id" type="varchar(64)" remarks="项目id"/>
   <column name="picture_url" type="varchar(64)" remarks="图片地址"/>
   <column name="picture_url_32" type="varchar(64)" remarks="图片地址32位"/>
   <column name="picture_url_64" type="varchar(64)" remarks="图片地址64位"/>
  </createTable>
 </changeSet>

</databaseChangeLog>

如果你的项目一开始就用了 liquibase,那可以像上面这样写,把建表语句都写在 changelog 里。

如果一开始没用,后期想引入 liquibase,可以把以前的数据库导出成 sql,然后引入 sql 文件。方式如下:

<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="liquibase/changelogs/project.sql" relativeToChangelogFile="false"/>

</databaseChangeLog>

上述就是小编为大家分享的liquibase怎么在SpringBoot中使用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI