Java Data Access Object pattern

Mode data access object (Data Access Object Pattern) for the DAO mode or a low-level API or data access operations from advanced business services. The following is a participant data access object model.

  • Interface data access object (the Data Object Access Interface) - This defines a standard interface to operate on a model of the object to be executed.
  • Data Access Objects entity classes (the Data Object Access Concrete class) - This class implements the above interface. Class is responsible for data acquisition from the data source, data source can be a database, it can be xml, storage or other mechanisms.
  • Model object / target value (the Model Object / the Value Object) - the object is a simple POJO, containing get / set methods DAO classes to store data retrieved by using.

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;
       }
    
}

StudentDao.java

import java.util.List;

public interface StudentDao {// create a data access object interface
    
    public List <Student> getAllStudents ();
       public Student getStudent (int rollNo);
       public void updateStudent (Student Student);
       public void deleteStudent (Student Student);
    
}

StudentDaoImpl.java

import java.util.ArrayList;
import java.util.List;
 
public class StudentDaoImpl implements StudentDao {
   
   //列表是当作一个数据库
   List<Student> students;
 
   public StudentDaoImpl(){
      students = new ArrayList<Student>();
      Student student1 = new Student("Robert",0);
      Student student2 = new Student("John",1);
      students.add(student1);
      students.add(student2);    
   }
   @Override
   public void deleteStudent(Student student) {
      students.remove(student.getRollNo());
      System.out.println("Student1: Roll No " + student.getRollNo()
         +", deleted from database");
   }
 
   //从数据库中检索学生名单
   @Override
   public List<Student> getAllStudents() {
      return students;
   }
 
   @Override
   public Student getStudent(int rollNo) {
      return students.get(rollNo);
   }
 
   @Override
   public void updateStudent(Student student) {
      students.get(student.getRollNo()).setName(student.getName());
      System.out.println("Student2: Roll No " + student.getRollNo()
         +", updated in the database");
   }
}

DaoPatternDemo.java

public class DaoPatternDemo {// StudentDAO used to demonstrate the usage of the data access object model
    
    public static void main (String [] args) {
        StudentDAO StudentDAO new new StudentDaoImpl = ();
        // output of all students
        for (Student student: studentDao.getAllStudents ( )) {
            System.out.println ( "Student3: [RollNo:"
                    + student.getRollNo () + ", the Name:" + student.getName () + "]");
        }
        
        // update the student
        student student = studentDao. . getAllStudents () gET (0);
        student.setName ( "Walker");
        studentDao.updateStudent (student);
        
        // get students
        studentDao.getStudent (0);
        System.out.println ( "Student4: [RollNo:"
                 +student.getRollNo()+", Name : "+student.getName()+" ]");  
    }
    
}

 

Guess you like

Origin blog.csdn.net/walkerwqp/article/details/91970381