CRUD Operations Part 2 – Update and Delete Employee using Spring Boot

Introduction :

In Part 1, we created the Employee Management backend application using Spring Boot and PostgreSQL.

We implemented:

✅ Save Employee API

✅ Get All Employees API

✅ Get Employee By ID API

In this Part 2 tutorial, we will continue building the CRUD application by implementing:

✅ Update Employee API

✅ Delete Employee API

After completing this tutorial, our backend CRUD operations will be fully functional and ready for React integration.

Update Employee Service :

Open EmployeeService.java and add the following method:

public Employee updateEmployee(Long id, Employee employeeDetails) {

Employee employee = employeeRepository.findById(id)

        .orElseThrow(() -> new RuntimeException("Employee not found"));

employee.setName(employeeDetails.getName());

employee.setEmail(employeeDetails.getEmail());

employee.setDepartment(employeeDetails.getDepartment());

return employeeRepository.save(employee);

}

Explanation:

The employee is first fetched from the database using its ID.

Existing values are updated with the new data.

The updated employee record is saved back to the database.

Delete Employee Service :

Add the following method inside EmployeeService.java

public void deleteEmployee(Long id) {

Employee employee = employeeRepository.findById(id)

        .orElseThrow(() -> new RuntimeException("Employee not found"));

employeeRepository.delete(employee);

}

Explanation:

The employee record is fetched using the provided ID.

If the employee exists, the record is deleted from the database.

// Update Employee API

Open EmployeeController.java and add:

@PutMapping("/{id}")

public Employee updateEmployee(@PathVariable Long id,

                           @RequestBody Employee employee) {

return employeeService.updateEmployee(id, employee);

}

API Endpoint:

PUT /employees/{id}

Sample Request Body:

{
"name": "Sulochana",
"email": "sulochana@gmail.com",
"department": "IT"
}

// Delete Employee API

Add the following method inside EmployeeController.java

@DeleteMapping("/{id}")

public String deleteEmployee(@PathVariable Long id) {

employeeService.deleteEmployee(id);

return "Employee deleted successfully";

}

API Endpoint:

DELETE /employees/{id}

Testing APIs Using Postman

Update Employee

Method: PUT

URL:

http://localhost:8080/employees/1

Request Body:

{
"name": "Sulochana",
"email": "sulochana@gmail.com",
"department": "IT"
}

Delete Employee

Method: DELETE

URL:

http://localhost:8080/employees/1

Verify CRUD Operations

At this stage, our application supports all CRUD operations:

Create → Save Employee

Read → Get Employee(s)

Update → Modify Employee

Delete → Remove Employee

Conclusion

In this tutorial, we successfully completed all backend CRUD operations using Spring Boot and PostgreSQL.

We implemented:

✅ Update Employee API

✅ Delete Employee API

Now our backend application is fully CRUD-enabled.

In the next tutorial, we will integrate these APIs with React and build a complete Full Stack Employee Management Application.

Comments

Popular posts from this blog

React Basics – How to Create Component

How to Build REST API using Spring Boot (Step-by-Step Guide)

Spring Boot @RequestParam Annotation Explained with Real-Time Examples