How to Quickly Set Up H2 Embedded Database with SpringBoot and MyBatis
This article explains the lightweight features of the pure‑Java H2 embedded database, demonstrates its various modes—including in‑memory, file‑based, and standalone server—and provides step‑by‑step integration with SpringBoot and MyBatis for rapid development and testing.
1. Introduction
When demonstrating MyBatis features, a lightweight database is needed; using MySQL is heavy, Docker adds dependencies. H2, a pure‑Java embedded relational database with JDBC support, fits small demo scenarios.
2. H2 Database Features
Very fast, open‑source, supports JDBC API.
Operates in embedded and server modes; can run as an in‑memory database.
Browser‑based console application.
Small footprint – the JAR is only about 2 MB.
It also runs on most platforms and is compatible with many mainstream RDBMS such as DB2, Oracle, SQL Server, MySQL, PostgreSQL, HSQLDB, Ignite, Derby, etc.
3. Use Cases
Because of its speed and small size, H2 is ideal for quickly building small applications, managing lightweight metadata, or any scenario where a full‑size database is unnecessary. It is especially convenient for development and unit testing, and SpringBoot already includes it as a dependency.
4. Using H2 in SpringBoot
Adding the H2 starter dependency (via SpringBoot’s BOM) and optionally MyBatis is enough to start. Configuration can be done in application.yml. H2 can run in embedded mode (inside the SpringBoot process) or in a separate server process.
5. Embedded Mode
In embedded mode the H2 service starts together with the application; the application acts as both client and server.
5.1 Memory Mode
The database resides entirely in memory and disappears when the JVM stops. A dedicated application-inner.yml profile can be used to configure this mode.
After starting with --spring.profiles.active=inner, the H2 console is reachable at http://localhost:8080/h2-console. The console URL must match the spring.datasource.url configured in the profile. Data inserted via DDL/DML scripts is visible, proving successful integration, but it is lost after shutdown because it is in memory.
5.2 Embedded (File) Mode
Changing spring.datasource.url to jdbc:h2:file:E:/H2/mybatis stores the database on disk, making data persistent across restarts. After restarting, the data remains; removing the initialization scripts shows that persistence is independent of them.
6. Standalone Process
H2 can also run as an independent server. After extracting the distribution, the h2.jar file can be started with: java -cp h2.jar org.h2.tools.Server The JDBC URL format is
jdbc:h2:tcp://{host::localhost}[:{port::9092}]/{database::default}[;USER={user};PASSWORD={password}]. Detailed options are documented in the official H2 manual.
7. Compatibility
H2 can emulate other databases via the MODE URL parameter, e.g.:
Oracle: jdbc:h2:~/test;MODE=Oracle MySQL: jdbc:h2:~/test;MODE=MySQL;DATABASE_TO_LOWER=TRUE PostgreSQL: jdbc:h2:~/test;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE SQL Server: jdbc:h2:~/test;MODE=MSSQLServer Compatibility is not 100 % perfect but works for most cases.
8. Summary
H2 is a lightweight, flexible database that integrates smoothly with SpringBoot and MyBatis for rapid development and testing. Its various modes, small size, and compatibility with major RDBMS make it a convenient choice for Java developers. Sample code is available on Gitee (branch h2).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
