Spring Data | The Repository

Photo of

The Repository Layer of a Spring Boot Application

The service layer in the middle holds the business logic, and it is also required to communicate with the repository layer to fulfil any data needs as well. The Repository layer of the application will in turn communicate with the database.

Spring Data

In a typical application, The repository layer is where we write all the data access objects

Spring Data

When using Spring Data,

  • There’s no need to write data access objects.
  • You can use any JPA provider.
  • You also get automatic custom queries and manual custom queries.

How Spring Data works

There’s a comment class in the application, and there’s a comment table in the database.

Spring Data is going to do all the hard work getting the comment back and forth.

Spring Data

CRUD Repository Interface

You can create your Repository interface extending the CrudRepository interface which provides some basic crud operations.

Following are all the methods available through the CrudRepository interface

  • count()
  • delete(T entity)
  • deleteAll()
  • deleteAll(Iterable<? extends T> entities)
  • deleteById(ID id)
  • existsById(ID id)
  • findAll()
  • findAllById(Iterable ids)
  • findById(ID id)
  • save(S entity)
  • saveAll(Iterable<S> entities)