Spring Boot Data Repository
Overview
In most of the applications developed in the past, the data layer design consists of multiple classes. But the application can only use simple CRUD operations on the database. Hence the design of the persistence layer forces the system to duplicate the logic across multiple DAO classes.
This violates the DRY(Do Not Repeat Yourself) principle.
Note: There is always a one-to-one mapping between DAO & Entity.
Spring boot offers a simplified model to design a data layer using a spring boot data repository.
Design Data Layer Using Spring Boot Data Repository
Spring boots simplify the Data Access Layer by providing a single, reusable implementation of a generic DAO, promoting a cleaner design.
To leverage the Spring Data programming model with JPA, our repository should extend one of the available Spring Data JPA repository interfaces. By extending the interface, we can perform basic CRUD operations and more without even writing a single line of code.
Spring Data JPA provides three types of repositories interfaces.
- CRUD repository - Add basic create, read, update and delete capabilities to the entity.
- PagingAndSortingRepository - It extends the CrudRepository and adds specific methods to sort and retrieve the data in a paginated way.
- JPA Repository - Extends both CRUD and PagingAndSortingRepository.
Great example of interface segregation principle.
Add Maven Dependency
We need to add the below dependency in our maven pom.
Starter dependency configures Datasource and entity manager for us. No need to configure it separately.
Choice of database
Even though there are numerous choices of databases, we will implement the solution using H2 in-memory DB. We can use any physical database for our implementation.
To use H2 DB in the application, the below dependency needs to be added.
Configure Data Source
By default, Spring Boot configures the application to connect to an in-memory store with the username sa and an empty password.
However, we can change those parameters by adding the following properties to the application.properties file:
Run the application. If you don't see any error, the application to database connection is successful.
Add Entity
Let's create an employee entity with a few attributes.
Add Repository
To interact with the database, we need to extend one of the existing repository classes from the spring data package.
We will extend JpaRepository.java
If we observe the decompiled source of the JpaRepository class, we’ll notice it contains all basic methods to perform CRUD operations on entities.
Our persistence layer is fully ready.
Conclusions
- spring-boot-starter-data-jpa provides an easy way to set up a spring data project and implement a data layer that requires almost no code.
- With the starter dependency, there is no need to configure entity manager and transaction manager beans.
- Spring Data JPA simplifies the creation of repositories because it can automatically create concrete implementations of their interfaces.