VOOZH about

URL: https://www.geeksforgeeks.org/springboot/spring-repository-annotation-with-example/

⇱ Spring Boot @Repository Annotation with Example - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring Boot @Repository Annotation with Example

Last Updated : 3 Jun, 2026

The @Repository annotation is used in the persistence layer of a Spring application to interact with the database. Classes marked with @Repository contain the code required to perform CRUD operations and other database-related tasks. It acts as a bridge between the business layer and the database layer, helping maintain a clean separation of responsibilities within the application.

  • Provides automatic translation of database-specific exceptions into Spring's DataAccessException hierarchy.
  • Works seamlessly with Spring Data JPA, Hibernate, JDBC, and other persistence technologies.
  • Enables dependency injection so repository objects can be used in service classes.
  • Improves code organization by keeping database logic in a dedicated layer.
  • Supports easier testing and maintenance through proper layer separation.

Use Cases of @Repository

  • Saving Data into a Database: Used to insert records such as employees, students, customers, or products.
  • Retrieving Data from a Database: Fetching all records or a specific record based on an identifier.
  • Updating Existing Records: Modifying employee details, customer information, or product data.
  • Deleting Records: Removing unwanted or outdated data from the database.
  • Custom Database Queries: Executing JPQL, HQL, native SQL, or derived query methods to retrieve filtered data.
  • Implementing Data Access Layer (DAO): Centralizing all database interaction logic in a dedicated repository layer.

Steps to Implements the @Repository Annotation

Let's consider a simple example to understand how to use the @Repository annotation in a Spring Boot application.

Step 1: Create a Simple Spring Boot Project

Refer to this article Create and Setup Spring Boot Project in Eclipse IDE and create a simple spring boot project. 

Step 2: Add the Spring-Context Dependency

In your pom.xml file, add the following spring-context dependency.

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>5.3.13</version>

</dependency>

Step 3: Create Packages and Classes

Create two packages: entity and repository. Inside the entity package, create a Student class. In the repository package, create a generic interface named DemoRepository and a class named StudentRepository.

πŸ‘ Image

Step 4: Create the Entity Class

Create an entity class for which we will implement a spring repository. Here our entity class is Student. Below is the code for the Student.java file. This is a simple POJO (Plain Old Java Object) class in Java. 

Student.java:

Step 5: Create the Repository Interface

Before implementing the Repository class we have created a generic DemoRepository interface to define the contract for our repository class. Below is the code for the DemoRepository.java file.

Step 6: Implement the Repository Class

Now let’s look at the implementation of our StudentRepository class.

In this StudentRepository.java file, you can notice that we have added the @Repository annotation to indicate that the class provides the mechanism for storage, retrieval, update, delete and search operation on objects.

πŸ‘ Image

Note: Here we have used an in-memory Map to store the object data, you can use any other mechanisms too. In the real world, we use Databases to store object data. 

Step 7: Spring Repository Test

Now our Spring Repository is ready, let’s test it out. Go to the DemoApplication.java file and refer to the below code. 

Step 8: Run the Application

  • Open DemoApplication.java.
  • Right-click ->Run As ->Spring Boot App.

Output:

πŸ‘ Image
Comment
Article Tags:

Explore