Spring boot H2 in-memory (or embedded) database example is a simple Spring boot application to demonstrate how to connect H2 database using Spring boot with JPA and test database results with a simple test case and from H2 console web browser.
1. H2 In memory database
What is H2 Databse? : H2 is a open-source relational database management system written in Java. It can be embedded in Java applications or run in client-server mode. It is one of the popular In memory database. Spring Boot provides excellent integration support for H2.
H2 Database Main Features :
- Very fast and light weight database engine
- Open source
- Written in Java
- It is possible to create both in-memory tables, as well as disk-based tables.
- Supports standard SQL, JDBC API
- Embedded and Server mode, Clustering support
- Strong security features
- The PostgreSQL ODBC driver can be used
- Multi version concurrency
- Two full text search implementations are included, a native implementation and one using Lucene.
The complete list of features you can find at H2 Features.
2. Integrating Spring boot + H2 in memory database + JPA
2.1. Technologies used :
- H2 1.4.2
- Spring Boot 2.2.2.RELEASE
- Spring 5.2.2.RELEASE
- Spring Data JPA 2.2.3.RELEASE
- Junit 5.5.2
- Maven 3
- Java 8
- Spring Tool Suite 3.9.8
2.2. Project Structure :
Following is the project structure I have created in this example.
2.3. Maven pom.xml
2.4. User entity to save in H2 Database
2.5. UserRepository
UserRepository.java
to provide mechanism for storage, retrieval, search, update and delete operation to H2 in memory database on User
entity.
2.6. H2 data source properties
Following are the application properties in application.properties
to create data source for H2. For in-memory embedded mode, spring.datasource.url need to update in spring boot properties file jdbc:h2:mem:<db_name>
. You can operate database in other modes also like server or mixed mode.
2.7. Enabling H2 console and web server port mapping
Set following properties in application.properties
to enable H2 console access for GUI access and update port to run application different port than 8080.
2.8. Configuration to Start Spring boot application
Following code is to run Spring boot app. Spring calls @PostConstruct
initDb()
method after the initialization of bean properties.
2.9. Access H2 from console
The H2 Console lets you access a SQL database using a browser gui interface. It is a lightweight application used for examples only. It is not robust or scalable, is not supported, and should NOT be used in a production environment.
Start application using maven $ mvn spring-boot:run
, so that application will get started under port 8181
on context root /usermanagement
based on above configuration.
Np need to start H2 database separately, you can access H2 console using web browser http://127.0.0.1:8181/usermanager/h2
based on above configuration. You will see following window in browser. You can test and connect to H2 database.
2.10. Spring Controller to Users results from browser
Hit the following url in browser http://127.0.0.1:8181/usermanager/api/users
, you will see following results in browser.
2.11. Test the configuration using Test Case
Following is the simple Junit 5 tests case to save User entity in H2 database and test results.
3. Conclusion
In this tutorial we went through H2 in memory database Spring Boot example. We have seen configurations to integrate Spring boot, H2 and JPA and how to access H2 console.
You can checkout source from our github repository: https://github.com/WeLook-team/nguyen-giang-tip.blogspot.com/tree/main/Spring/H2/Spring-Boot-jpa-h2-example
References
- H2 database
- Spring boot hello world
- Spring boot Junit 5 Test
- H2(DBMS)
- JpaRepositiory
- @Transient annotation in JPA and Hibernate to ignore fields
- JUnit 5 Tutorial
No comments:
Post a Comment