SoFunction
Updated on 2025-03-03

Complete Guide to Integrating H2 Databases in SpringBoot

introduction

Spring Boot is a powerful framework for simplifying enterprise-level Java application development. The H2 database is a lightweight, open source SQL database that is ideal for development and testing. This article will guide you on how to integrate an H2 database in your Spring Boot application and explore some advanced configuration options.

Dependencies

First, we need to add the dependencies of the H2 database and Spring Data JPA in the project's file:

<dependency>
    <groupId></groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

Database configuration

Spring Boot default configuration application is connected to a memory-stored H2 database with usernamesa, the password is empty. We can passAdd the following properties to the file to customize these settings:

=jdbc:h2:mem:testdb
=org.
=sa
=password
-platform=.H2Dialect

Or, we can use itFile configuration:

spring:
  datasource:
    url: jdbc:h2:mem:mydb
    username: sa
    password: password
    driverClassName: org.
  jpa:
    database-platform: .H2Dialect

Database persistence

By default, memory storage of the H2 database will result in data loss after the application restarts. To solve this problem, we can store the database as a file:

=jdbc:h2:file:/data/demo

Or in the YAML file:

spring:
  datasource:
    url: jdbc:h2:file:/data/demo

Database operations

Performing CRUD operations in Spring Boot is similar to that in other SQL databases. We can use JPA repository interfaces and entity classes to manage database operations.

Initialize the data source

We can use SQL scripts to initialize the database. existsrc/main/resourcesCreate an SQL file in the directory and fill in some sample data:

INSERT INTO countries (id, name) VALUES (1, 'USA');
INSERT INTO countries (id, name) VALUES (2, 'France');
INSERT INTO countries (id, name) VALUES (3, 'Brazil');
INSERT INTO countries (id, name) VALUES (4, 'Italy');
INSERT INTO countries (id, name) VALUES (5, 'Canada');

Spring Boot will automatically run this file to initialize the database. We can disable this default behavior by setting the property to never. In addition, multiple SQL files can be configured to load initial data.

Hibernate and

By default, the script is executed before Hibernate is initialized. This makes script-based initialization consistent with other database migration tools such as Flyway and Liquibase. When we recreate the Hibernate generated schema, we need to set an extra property:

-datasource-initialization=true

This modifies the default Spring Boot behavior and fills the data after the Hibernate generation pattern.

Access the H2 console

H2 provides an embedded GUI console for browsing database content and running SQL queries. To enable the H2 console,Add the following properties to:

spring.=true

Or inmiddle:

spring:
  h2:
    console:
      enabled: true

After starting the application, accesshttp://localhost:8080/h2-consoleYou can use the H2 console. Log in on the login page using the configured database URL and credentials.

Further configure the H2 console

We can use the projectSpecify the following properties in the console to further configure:

spring.=/h2-console
spring.=false
spring.-allow-others=false

Or in YAML configuration:

spring:
  h2:
    console:
      path: /h2-console
      settings:
        trace: false
        web-allow-others: false

H2 database URL options

The H2 database URL provides a variety of options to further customize database behavior. For example:

  • DB_CLOSE_DELAY=-1: Make sure the database remains open during the JVM run.
  • DB_CLOSE_ON_EXIT=FALSE: The database remains open even if the JVM is closed.
  • AUTO_RECONNECT=TRUE: Allows automatic reconnection when the connection is lost.
  • MODE=PostgreSQL: Set the H2 database to PostgreSQL compatibility mode.

Sample configuration:

=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE;MODE=PostgreSQL;

in conclusion

The integration of the H2 database with Spring Boot is very smooth, providing a lightweight and feature-rich solution that is perfect for development and testing. With this article's guide, you should be able to easily integrate H2 databases in your Spring Boot application and take advantage of its powerful capabilities.

The above is the detailed content of the complete guide to integrating H2 databases in SpringBoot. For more information about SpringBoot integrating H2 databases, please follow my other related articles!