Java Data Access Object Pattern(數(shù)據(jù)訪問對象模式)

數(shù)據(jù)訪問對象模式(Data Access Object Pattern)或 DAO 模式用于把低級的數(shù)據(jù)訪問 API 或操作從高級的業(yè)務(wù)服務(wù)中分離出來。以下是數(shù)據(jù)訪問對象模式的參與者。

  • 數(shù)據(jù)訪問對象接口(Data Access Object Interface) - 該接口定義了在一個(gè)模型對象上要執(zhí)行的標(biāo)準(zhǔn)操作。
  • 數(shù)據(jù)訪問對象實(shí)體類(Data Access Object concrete class) - 該類實(shí)現(xiàn)了上述的接口。該類負(fù)責(zé)從數(shù)據(jù)源獲取數(shù)據(jù),數(shù)據(jù)源可以是數(shù)據(jù)庫,也可以是 xml,或者是其他的存儲(chǔ)機(jī)制。
  • 模型對象/數(shù)值對象(Model Object/Value Object) - 該對象是簡單的 POJO,包含了 get/set 方法來存儲(chǔ)通過使用 DAO 類檢索到的數(shù)據(jù)。
  1. 創(chuàng)建數(shù)值對象。
/**
 * 1. 創(chuàng)建數(shù)值對象。
 * @author mazaiting
 */
public class Student {
    private String name;
    private int rollNo;
    public Student(String name, int rollNo) {
        this.name = name;
        this.rollNo = rollNo;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getRollNo() {
        return rollNo;
    }
    public void setRollNo(int rollNo) {
        this.rollNo = rollNo;
    }
}
  1. 創(chuàng)建數(shù)據(jù)訪問對象接口。
/**
 * 2. 創(chuàng)建數(shù)據(jù)訪問對象接口。
 * @author mazaiting
 */
public interface StudentDao {
    public List<Student> getAllStudents();
    public Student getStudent(int rollNo);
    public void updateStudent(Student student);
    public void deleteStudent(Student student);
}
  1. 創(chuàng)建實(shí)現(xiàn)了上述接口的實(shí)體類。
/**
 * 3. 創(chuàng)建實(shí)現(xiàn)了StudentDao接口的實(shí)體類。
 * @author mazaiting
 */
public class StudentDaoImpl implements StudentDao{
    List<Student> students;
    public StudentDaoImpl(){
        students = new ArrayList<Student>();
        Student student1 = new Student("Rebert", 0);
        Student student2 = new Student("John", 1);
        students.add(student1);
        students.add(student2);
    }
    public List<Student> getAllStudents() {
        return students;
    }

    public Student getStudent(int rollNo) {
        return students.get(rollNo);
    }

    public void updateStudent(Student student) {
        students.get(student.getRollNo()).setName(student.getName());
          System.out.println("Student: Roll No " + student.getRollNo() 
             +", updated in the database");
    }

    public void deleteStudent(Student student) {
        students.remove(student.getRollNo());
          System.out.println("Student: Roll No " + student.getRollNo() 
             +", deleted from database");
    }

}
  1. 使用 StudentDao 來演示數(shù)據(jù)訪問對象模式的用法。
/**
 * 4. 使用 StudentDao 來演示數(shù)據(jù)訪問對象模式的用法。
 * @author mazaiting
 */
public class Client {
    public static void main(String[] args) {
        StudentDao studentDao = new StudentDaoImpl();
        
        // 輸出所有的學(xué)生
        for (Student student : studentDao.getAllStudents()) {
             System.out.println("Student: [RollNo : "
                        +student.getRollNo()+", Name : "+student.getName()+" ]");
        }
        
        // 更新學(xué)生
        Student student = studentDao.getAllStudents().get(0);
        student.setName("Michael");
        studentDao.updateStudent(student);
        
        // 獲取學(xué)生
        studentDao.getStudent(0);
        System.out.println("Student: [RollNo : "
                 +student.getRollNo()+", Name : "+student.getName()+" ]");  
        
    }
}
  1. 打印結(jié)果
Student: [RollNo : 0, Name : Rebert ]
Student: [RollNo : 1, Name : John ]
Student: Roll No 0, updated in the database
Student: [RollNo : 0, Name : Michael ]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,767評論 18 399
  • 國家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...
    庭說閱讀 11,183評論 6 13
  • 轉(zhuǎn)自:http://blog.csdn.net/jackfrued/article/details/4492194...
    王帥199207閱讀 8,618評論 3 93
  • 我沿著生機(jī)勃然的河岸走著,期待深郁色的河水泛上來浸濕我的鞋。但,不能如愿的,郁色的水毫不理睬我的心聲。連流淌的聲音...
    冬眠不覺曉閱讀 251評論 0 1