【问题标题】:Jhipster : How to populate DB with Liquibase at startJhipster:如何在开始时使用 Liquibase 填充数据库
【发布时间】:2018-06-28 12:45:12
【问题描述】:

我是 Jhipster 的新手,我创建了一个单体应用程序,我想知道当我的应用程序在 dev 中启动时如何在我的 H2 数据库中自动创建实体?

我在 /resources/config/liquibase 中看到了一些似乎创建用户和权限的 csv 文件,但我不知道如何使用它创建其他实体

【问题讨论】:

标签: java spring-boot jhipster


【解决方案1】:

您可以创建一个使用 ext:LoadData 元素的新 Liquibase ChangeSet。在此元素中,您需要指定要填充的每一列。

<changeSet author="Joe Bloggs" id="42">
  <ext:loadData identityInsertEnabled="true" commentLineStartsWith="#"
            encoding="UTF-8"
            file="config/liquibase/changelog/data/foo.csv"
            quotchar="&#34;"
            separator=","
            tableName="FOO">
     <column name="bar" type="STRING"/>
     <column name="baz" type="STRING"/>
  </ext:loadData>
</changeSet>

然后在您的 foo.csv 文件中添加您的值(第一行是标题,后续行是数据):

"bar","baz"
"bar1","baz1"
"bar2","baz2"
"bar3","baz3"

【讨论】:

  • 要完全回答仅在 dev 中为 h2 加载它的问题,您可以在 dbms="h2"context="dev" 上添加条件
【解决方案2】:

什么对我有用:

在您的 config/liquidbase/changelog 中编辑“add_entity”xml(例如:20180606131920_added_entity_Product.xml)并在 createTable 标签之后添加:

        <loadData encoding="UTF-8"
              file="config/liquibase/product.csv"
              separator=";"
              tableName="product">
    </loadData>

当然会在指定位置创建 product.csv

【讨论】:

    猜你喜欢
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-31
    • 1970-01-01
    • 2020-10-31
    相关资源
    最近更新 更多