The data access object pattern (Data Access Object Pattern) or DAO pattern is used to separate low-level data access API or operations from high-level business services. The following are the participants in the data access object pattern.
数据访问对象接口(Data Access Object Interface) -this interface defines the standard operations to be performed on a model object.
数据访问对象实体类(Data Access Object concrete class) -this class implements the above interface. This class is responsible for fetching data from a data source, which can be a database, xml, or other storage mechanism.
模型对象/数值对象(Model Object/Value Object) This object is a simple POJO that contains get/set methods to store the data retrieved by using the DAO class.
6.31.1. Realize ¶
We will create a model object or a numeric object Student Object. StudentDao Is the data access object interface. StudentDaoImpl Is an entity class that implements the data access object interface. DaoPatternDemo Our demo class uses the StudentDao To demonstrate the use of the data access object pattern.

6.31.2. Step 1 ¶
Create numeric objects.Student.java ¶
publicclassStudent{privateStringname;privateintrollNo;Student(Stringname,introllNo){this.name=name;this.rollNo=rollNo;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicintgetRollNo(){returnrollNo;}publicvoidsetRollNo(introllNo){this.rollNo=rollNo;}}
6.31.3. Step 2 ¶
Create a data access object interface.StudentDao.java ¶
importjava.util.List;publicinterfaceStudentDao{publicList<Student>getAllStudents();publicStudentgetStudent(introllNo);publicvoidupdateStudent(Studentstudent);publicvoiddeleteStudent(Studentstudent);}
6.31.4. Step 3 ¶
Create an entity class that implements the above interface.StudentDaoImpl.java ¶
importjava.util.ArrayList;importjava.util.List;publicclassStudentDaoImplimplementsStudentDao{//列表是当作一个数据库List<Student>students;publicStudentDaoImpl(){students=newArrayList<Student>();Studentstudent1=newStudent("Robert",0);Studentstudent2=newStudent("John",1);students.add(student1);students.add(student2);}@OverridepublicvoiddeleteStudent(Studentstudent){students.remove(student.getRollNo());System.out.println("Student:
Roll No"+student.getRollNo()+", deleted from
database");}//从数据库中检索学生名单@OverridepublicList<Student>getAllStudents(){returnstudents;}@OverridepublicStudentgetStudent(introllNo){returnstudents.get(rollNo);}@OverridepublicvoidupdateStudent(Studentstudent){students.get(student.getRollNo()).setName(student.getName());System.out.println("Student:
Roll No"+student.getRollNo()+", updated in the database");}}
6.31.5. Step 4 ¶
Use StudentDao To demonstrate the use of the data access object pattern.DaoPatternDemo.java ¶
publicclassDaoPatternDemo{publicstaticvoidmain(String[]args){StudentDaostudentDao=newStudentDaoImpl();//输出所有的学生for(Studentstudent:studentDao.getAllStudents()){System.out.println("Student:
[RollNo :"+student.getRollNo()+", Name
:"+student.getName()+"]");}//更新学生Studentstudent=studentDao.getAllStudents().get(0);student.setName("Michael");studentDao.updateStudent(student);//获取学生studentDao.getStudent(0);System.out.println("Student:
[RollNo :"+student.getRollNo()+", Name :"+student.getName()+"]");}}
6.31.6. Step 5 ¶
Execute the program and output the result:
Student: [RollNo : 0, Name : Robert ]
Student: [RollNo : 1, Name : John ]
Student: Roll No 0, updated in the database
Student: [RollNo : 0, Name : Michael ]