Spring Data

What is Spring-Data?

Spring-Data is a part of Spring family whose purpose is to easily access relational database systems, NoSQL data stores and GraphQL data stores.

Brief Description

In the above diagram, I represented an architecture diagram of spring-data connecting with respective databases.

  • First layer is the base interface layer consists of CrudRepository, PagingAndSortingRepository.
  • Second layer is that layer used for respective databases like JpaRepository used for RDBMS, MongoRepository used for MongoDB, GraphRepository used for Neo4j DB.
  • All the above derived Repository Interface extends the Base Repository Interface as JpaRepository extends CrudRepository, MongoRepository extends PagingAndSortingRepository and GraphRepsoitory extends PagingAndSortingRepository.
  • Now the final step is to define the Drivers and Dialect by which, from code it will connect to the respective databases.

Why Spring Data?

Introduction to Spring Data reduces a lot of the boilerplate code which we used to write earlier to make a JDBC Connection.

Earlier we used to write a DAO layer which will holds the business logic interacting with the Model and the Model connects with the database. In this DAO Layer we used to write everything like loading the Driver class as required, opening a connection, write a query, based on the query action either it inserts or updates or deletes or fetches the data from DB, converts the data from Model to an object which will be best fitted in the application and last of all to close the connection. All these above activities are now taken care of Spring-Data if you use it in your application or project.

Reasons to use Spring-Data

There are 3 most important and valid reasons of using Spring-Data over the legacy implementation to connect to database.

  1. No-code Repositories – One of the most popular persistence-related patterns is the Repository Pattern. It helps the developer to only focus on the implementation of business logic and hides the data store specific implementation details. It also helps not to write the repetitive standard CRUD operations for each entity. It provides a set of repository interfaces from where you will extend to define a specific repository for one of your entities.
  2. Reduce boilerplate code – Spring-Data provides a default implementation for each method like read or write operations. That means you don’t have to write the basic implementation logic for read and write operations excluding the business logic.
  3. Generated Queries – Spring-Data generates the database queries based on their method names. You just need to define a method in your repository interface with a name that starts with findBy. Spring parses the method names and creates a JPQL query, sets the provided method parameters as bind parameter values, executes the query, and returns the result.

I have provided child pages where you can experience the usage of Spring Data using Spring REST with various types of databases like MySQL (for RDMBS), Mongo (for NoSQL DB), and Neo4j (a GraphQL DB).

Spring Data Implementations with Different Types of Databases

Spring Data using RDBMS (MySQL DB) and Spring REST

Spring Data using NoSQL DB and Spring REST

Spring Data using Cypher Neo4j DB and Spring REST

Spring Data using Redis and Spring REST

3 Comments

Comments are closed