Skip to main content

Step 11 - H2 test DB


The Goal

Add H2 config

  • After this step, your Spring Boot app will be able to connect to one of 2 databases:
    • MySQL
      • profile: 'mysql'
      • local instance or Docker container
        • you are responsible for the database config
    • H2
      • profile: 'h2'
      • in-memory database
      • Spring will do everything for you
+-------------+         +-------------------+             +---------------+
| SpringBoot | | Datasource_MySQL | | Datasource_H2 |
+-------------+ +-------------------+ +---------------+
| | ----------------------\ |
| |-| A local instance or | |
| | | Docker container: | |
| | | your job | | ---------------------------\
| | |---------------------| |-| Spring's job |
| | | |--------------------------|
| | |
| profile: mysql | |
|------------------------->| |
| | |
| profile: h2 | |
|--------------------------------------------------------->|
| | |


Starter code

The updated files after this step

H2 config


TODO

Update pom.xml: add h2 dependency

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>northwind-shipper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>northwind-shipper</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Update pom.xml: create application-h2.properties

spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:${DB_NAME:northwind}

The Glossary

H2

H2 is a lightweight, in-memory database used in Spring applications for development, testing, and prototyping purposes.

Spring Boot provides built-in support for H2, allowing developers to quickly and easily configure and use it as a database for their applications. H2 can be used as an embedded database, which means that it can run within the application and be automatically created and destroyed as needed, or it can be used as a standalone database that runs separately from the application.

Using H2 in Spring allows developers to quickly prototype and test their applications without the need for a full-fledged database system, and it provides a convenient and flexible way to store and retrieve data in various scenarios.