古詩MySQL資料庫中使用到的DAO模式

來源:互聯網
上載者:User

標籤:

一.什麼是DAO模式

  DAO(Data Access Object Pattern)用於將低層的資料操作API與上層的商務邏輯層分離,其主要涉及以下幾個部分:

1.Data Access Object Interface

定義了在model object上的標準操作介面。

2.Data Access Object concrete class

實現了1中的介面,負責從database或者xml等中操作資料。

3.Model Object or Value Object

簡單的POJO對象。

 

二.DAO模式例子
Model or Value Object :Student

Data Access Object Interface: StudentDAO

Concrete class Implement Data Access Object Interface:StudentDaoImpl

StudentDAO用來展示如何使用DAO模式。

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

import java.util.ArrayList;import java.util.List;public class StudentDaoImpl implements StudentDao {       //list is working as a database   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("Student: Roll No " + student.getRollNo() + ", deleted from database");   }   //retrive list of students from the database   @Override   public List<Student> getAllStudents() {      return students;   }   @Overridepublic Student getStudent(int rollNo) {      return students.get(rollNo);   }   @Override   public void updateStudent(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() + " ]");   }}

 

古詩MySQL資料庫中使用到的DAO模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.