Skip to main content
J2EE Patterns: Data Access Object. The Data Access Object (DAO) pattern provides an abstract interface to some type of database or other persistence mechanism. By mapping application calls to the persistence layer, the DAO provides some specific data operations without exposing details of the database. In simple terms, it isolates the application/business layer from the persistence layer (usually a relational database) using an abstract API.

Core Components

  • DAO Interface: Defines the standard operations to be performed on a model object(s). Example: StudentDao interface with methods like getAllStudents() or updateStudent().
  • 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 get and set methods 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 simple List 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 MockStudentDao that returns dummy data without connecting to a real database.

Pros & Cons

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 the StudentDaoImpl class manually.

The Spring Data Way

Instead of writing the implementation code, you simply create an interface that extends JpaRepository. Spring generates the implementation code at runtime (using Proxies).
  • StudentDao (Old J2EE) StudentRepository (Spring Boot)
  • The annotation @Repository is a specialization of @Component that also enables automatic persistence exception translation.