In this tutorial, you will learn what the @Respository annotation is and how to use it in your Spring Boot applications.
@Repository is a Spring annotation that indicates that the decorated class is a repository. A repository is a mechanism for encapsulating storage, retrieval, and search behaviour which emulates a collection of objects.
How to use @Repository Annotation
You will use @Repository annotation to indicate that the decorated class is a repository and that the class is a stereotype. This stereotype indicates that the class is responsible for storing, retrieving, and searching for objects.
To use the @Repository annotation in Spring Boot, you need to follow these steps:
Step 1: Annotate your repository interface with @Repository
@Repository public interface UserRepository extends CrudRepository<User, Long> { }
Notice that in the above code snippet, the @Repository annotation is used above the Java interface. Notice that the UserRepository extends the CrudRepository interface? Extending CrudRepository will allow you to perform basic CREATE, READ, UPDATE and DELETE database operations. You will be able to perform these operations without writing a single SQL query.
To learn how to use Spring Data JPA with custom SQL queries, check out the following tutorials:
- Spring Data JPA Native SQL Query,
- Spring Data JPA Native UPDATE SQL Query,
- Spring Boot REST and JPA – Save to a Database
Step 2: Enable Spring Data JPA
For the above code snippet to compile, you will need to add Spring Data JPA dependency to a pom.xml file of your project.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
Step 3: Configure Database Connection
@Repository annotation will help your Java class access the database and perform database-related operations. For it to work, we will need to configure database connection details in the application.properties file. Open the application.properties file of your Spring Boot project, and add the following properties:
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.jdbc.Driver
The above configuration is to access the MySQL database server. To learn how to configure the H2 in-memory database instead of MySQL, read the following tutorial: Add H2 Database to Spring Boot Project with Spring Security.
Step 4: Use Your Repository to Access Database
You can now inject a Repository into a Service class and use it to perform database-related operations. In the below code example, I use constructor-based dependency injection to inject the repository into a Service class.
@Service public class UserService { private final UserRepository userRepository; public UserService(UserRepository userRepository) { this.userRepository = userRepository; } public List<User> getAllUsers() { return userRepository.findAll(); } public User getUserById(long id) { return userRepository.findById(id).orElse(null); } public User addUser(User user) { return userRepository.save(user); } public void deleteUser(long id) { userRepository.deleteById(id); } }
That’s it!
You have now successfully used the @Repository annotation in your Spring Boot application.
To learn more, check out the Spring Boot tutorials page.
Happy learning!