Core Components
-
DAO Interface:
Defines the standard operations to be performed on a model object(s).
Example:
StudentDaointerface with methods likegetAllStudents()orupdateStudent(). - DAO Implementation Class: Implements the DAO interface. This class is responsible for getting data from a data source (Xml, database, etc.), which can be complex and specific to the database technology (JDBC, Hibernate, etc.).
-
Model / Transfer Object:
Simple POJO (Plain Old Java Object) containing
getandsetmethods to store data retrieved using the DAO class.
Real-World Analogy
Think of a Human Resources (HR) Department:- The Manager (Business Logic) needs to update an employee’s salary.
- The Manager does not go to the physical file cabinet or the secure server room to change the record personally.
- Instead, they send a request form to the HR Clerk (DAO).
- The HR Clerk knows exactly which file cabinet, folder, or database system holds the record and how to access it securely.
- The Manager doesn’t care if the records are on paper, in Excel, or in the Cloud—they just trust the Clerk to handle the data access.
Implementation Example
Here is a raw Java implementation. This example simulates a database using a simpleList to keep the code focused on the structure.
1. Create the Model
The value object we are persisting.Student.java
2. Create the DAO Interface
This defines what we can do, not how we do it.StudentDao.java
3. Create the DAO Implementation
This defines how we access the data. In a real app, this would contain JDBC or SQL code.StudentDaoImpl.java
4. Demo Class
The business logic uses the Interface, not the Implementation class directly.DaoPatternDemo.java
When to Use
- Database Swapping: When you might need to change the underlying database (e.g., from MySQL to Oracle) without rewriting the business logic.
- Complex Queries: When you want to encapsulate complex SQL logic away from the main application code.
- Testing: It makes Unit Testing easier because you can create a
MockStudentDaothat returns dummy data without connecting to a real database.
Pros & Cons
| Pros | Cons |
|---|---|
| Separation: Keeps domain logic clean and separate from database scripts. | Boilerplate: Requires creating interfaces and implementations for every entity. |
| Security: Centralizes data access, making it easier to audit and secure. | Complexity: Adds an extra layer of abstraction which might be unnecessary for very small apps. |
| Maintainability: Changes in the database schema only affect the DAO class, not the whole app. |
Spring Boot Context
In the modern Spring ecosystem, the DAO pattern has evolved significantly into the Repository Pattern. While the concept remains the same (abstracting data access), Spring Data JPA eliminates the need to write theStudentDaoImpl class manually.
The Spring Data Way
Instead of writing the implementation code, you simply create an interface that extendsJpaRepository. Spring generates the implementation code at runtime (using Proxies).
StudentDao(Old J2EE)StudentRepository(Spring Boot)- The annotation
@Repositoryis a specialization of@Componentthat also enables automatic persistence exception translation.