...

/

Spring Data Repositories

Spring Data Repositories

Explore Spring Data repositories and learn how to use them to add persistence to the application.

What is a Spring Data repository?

A repository interface provides storage, retrieval, and search behavior over a collection of objects that usually persist in the database. Likewise, the Spring Data repository provides a complete abstraction over the data access layer in a project by using the @Repository annotation.

First, the repository interfaces offer various abstract methods for storage, retrieval, and search features. Then, Spring provides proxy implementations for the defined interfaces at runtime, reducing boilerplate code, which is necessary for creating an efficient data access layer that includes implementing and utilizing an entity manager. Let’s check out the repository interfaces available through the generic Spring Data Framework.

The CrudRepository interface

The CrudRepository is the most common repository interface of the Spring Data Framework that provides CRUD features through methods like save, findById, and delete. Also, it has other helper methods like existsById, findAll, and count.

Press + to interact
package org.springframework.data.repository;
public interface CrudRepository<T, ID> extends Repository<T, ID> {
<S extends T> S save(S entity);
<S extends T> Iterable<S> saveAll(Iterable<S> entities);
Optional<T> findById(ID id);
boolean existsById(ID id);
Iterable<T> findAll();
Iterable<T> findAllById(Iterable<ID> ids);
long count();
void deleteById(ID id);
void delete(T entity);
void deleteAllById(Iterable<? extends ID> ids);
void deleteAll(Iterable<? extends T> entities);
void deleteAll();
}
  • Line 5: The CrudRepository interface provides the abstract method
...