?? Day 15: My Spring Boot Learning Journey ??

?? 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

  • The CRUD stands for Create, Read/Retrieve, Update, and Delete. These are the four basic functions of the persistence storage.
  • The CRUD operation can be defined as user interface conventions that allow view, search, and modify information through computer-based forms and reports. CRUD is data-oriented and the standardized use of HTTP action verbs.

HTTP has a few important verbs.

  1. POST: Creates a new resource
  2. GET: Reads a resource
  3. PUT: Updates an existing resource
  4. DELETE: Deletes a resource

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

  • CREATE Operation: It performs the INSERT statement to create a new record.
  • READ Operation: It reads table records based on the input parameter.
  • UPDATE Operation: It executes an update statement on the table. It is based on the input parameter.
  • DELETE Operation: It deletes a specified row in the table. It is also based on the input parameter.

2?? How CRUD Operations Works

  • CRUD operations are at the foundation of the most dynamic websites. Therefore, we should differentiate CRUD from the HTTP action verbs.
  • Suppose, if we want to create a new record, we should use HTTP action verb POST. To update a record, we should use the PUT verb. Similarly, if we want to delete a record, we should use the DELETE verb. Through CRUD operations, users and administrators have the right to retrieve, create, edit, and delete records online.
  • We have many options for executing CRUD operations. One of the most efficient choices is to create a set of stored procedures in SQL to execute operations.
  • The CRUD operations refer to all major functions that are implemented in relational database applications. Each letter of the CRUD can map to a SQL statement and HTTP methods.


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

  • Spring Boot provides an interface called CrudRepository that contains methods for CRUD operations. It is defined in the package org.springframework.data.repository.
  • It extends the Spring Data Repository interface. It provides generic Crud operation on a repository.
  • If we want to use CrudRepository in an application, we have to create an interface and extend the CrudRepository.

Syntax

  1. public?interface?CrudRepository<T,ID>?extends?Repository<T,ID>??

where,

  • T is the domain type that repository manages.
  • ID is the type of the id of the entity that repository manages.

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

  • JpaRepository provides JPA related methods such as flushing, persistence context, and deletes a record in a batch. It is defined in the package org.springframework.data.jpa.repository. JpaRepository extends both CrudRepository and PagingAndSortingRepository.

For example:

public?interface?BookDAO?extends?JpaRepository??? {?? }??

Why should we use these interfaces?

  • The interfaces allow Spring to find the repository interface and create proxy objects for that.
  • It provides methods that allow us to perform some common operations. We can also define custom methods as well.


5?? Difference Between CrudRepository and JpaRepository



6?? CRUD Endpoints

  • In Spring Boot, RESTful APIs are used to expose CRUD operations.

These endpoints are implemented using HTTP methods:

  1. POST: For Create operations.
  2. GET: For Read operations.
  3. PUT/PATCH: For Update operations.
  4. DELETE: For Delete operations.

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

  • Testing CRUD APIs is a crucial step to ensure data operations work as expected. Tools like Postman or cURL are commonly used for this purpose.

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.


要查看或添加评论,请登录

Sakthi Kumar M的更多文章

  • ?? Day 14: My Spring Boot Learning Journey ??

    ?? Day 14: My Spring Boot Learning Journey ??

    Today, I dived into Spring Boot integration with MySQL databases, a critical step in managing data for dynamic and…

  • ?? Day 13: My Spring Boot Learning Journey ??

    ?? Day 13: My Spring Boot Learning Journey ??

    Today, I explored Spring Boot JDBC and how it simplifies database connectivity and operations. This hands-on experience…

  • ?? Day 12: My Spring Boot Learning Journey ??

    ?? Day 12: My Spring Boot Learning Journey ??

    Today, I explored the world of Spring Boot JPA (Java Persistence API) and learned how it simplifies database operations…

  • ?? Day 11: My Spring Boot Learning Journey ??

    ?? Day 11: My Spring Boot Learning Journey ??

    Today, I delved into Spring Boot AOP (Aspect-Oriented Programming) and gained insights into how it helps in…

  • ?? Day 10: My Spring Boot Learning Journey ??

    ?? Day 10: My Spring Boot Learning Journey ??

    Today, I created a Spring Boot Hello World Project using Thymeleaf, taking my first step toward building dynamic web…

  • ?? Day 9: My Spring Boot Learning Journey ??

    ?? Day 9: My Spring Boot Learning Journey ??

    Today, I began exploring Spring Boot with Thymeleaf, a powerful template engine for creating dynamic web pages. Here's…

  • ?? Day 8: My Spring Boot Learning Journey ??

    ?? Day 8: My Spring Boot Learning Journey ??

    Today, I took my first step towards building real-world applications by creating a Spring Boot Hello World project!…

  • ?? Day 7: My Spring Boot Learning Journey ??

    ?? Day 7: My Spring Boot Learning Journey ??

    Today, I explored some critical concepts that make Spring Boot applications flexible and efficient. Here's what I…

    1 条评论
  • ?? Day 6: My Spring Boot Learning Journey ??

    ?? Day 6: My Spring Boot Learning Journey ??

    Today's session was all about exploring tools and features that enhance development and monitoring in Spring Boot…

    1 条评论
  • ?? Day 5: My Spring Boot Learning Journey ??

    ?? Day 5: My Spring Boot Learning Journey ??

    Today, I dived into the world of Spring Boot Starters and their role in simplifying project configurations. Here's what…

    1 条评论

社区洞察

其他会员也浏览了