如何使用Liquibase将数据插入Postgresql数据库?

时间:2019-05-01 13:15:28

标签: postgresql spring-boot liquibase

我已经开发了一个具有两个实体的Spring boot应用程序-Book,Comment-并且我正在使用Liquibase变更集为这些实体创建和插入数据。

application.properties文件:

#### DB properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/bookmanagement
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.initialization-mode=always
spring.datasource.driver-class-name=org.postgresql.Driver

#### Liquibase configuration
spring.liquibase.change-log=classpath:/db/liquibase-changelog.xml
logging.level.liquibase = INFO

#### Hibernate Configuration:
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.generate-ddl=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.hibernate.ddl-auto=create-drop

Liquibase更新日志

<?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="changelog/01-create-book-and-comment-schema.xml" relativeToChangelogFile="true"/>
    <include file="changelog/03-insert-data-books.xml" relativeToChangelogFile="true"/>
    <include file="changelog/02-insert-data-comments.xml" relativeToChangelogFile="true"/>

</databaseChangeLog>

创建表

<changeSet id="01" author="admin">
    <createTable tableName="book"
    remarks="A table to contain all books">
        <column autoIncrement="true" name="id" type="int">
            <constraints unique="true" primaryKey="true"/>
        </column>
        <column name="title" type="varchar(255)" />
        <column name="author" type="varchar(255)" />
        <column name="isbn13" type="varchar(13)" />
    </createTable>

    <createTable tableName="comment"
    remarks="A table to contain all comments related to specific book">
        <column autoIncrement="true" name="id" type="int">
            <constraints unique="true" primaryKey="true"/>
        </column>
        <column name="date" type="date"/>
        <column name="content" type="varchar"/>
        <column name="book_id" type="int"/>
    </createTable>

    <addForeignKeyConstraint baseTableName="comment" baseColumnNames="book_id"
        constraintName="book_fk"
        referencedTableName="book" referencedColumnNames="id"/>
</changeSet>

插入书籍数据

<changeSet author="admin" id="3">
    <insert tableName="book">
        <column name="id" valueNumeric="1"/>
        <column name="author" value="George Orwell"/>
        <column name="title" value="1984"/>
        <column name="isbn13" value="9780451524935"/>
    </insert>
</changeSet>

插入评论数据

<changeSet author="admin" id="2">
    <insert tableName="comment">
        <column name="id" valueNumeric="1"/>
        <column name="date" value="2019-05-09"/>
        <column name="content" value="Comment content test 1"/>
        <column name="book_id" valueNumeric="1"/>
    </insert>
</changeSet>

运行应用程序时,将创建PostgreSQL表,但未插入数据。我不知道这是application.properties文件还是变更集的问题。

0 个答案:

没有答案