In this tutorial, we’re gonna build a Spring Boot Rest CRUD API example with Maven that use Spring Data JPA to interact with H2 database. You’ll know:
- How to configure Spring Data, JPA, Hibernate to work with Database
- How to define Data Models and Repository interfaces
- Way to create Spring Rest Controller to process HTTP requests
- Way to use Spring Data JPA to interact with H2 Database
For more details, please visit:
https://bezkoder.com/spring-boot-jpa-h2-example/
Using JdbcTemplate instead:
Spring Boot JdbcTemplate example: CRUD Rest API
Overview of Spring Boot JPA + H2 example
We will build a Spring Boot Rest Apis using Spring Data JPA with H2 Database for a Tutorial application in that:
- Each Tutotial has id, title, description, published status.
- Apis help to create, retrieve, update, delete Tutorials.
- Apis also support custom finder methods such as find by published status or by title.
These are APIs that we need to provide:
- POST
/api/tutorials
: create new Tutorial - GET
/api/tutorials
: retrieve all Tutorials - GET
/api/tutorials/[id]
: retrieve a Tutorial by :id - PUT
/api/tutorials/[id]
: update a Tutorial by :id - DELETE
/api/tutorials/[id]
: delete a Tutorial by :id - DELETE
/api/tutorials
: delete all Tutorials - GET
/api/tutorials/published
: find all published Tutorials - GET
/api/tutorials?title=[keyword]
: find all Tutorials which title contains keyword
– We make CRUD operations & finder methods with Spring Data JPA’s JpaRepository.
– The database will be H2 Database (in memory or on disk) by configuring project dependency & datasource.
Run the Spring Boot JPA + H2 example
Run Spring Boot application with command: mvn spring-boot:run
.
tutorials table will be automatically generated in Database.
Let's open H2 console with url: http://localhost:8080/h2-ui
:
- For In-memory database:
- For on Disk database:
Click on Connect button, then check H2 database, you can see things like this:
Create some Tutorials:
H2 database tutorials
table after that:
Update some Tutorials:
The table data is changed:
Retrieve all Tutorials:
Retrieve a Tutorial by Id:
Find all published Tutorials:
Find all Tutorials which title contains string 'ot':
Delete a Tutorial:
Delete all Tutorials:
H2 database table is clean now:
Technology
- Java 8
- Spring Boot 2.4 (with Spring Web MVC, Spring Data JPA)
- H2 Database
- Maven 3.6.1
Project Structure
Let me explain it briefly.
– Tutorial
data model class corresponds to entity and table tutorials.
– TutorialRepository
is an interface that extends JpaRepository
for CRUD methods and custom finder methods. It will be autowired in TutorialController
.
– TutorialController
is a RestController which has request mapping methods for RESTful requests such as: getAllTutorials, createTutorial, updateTutorial, deleteTutorial, findByPublished…
– Configuration for Spring Datasource, JPA & Hibernate in application.properties.
– pom.xml contains dependencies for Spring Boot and H2 Database.
Source Code
Tutorial link with details, implementation (step by step) and Github:
https://bezkoder.com/spring-boot-jpa-h2-example/
JdbcTemplate instead:
Spring Boot JdbcTemplate example: CRUD Rest API
Further Reading
Using other databases:
– Spring JPA + PostgreSQL
– Spring JPA + MySQL
– Spring Data + MongoDB
If you want to add Pagination to this Spring project, you can find the instruction at:
Spring Boot Pagination & Filter example | Spring JPA, Pageable
To sort/order by multiple fields:
Spring Data JPA Sort/Order by multiple Columns | Spring Boot
Handle Exception for this Rest APIs is necessary:
– Spring Boot @ControllerAdvice & @ExceptionHandler example
– @RestControllerAdvice example in Spring Boot
Or way to write Unit Test for the JPA Repository:
Spring Boot Unit Test for JPA Repositiory with @DataJpaTest
Top comments (1)
Nice article.
But totally useless since we have spring-data-rest-webmvc. All you need is - just a model, and repository extends CrudRepository. That's it. Completely ;)