?? Day 15: My Spring Boot Learning Journey ??
Today, I took a deep dive into CRUD Operations in Spring Boot, which are the foundation of any database-driven application. CRUD (Create, Read, Update, Delete) operations allow applications to interact seamlessly with databases, enabling dynamic data management. Here's what I explored and practiced:
1?? Introduction to CRUD Operations
HTTP has a few important verbs.
Within a database, each of these operations maps directly to a series of commands. However, their relationship with a RESTful API is slightly more complex.
Standard CRUD Operation
2?? How CRUD Operations Works
In a Spring Boot application, CRUD operations follow a structured flow:
1. Client Request: The user interacts with the application, triggering a request to perform a CRUD operation. For example, adding a new record or viewing existing data.
2. Controller Layer: The request reaches the Controller, where specific endpoints handle it (e.g., /create, /read, /update, /delete).
3. Service Layer: The business logic is processed in the Service Layer, ensuring data validation and integrity.
4. Repository Layer: The Repository interacts directly with the database to execute the operation.
5. Response: Once the operation is complete, the server sends a response back to the client.
This structured flow ensures that data operations are efficient, secure, and scalable.
3?? Spring Boot CrudRepository
Syntax
where,
For example:
public?interface?StudentRepository?extends?CrudRepository<Student,?Integer>??
{??
}??
In the above example, we have created an interface named StudentRepository that extends CrudRepository. Where Student is the repository to manage, and Integer is the type of Id that is defined in the Student repository.
4?? Spring Boot JpaRepository
For example:
public?interface?BookDAO?extends?JpaRepository??? {?? }??
Why should we use these interfaces?
5?? Difference Between CrudRepository and JpaRepository
领英推荐
6?? CRUD Endpoints
These endpoints are implemented using HTTP methods:
Example Implementation of CRUD Endpoints:
@RestController
@RequestMapping("/employees")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@PostMapping
public Employee createEmployee(@RequestBody Employee employee) {
return employeeService.saveEmployee(employee);
}
@GetMapping("/{id}")
public Employee getEmployeeById(@PathVariable int id) {
return employeeService.getEmployeeById(id);
}
@PutMapping("/{id}")
public Employee updateEmployee(@PathVariable int id, @RequestBody Employee employee) {
return employeeService.updateEmployee(id, employee);
}
@DeleteMapping("/{id}")
public String deleteEmployee(@PathVariable int id) {
employeeService.deleteEmployee(id);
return "Employee deleted successfully!";
}
}
Annotations like @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping make it easy to define RESTful endpoints.
7?? Testing CRUD Operations
Example Test Scenarios:
1. POST: Test adding a new employee using a JSON request body.
2. GET: Verify that data is fetched correctly using /employees/{id}.
3. PUT: Check if updating an employee’s details works as intended.
4. DELETE: Test deleting an employee by their ID and ensure the record is removed from the database.
Postman allows you to define headers, request bodies, and test API responses interactively.