When you first start learning backend development, you’ll often come across a folder structure that looks something like this:
controller/
entity/
repository/
service/
If you’re new, this might feel confusing. What do these folders mean? How are they connected? And how does the data flow from one to another?
In this article, let’s break it down step by step in beginner-friendly language so you can clearly understand how the backend works.
📂 The Four Main Layers
1. Entity Folder
This folder defines the blueprint of your data. Each class inside entity/
represents a database table.
- One object = one row in the database.
- Often mapped with annotations (like
@Entity
in Java Spring Boot).
Example:
@Entity
public class User {
@Id
private Long id;
private String name;
private String email;
}
Think of Entity as the “ingredients” of your application — the raw data.
2. Repository Folder
This is where your app talks to the database. The repository is responsible for all CRUD operations (Create, Read, Update, Delete).
Example:
public interface UserRepository extends JpaRepository<User, Long> {
User findByEmail(String email);
}
The Repository is like the pantry in a restaurant — it stores and fetches the ingredients (data).
3. Service Folder
This is the business logic layer — the brain of your application.
- It decides what to do with the data.
- Calls the repository whenever data needs to be saved or fetched.
Example:
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User createUser(User user) {
return userRepository.save(user);
}
public User getUserByEmail(String email) {
return userRepository.findByEmail(email);
}
}
The Service is like the chef in a restaurant — deciding how to prepare the food using ingredients from the pantry.
4. Controller Folder
This is the entry point of your backend.
- Handles HTTP requests (e.g.,
/users
,/login
). - Passes the request to the Service.
- Returns a response (usually JSON) back to the client or frontend.
Example:
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@PostMapping
public User createUser(@RequestBody User user) {
return userService.createUser(user);
}
@GetMapping("/{email}")
public User getUserByEmail(@PathVariable String email) {
return userService.getUserByEmail(email);
}
}
The Controller is like the waiter — taking orders from the customer and serving the food back.
🔄 How the Flow Works
Here’s how a typical request flows in this architecture:
-
Client/Frontend sends a request (e.g.,
POST /users
). - Controller receives the request and hands it to the Service.
- Service applies business logic and calls the Repository.
- Repository fetches or saves data in the database via Entities.
- The result flows back: Repository → Service → Controller → Client.
🧠 Restaurant Analogy
To make it simple, imagine your backend is a restaurant:
- Controller = Waiter (takes the order from the customer).
- Service = Chef (decides how to cook the food).
- Repository = Pantry (where ingredients are stored).
- Entity = Ingredients (rice, chicken, spices).
Flow: Customer → Waiter → Chef → Pantry → Chef → Waiter → Customer.
🚀 Final Thoughts
Understanding this structure is the first step to writing clean, maintainable backend code. The separation of responsibilities makes it easier to debug, test, and scale your application.
Next time you see these four folders, you’ll know exactly how they connect — from handling requests to saving data in the database.
Follow me on : Github Linkedin Threads Youtube Channel
Top comments (0)