I. What is DAO mode
DAO (Data Access Object Pattern) separates the lower-level data manipulation APIs from the business logic layer in the upper layer, which mainly involves the following sections:
1.Data Access Object Interface
Defines the standard operating interface on the model object.
2.Data Access Object Concrete Class
The interface is implemented in 1, which is responsible for manipulating data from database or XML.
3.Model object or Value object
A simple Pojo object.
Two. DAO Pattern example
Model or Value object:student
Data Access Object Interface:studentdao
Concrete class Implement Data Access Object Interface:studentdaoimpl
Studentdao is used to show how to use the DAO pattern.
Step1
Student.java
public class Student { private String name; private int rollno; 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; }}
Step2
Data Access Object Interface
Studentdao.java
Import Java.util.list;public interface Studentdao {public list<student> getallstudents (); Public Student getstudent (int rollno); public void Updatestudent (Student Student); public void Deletestudent (Student Student);
Step3
Concrete class Implemting above interface
Studentdaoimpl.java
Importjava.util.ArrayList;Importjava.util.List; Public classStudentdaoimplImplementsStudentdao {//list is working as a databaseList<student>students; PublicStudentdaoimpl () {Students=NewArraylist<student>(); Student Student1=NewStudent ("Robert", 0); Student Student2=NewStudent ("John", 1); Students.add (STUDENT1); Students.add (Student2); } @Override Public voiddeletestudent (Student Student) {students.remove (Student.getrollno ()); System.out.println ("Student:roll No" + student.getrollno () + ", deleted from database"); } //retrive List of students from the database@Override PublicList<student>getallstudents () {returnstudents; } @Override PublicStudent Getstudent (intRollno) { returnStudents.get (Rollno); } @Override Public voidupdatestudent (Student Student) {students.get (Student.getrollno ()). SetName (Student.getname ()); System.out.println ("Student:roll No" + student.getrollno () + ", updated in the database"); }}
Step4
Use Studentdao to demonstrate DAO Pettern
public class Daopatterndemo {public static void Main (string[] args) { Studentdao Studentdao = new Studentdaoimpl ( ); Print all students for (Student student:studentDao.getAllStudents ()) { System.out.println ("Student: [ Rollno: "+ student.getrollno () +", Name: "+ student.getname () +"] "); } Update student student Student =studentdao.getallstudents (). get (0); Student.setname ("Michael"); Studentdao.updatestudent (student); Get the student studentdao.getstudent (0); System.out.println ("Student: [Rollno:" + student.getrollno () + ", Name:" + student.getname () + "]");} }
The DAO pattern used in the ancient poetry MySQL database