# Build an Employee Management System with Spring Boot: DTO, Validation, Exception Handling & JPA Explained (Beginner Friendly)

# 🚀 Learn Spring Boot by Building an Employee Management System

> "Have you ever wondered how companies manage thousands of employee records without getting lost in spreadsheets?"

Every company, from startups to large enterprises, needs a system to manage employee data efficiently.

In this article, we'll build an **Employee Management System** using **Spring Boot**, **MySQL**, and **Spring Data JPA** while learning concepts like **DTOs**, **validation**, **exception handling**, and **soft delete**.

---

## 📌 What Are We Building?

We are building a REST API that can:

- ✅ Create employees
- ✅ Get all employees
- ✅ Get employee by ID
- ✅ Update employee details
- ✅ Filter employees by department
- ✅ Soft delete employees
- ✅ Validate user input
- ✅ Handle exceptions globally

---

## 🛠️ Tech Stack

| Technology | Purpose |
|------------|----------|
| Java 21 | Programming language |
| Spring Boot | Backend framework |
| MySQL | Database |
| Spring Data JPA | Database operations |
| Bean Validation | Input validation |
| Maven | Dependency management |

---

## 🏗️ Project Architecture

A clean project structure makes your code easier to maintain.

```text
Client
   ↓
Controller
   ↓
Service
   ↓
Repository
   ↓
Database
```

### Controller

Handles HTTP requests and returns responses.

### Service

Contains all business logic.

### Repository

Communicates with the database.

### DTO

Transfers data safely between the client and server.

---

## 📊 Database Schema

| Field | Example |
|--------|----------|
| Name | Aman Sharma |
| Email | aman@gmail.com |
| Department | IT |
| Designation | Software Engineer |
| Salary | ₹50,000 |

---

## 🔥 API Endpoints

| Method | Endpoint | Description |
|---------|---------|-------------|
| GET | `/api/v1/employee` | Get all employees |
| GET | `/api/v1/employee/{id}` | Get employee by ID |
| POST | `/api/v1/employee` | Create employee |
| PUT | `/api/v1/employee/{id}` | Update employee |
| PATCH | `/api/v1/employee/{id}` | Soft delete employee |
| GET | `/api/v1/employee/filter?department=IT` | Filter by department |

---

## 📥 Sample Request

```json
{
  "name": "Aman Sharma",
  "email": "aman@gmail.com",
  "department": "IT",
  "designation": "Software Engineer",
  "salary": 50000
}
```

---

## 📦 Understanding DTO

DTO (**Data Transfer Object**) is used to transfer data between the client and the server.

Think of it like an employee ID card.

The company database contains a lot of information, but the ID card only shows the necessary details.

### Why use DTO?

- Keeps APIs clean
- Hides internal fields
- Improves security
- Makes validation easier

---

## ✅ Validation

Validation ensures that invalid data never reaches the database.

❌ Invalid request

```json
{
  "name": "",
  "email": "abc"
}
```

✅ Valid request

```json
{
  "name": "Rahul Sharma",
  "email": "rahul@gmail.com"
}
```

### Validation rules

- Name cannot be empty
- Email must be valid
- Salary must be greater than 100
- Department cannot be blank

---

## ⚠️ Global Exception Handling

Instead of returning confusing errors, we send meaningful messages.

### Resource not found

```json
{
  "message": "Employee not found with id: 10"
}
```

### Duplicate email

```json
{
  "message": "Email already exists"
}
```

### Validation error

```json
{
  "email": "Invalid email format"
}
```

---

## 🗑️ What Is Soft Delete?

Normally, deleting data removes it permanently.

Soft delete simply marks the record as deleted.

```java
employee.setDeleted(true);
```

### Benefits

- Data is not lost permanently
- Easy to restore
- Better auditing
- Fewer database issues

---

## 🚧 Challenges I Faced

As a beginner, I made several mistakes:

- Returning entities directly
- Forgetting validation
- Writing business logic inside controllers
- Ignoring exception handling
- Showing deleted records

These mistakes helped me understand how enterprise applications are built.

---

## 📚 What I Learned

After completing this project, I learned:

- DTOs
- Validation
- Exception handling
- Spring Data JPA
- REST API design
- Layered architecture
- Soft delete

---

## 🚀 Future Improvements

- Pagination and sorting
- Swagger documentation
- Spring Security
- Role-based authentication
- Unit testing

---

## 🎯 Conclusion

This project is much more than a simple CRUD application.

It teaches you how to build clean, maintainable, and production-ready backend applications using Spring Boot.

If you're a beginner, try building this project yourself. You'll learn much more than by simply watching tutorials.

⭐ If you found this article helpful, don't forget to share it!
