DAO (DataAccessObjectPattern)

Source: Internet
Author: User
What dao is always seen, but what is Dao? Dao is a design patter. It is called DataAccessObjectPatter. Next, I will translate a small article to explain it. DataAccessObjectPattern or DAO mode is used to separate low-level data access and advanced business logic operations. The following are some elements contained in DAO mode.

What dao is always seen, but what is Dao? Dao is a design Patter called Data Access Object Patter. Next, I will translate a small article to explain it. The Data Access Object Pattern or DAO mode is used to separate low-level Data Access and advanced business logic operations. The following are some elements contained in DAO mode.

What dao is always seen, but what is Dao? Dao is a design Patter called Data Access Object Patter. Next, I will translate a small article to explain it.

The Data Access Object Pattern or DAO mode is used to separate low-level Data Access and advanced business logic operations. The following are some elements contained in DAO mode.

· Data Access Object Interface-defines an Interface for standard operation methods on the model class.

· Data Access Object concrete class-implements the class of the above interface. This class is responsible for obtaining data from the data storage area (Database, xml.

· Model Object or Value Object-this is a simple POJO (plain Old Java Object) that contains the getter and setter methods. Access by DAO class.

Implementation

Creating a Student class represents a Model or Value object. StudentDao is a Dao interface. StudentDaoImpl is a class that implements StudentDao. See:


STEP 1.

Create model class, Student

package com.wly.dp.dao;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;}}
STEP 2.

Create a Dao interface, StudentDao

package com.wly.dp.dao;import java.util.List;public interface StudentDao {public List
 
   getAllStudents();public Student getStudent(int rollNo);public void updateStudent(Student student);public void deleteStudent(Student student);}
 
STEP 3.

Create a class that implements the Dao interface, StudentDaoImpl

Package com. wly. dp. dao; import java. util. ArrayList; import java. util. List; public class StudentDaoImpl implements StudentDao {List
 
  
Students; // simulate the public StudentDaoImpl () {students = new ArrayList
  
   
(); Student student1 = new Student ("Robert", 0); Student student2 = new Student ("John", 1); students. add (student1); students. add (student2) ;}@ Overridepublic List
   
    
GetAllStudents () {return students;} @ Overridepublic Student getStudent (int rollNo) {return students. get (rollNo) ;}@ Overridepublic void updateStudent (Student student) {students. get (student. getRollNo ()). setName (student. getName (); System. out. println ("Student: Roll No" + student. getRollNo () + ", updated in the database");} @ Overridepublic void deleteStudent (Student student) {students. remove (student. getRollNo (); System. out. println ("Student: Roll No" + student. getRollNo () + ", deleted from database ");}}
   
  
 
STEP 4.

Test

Package com. wly. dp. dao; public class Test {public static void main (String [] args) {StudentDao studentDao = new StudentDaoImpl (); // output all studentfor (Student student: studentDao. getAllStudents () {System. out. println ("Student: [RollNo:" + student. getRollNo () + ", Name:" + student. getName () + "]");} // update studentStudent = studentDao. getAllStudents (). get (0); student. setName ("Michael"); studentDao. updateStudent (student); // studentstudentDao after the update is obtained. getStudent (0); System. out. println ("Student: [RollNo:" + student. getRollNo () + ", Name:" + student. getName () + "]") ;}}
STEP 5.

Verify output

Student: [RollNo : 0, Name : Robert ]Student: [RollNo : 1, Name : John ]Student: Roll No 0, updated in the databaseStudent: [RollNo : 0, Name : Michael ]


O la ~~~

Repost please keep Source: http://blog.csdn.net/u011638883/article/details/13093701

Thank you !!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.