SoFunction
Updated on 2025-04-04

Detailed steps to integrate Liquibase in SpringBoot

1. Create a Spring Boot project

First, you need to create a Spring Boot project. You can use Spring Initializr(/) to create a project, selecting the following dependencies:

  • Spring Web
  • Spring Data JPA
  • MySQL Driver(or the database driver you are using)
  • Liquibase Migration

Generate basic project structure

After selecting the appropriate project settings in Spring Initializr, download the generated project and unzip it.

2. Add Maven dependencies

If you don't select Liquibase when you build your project, you need to manually add the Liquibase dependency. existAdd the following dependencies:

<dependency>
    <groupId></groupId>
    <artifactId>liquibase-core</artifactId>
    <version></version> <!-- Use the latest version -->
</dependency>

Make sure to include Spring Data JPA and database drivers, such as MySQL, in the dependencies:

<dependency>
    <groupId></groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

3. Configure database connections

existsrc/main/resources/orConfigure database connection information:

Example

=jdbc:mysql://localhost:3306/my_database?useSSL=false&serverTimezone=UTC
=my_user
=my_password
-auto=none
-log=classpath:db/changelog/

4. Create a change log file

existsrc/main/resources/db/changelogCreated in the directoryFile and define database changes.

Example

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
    xmlns="/xml/ns/dbchangelog"
    xmlns:xsi="http:///2001/XMLSchema-instance"
    xsi:schemaLocation="/xml/ns/dbchangelog
    /xml/ns/dbchangelog/dbchangelog-4.">

    <changeSet  author="authorName">
        <createTable tableName="users">
            <column name="id" type="int">
                <constraints primaryKey="true" autoIncrement="true"/>
            </column>
            <column name="name" type="varchar(100)">
                <constraints nullable="false"/>
            </column>
            <column name="email" type="varchar(100)">
                <constraints unique="true"/>
            </column>
            <column name="created_at" type="timestamp">
                <constraints defaultValueComputed="CURRENT_TIMESTAMP"/>
            </column>
        </createTable>
    </changeSet>

    <changeSet  author="authorName">
        <addColumn tableName="users">
            <column name="age" type="int"/>
        </addColumn>
    </changeSet>

    <changeSet  author="authorName">
        <createTable tableName="orders">
            <column name="id" type="int">
                <constraints primaryKey="true" autoIncrement="true"/>
            </column>
            <column name="user_id" type="int"/>
            <column name="product" type="varchar(100)">
                <constraints nullable="false"/>
            </column>
            <column name="quantity" type="int">
                <constraints nullable="false"/>
            </column>
            <column name="order_date" type="timestamp">
                <constraints defaultValueComputed="CURRENT_TIMESTAMP"/>
            </column>
            <foreignKeyConstraint baseTableName="orders" baseColumnNames="user_id"
                referencedTableName="users" referencedColumnNames="id" 
                constraintName="fk_user_id"/>
        </createTable>
    </changeSet>

</databaseChangeLog>

5. Run the Spring Boot application

Run the application in the IDE, or use the command line:

mvn spring-boot:run

Start the application

When the Spring Boot application starts, Liquibase will automatically detectChanges defined in   and apply to the database. You will see the console output showing the change set that Liquibase is executing.

6. Verify database changes

Connect to using database management tools (such as MySQL Workbench, DBeaver, etc.)my_database, and checkusersandordersWhether the table has been successfully created.

Checklist

You can run the following SQL query to verify:

SHOW TABLES; -- View all tables in the database

SELECT * FROM users; -- Check users surface
SELECT * FROM orders; -- Check orders surface

7. Monitoring and Maintenance

Liquibase creates a databasedatabasechangelogTable, record all applied change sets. You can query this table for change history:

SELECT * FROM databasechangelog;

8. Rollback Change (optional)

If you need to undo a change, you can use Liquibase's rollback function. For example, you could create a rollback file and roll back using the command line:

liquibase rollbackCount 1

This will roll back the most recent change set.

Summarize

Through the above steps, you have successfully integrated Liquibase into your Spring Boot application. Liquibase provides powerful database versioning and migration capabilities, making managing database changes simple and efficient. By using Liquibase, you ensure that the database structure is synchronized with application code and that the evolution of the database can be easily managed and maintained.

The above is the detailed steps of SpringBoot integrating Liquibase. For more information about SpringBoot integrating Liquibase, please follow my other related articles!