This guide shows how to integrate
Liquibase with
H2 database in a
Spring Boot app for managing schema changes easily.
1. Add Dependencies
In
pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
2. Configure H2 and Liquibase
In
application.properties:
spring.datasource.url=jdbc:h2:mem:liquibasedb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=none
spring.liquibase.change-log=classpath:db/changelog/db.changelog-master.xml
3. Create Changelog File
src/main/resources/db/changelog/db.changelog-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">
<changeSet id="1" author="dev">
<createTable tableName="employee">
<column name="id" type="BIGINT" autoIncrement="true" primaryKey="true"/>
<column name="name" type="VARCHAR(100)"/>
<column name="role" type="VARCHAR(50)"/>
</createTable>
</changeSet>
</databaseChangeLog>
4. Run the Application
Start your app:
mvn spring-boot:run
Liquibase will apply the schema automatically to H2.
5. Verify
Open H2 console at:
http://localhost:8080/h2-console
Run:
SELECT * FROM employee;
You will see the table created by Liquibase.
image quote pre code