Updated April 10, 2023
Definition of Spring boot repository
Spring boot is a java framework that is an extension to the Spring framework, by the use of this we can get rid of many manual configurations now they are managed by the spring boot framework itself. Spring boot framework provides us repository which is responsible to perform various operations on the object. To make any class repository in spring boot we have to use the repository annotation on that class, this annotation is provided by the spring framework itself. Repository annotation in spring boot is the specialization of the @Component annotation. In the coming section of the tutorial, we will see the internal working, and most importantly how we can use this annotation inside our application to understand its implementation as well for the beginners.
Syntax:
As we already know that repository in spring boot is an annotation that is used over a class to perform any operations on the object. In this section we will see how we can import this repository into our application for better understanding see below;
@Repository
public interface Name extends any_repository<Type, Type> {
}
As you can see in the above syntax for using repository we have used @Repository annotation to make any class a repository in spring boot. Also, we have to import the required package in order to use this annotation in our class otherwise we will get a compile-time error saying the package does not exist. Let’s take a look at the practice syntax for a repository in spring boot to see below;
e.g. :
@Repository
public interface AddEmployee extends JPARepository<String, String> {
// your logic goes here.
}
As you can see we have now defined some understandable code for beginners to understand the syntax. In the coming section of the tutorial, we will see its internal working and required dependency to use this inside the application to run it without error.
How does Spring boot repository work?
As know, we already know that repository in spring boot is used to perform the various activities on the object these operating includes deletion, storage, retrieval, storing list at a single time, and also searching from the database based on the particular criteria. @Repository annotation helps us to convert the non-spring-based expectation to a spring unchecked exception. This helps us to identify where does the problem occur. We can use this @repository annotation on the DAO classes in spring boot. Basically, we implement this repository as the interface in Java and extend different repositories available most commonly we used JPARepository to perform the crud operations on the object. This is a very important layer because all the transactions happened inside this only so we need to track this if any exception occurred. In this section we will see how we can use this in our application let’s get started to see below;
1) Dependency to be added:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
We need to add this dependency to use this inside our application, after that we can use any repository JpaRepository to perform any operations.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.4.2</version>
</dependency>
2) We should have both these in place to start with it. After that we can create one interface which is responsible to perform any operation on the object let’s taken a look at the syntax of it see below;
e.g. :
@Repository
public interface EmpRepository extends JpaRepository<String, String> {
// our methods if any
}
3) Once we have defined the repository and its required method then we can easily autowired this in our service class to use this and perform action based on it. see below for better understanding;
e.g. :
@Service
public class EmpService {
@Autowired
private EmpRepository empRepository;
}
As you can see in the above sample code that we have created one Employee service which will respond to interact with the repository, so we have auto wired the object of the repository inside the service class to invoke their methods.
4) Also we should already have our entity and dao classes in place to perform the further logic, your entity class should be like below;
e.g. :
@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;
@Column(name = "NAME")
private String name;
@Column(name = "CITY")
private String city;
@Column(name = "ROLL_NO")
private String rollNumber;
}
5) Now we can save. delete, search, retrieve or perform any operations which are required by the use of repository in spring boot. Let’s take a look at how we can do that see below;
// to create new employee ..
public String saveEmployee(Employee emp) {
System.out.println("Employee created successfully !!");
return empRepository.save(emp);
}
// to delete an object
public String deleteEmployee(Employee emp) {
System.out.println("Employee deleted successfully !!");
return empRepository.delete(emp);
}
// delete all
public String deleteAllEmployee() {
System.out.println("All employee deleted successfully");
return empRepository.deleteAll();
}
// find employee by id
public String findEmpById(Long Id) {
System.out.println("search emp");
return empRepository.findById(Id);
}
// find all employee
public String findEmpById() {
System.out.println("search all employee");
return empRepository.findAll();
}
To run any application or any example in spring boot application w have to do all the configuration these examples cannot be run by using any online compiler or tool we have to run this inside any edition like Intellij or eclipse etc. So set up all the necessary things which have described above and run them as spring boot application then it will start working.
NOTE :
1) Repository is the annotation that is required to perform actions on the object.
2) We can use crud repository or JPA repository to perform any operations like add, delete, save, find etc.
3) it is a specialization to @componant in spring boot.
Conclusion
As we have already seen it is very required annotation in spring boot, because it is responsible to interact with the objects store in the database also converts the exception to spring unchecked exceptions. To run this, we required service, dao, entity, and spring boot man class to be in place otherwise we will have an error and the application will not work.
Recommended Articles
This is a guide to Spring Boot Repository. Here we discuss the definition, syntax, How the Spring boot repository works? examples with code implementation. You may also have a look at the following articles to learn more –