Hello,
Today we will create a spring boot CRUD application. In, this example, we are using the MySQL database to store data, to view and manage the data we will use react Js library in the frontend part. For API testing we will use Postman.
In this article, We will create the required classes and interfaces. So, firstly we will create the Employee Repository Interface as EmployeeRepository. It will extend the JpaRepository Interface. The Interface will be annotated with @Repository annotation.
package com.crud.repository;
import com.crud.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee,Long> {
}
Then we will create the Service Interface as EmployeeService. In this interface, we will create the required method signatures without the body.
package com.crud.service;
public interface EmployeeService {
}
Then we will create the Service Implementation Class as EmployeeServiceImpl. In this class, we have implemented all the methods from the EmployeeService interface and also we will inject the EmployeeRepository using the @Autowired annotation. This class will be annotated with the @Service annotation.
package com.crud.serviceImpl;
import com.crud.repository.EmployeeRepository;
import com.crud.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
}
Finally, we will create the Controller class as EmployeeController. In this class. we will create the required APIs (request and response methods) and also we will inject the EmployeeServiceImpl class using @Autowired annotation. This class will be annotated with the @RestController annotation. and also we will use the @RequestMapping(“/api/v1”) for specifying the base API url.
package com.crud.controller;
import com.crud.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
}