Skip to content

This is a very basic project for beginners to learn about Spring Boot CRUD operations using REST-API and H2 embedded database. We all know about CRUD operation is the combination of Create (SAVE), Read (GET), Update (PUT) and Delete (DELETE). Some of us do not know about H2 Database but we all know about MySQL or PostgreSQL database. Let's learn…

Notifications You must be signed in to change notification settings

rafsan-jany/spring-boot-rest-api-h2-database

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spring-boot-rest-api-h2-database

This is a very basic project for beginners to learn about Spring Boot CRUD operations using REST-API and H2 Embedded Database. We all know about CRUD operation is the combination of Create (SAVE), Read (GET), Update (PUT) and Delete (DELETE). Some of us do not know about H2 Database but we all know about MySQL or PostgreSQL database. Let's learn about H2!

H2 Database

H2 is an embedded and in-memory database. That means the data we are going to save using our spring boot project will be stored in a temporary memory. In this project we will save Student entity (id, name, age, email) in our H2 db. When we will store the Student object, It persists only in system-memory for that session not in the disk for permanently. That means if the session end, the data from the H2 DB will be erased. What about if we want to store data permanently in disk? Well, for this purpose, we use MySQL, PostgreSQL or OracleDB.

There are some other benifits too. We do not need to install H2 DB in our system, just need to add dependency to the spring boot pom.xml and configurations in application.properties. For this benifits, H2 DB is used widely for POCs (Proof of Concepts) and unit testing. Apache Derby is another known in-memory database.

The best way is to create a Spring Boot project using Spring Stater Project to add Spring Web, Spring Data JPA and H2 Database dependencies. A similar link can be followed if you are interested how to add STS in Eclipse IDE and add dependency using Spring Starter Project GitHub.

The Spring Web, Spring Data JPA and H2 Database dependencies in pom.xml are

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

The configuration in appliction.properties is

#enabling the H2 console  
spring.h2.console.enabled=true

spring.datasource.url=jdbc:h2:mem:rjanytest
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=123

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update

Here spring.datasource.url=jdbc:h2:mem:rjanytest rjanytest is the database name.

Dive into the project

Download or clone this project, import this to your favourite IDE and run. This project will run on default spring boot port on localhost is 8080. I have implemented 5 end points for serving the purpose of CRUD operations. The end points are :

HTTP Operation End Points Purpose
POST http://localhost:8080/student Save student
GET http://localhost:8080/student/1 Fetch student using ID
GET http://localhost:8080/student Fetch all students
PUT http://localhost:8080/student/1 Update student using ID
DELETE http://localhost:8080/student/1 Delete student using ID

After starting the project on port 8080, first need to login the H2 Database using the http://localhost:8080/h2-console/ url. just insert the password that is set in application.properties.

h2_db_login

There is a table created name Student in our rjanyTest database. Let's insert a student in the Student table using postman.

Post

Let's check the table using the browser. Click on the Student table and then click Run. The Student entity is saved in the table with id 1. wow!

h2

Let's try with GET, GET for all, PUT and DELETE and check the H2 database using browser.

GET Get

GET all
Before this we need to save another Student entity. GetAll

PUT Put Student

DELETE delete

That's it for today. Learned a lot!

About

This is a very basic project for beginners to learn about Spring Boot CRUD operations using REST-API and H2 embedded database. We all know about CRUD operation is the combination of Create (SAVE), Read (GET), Update (PUT) and Delete (DELETE). Some of us do not know about H2 Database but we all know about MySQL or PostgreSQL database. Let's learn…

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages