CRUD Operations Part 1 – Backend Development using Spring Boot

 Introduction :

In this tutorial, we will start building a CRUD Application using Spring Boot and PostgreSQL.

In this Part 1 blog, we will focus only on Backend Development.

We will create:

✅ Employee Entity
✅ Repository Layer
✅ Service Layer
✅ Controller Layer
✅ REST APIs using Spring Boot

✅ Save Employee Data
✅ Get All Employees
✅ Get Employee By ID

At the end of this, our backend APIs will be ready to integrate with React frontend.

What is CRUD?

CRUD stands for:

  • Create → Save Data
  • Read → Fetch Data
  • Update → Modify Data
  • Delete → Remove Data

PostgreSQL Configuration :

Configure database details inside application.properties

spring.datasource.url=jdbc:postgresql://localhost:5432/employee_db

spring.datasource.username=postgres // here you can use your db username

spring.datasource.password=postgres // here you can use db your password

spring.jpa.hibernate.ddl-auto=update

spring.jpa.show-sql=true

Employee Entity :

Create Employee.java

@Entity

@Getter

@Setter

@Table(name = "employees")

public class Employee {

    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private Long id;

    private String name;

    private String email;

    private String department;

}

This entity represents employee data stored in the database.

Repository Layer :

Create EmployeeRepository.java

@Repository

public interface EmployeeRepository extends JpaRepository<Employee, Long> {

}

The Repository layer helps communicate with the database.

By extending JpaRepository, Spring Boot automatically provides methods like:

  • save()
  • findAll()
  • findById()

Service Layer :

Create EmployeeService.java

@Service

public class EmployeeService {

    @Autowired

    private EmployeeRepository employeeRepository;

    // Save Employee

    public Employee saveEmployee(Employee employee) {

        return employeeRepository.save(employee);

    }

    // Get All Employees

    public List<Employee> getAllEmployees() {

        return employeeRepository.findAll();

    }

    // Get Employee By ID

    public Employee getEmployeeById(Long id) {

        return employeeRepository.findById(id).orElseThrow(() ->

               new RuntimeException("Employee not found"));

    }

}

Controller Layer:

Create EmployeeController.java

@RestController

@RequestMapping("/employees")

@CrossOrigin(origins = "http://localhost:3000") // you use origin = '*'

public class EmployeeController {

    @Autowired

    private EmployeeService employeeService;

    // Save Employee API

    @PostMapping

    public Employee saveEmployee(@RequestBody Employee employee) {

        return employeeService.saveEmployee(employee);

    }

    // Get All Employees API

    @GetMapping

    public List<Employee> getAllEmployees() {

        return employeeService.getAllEmployees();

    }

    // Get Employee By ID API

    @GetMapping("/{id}")

    public Employee getEmployeeById(@PathVariable Long id) {

        return employeeService.getEmployeeById(id);

    }

}

Conclusion :

In this tutorial, we successfully created the backend part of our CRUD Application using Spring Boot and PostgreSQL.


In the next blog, we will continue with:

πŸ‘‰ Update Employee API
πŸ‘‰ Delete Employee API

After completing all backend CRUD operations, we will integrate these APIs with the React frontend and build a complete Full Stack CRUD 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