search
HomeDatabaseMysql TutorialDAO(Data Access Object Pattern)

总是看到什么什么dao的,但是Dao到底是什么?Dao是一种设计模式,全称Data Access Object Patter。下面翻译一篇小文章,以作解释。 Data Access Object Pattern 或者DAO 模式被用来分离低层次数据访问和高级业务逻辑操作。 下面是一些DAO模式中包含的元素。

总是看到什么什么dao的,但是Dao到底是什么?Dao是一种设计模式,全称Data Access Object Patter。下面翻译一篇小文章,以作解释。

Data Access Object Pattern 或者DAO 模式被用来分离低层次数据访问和高级业务逻辑操作。下面是一些DAO模式中包含的元素。

· Data Access Object Interface - 定义了一个model类上标准的操作方法的接口。

· Data Access Object concrete class - 实现了上面接口的类。这个类负责从数据存储区(数据库、xml)中取得数据。

· Model Object or Value Object - 这是一个简单地POJO(plain Old Java Object)包含了getter和setter方法。由DAO类进行存取。

实现

创建一个Student类代表了Model或Value对象。StudentDao是一个Dao接口。StudentDaoImpl是一个实现StudentDao的类。见下图:

DAO(Data Access Object Pattern)

STEP 1.

创建model类,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.

创建一个Dao接口,StudentDao

package com.wly.dp.dao;

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);
}
</student>
STEP 3.

创建一个实现Dao接口的类,StudentDaoImpl

package com.wly.dp.dao;

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 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("Student: Roll No " + student.getRollNo() 
	         +", updated in the database");
	}

	@Override
	public void deleteStudent(Student student) {
		students.remove(student.getRollNo());
	      System.out.println("Student: Roll No " + student.getRollNo() 
	         +", deleted from database");
	}

}
</student></student></student>
STEP 4.

测试类,Test

package com.wly.dp.dao;

public class Test {

	public static void main(String[] args) {
		StudentDao studentDao = new StudentDaoImpl();

		// 输出所有student
		for (Student student : studentDao.getAllStudents()) {
			System.out.println("Student: [RollNo : " + student.getRollNo()
					+ ", Name : " + student.getName() + " ]");
		}

		// 更新一个student
		Student student = studentDao.getAllStudents().get(0);
		student.setName("Michael");
		studentDao.updateStudent(student);

		// 得到更新后的student
		studentDao.getStudent(0);
		System.out.println("Student: [RollNo : " + student.getRollNo()
				+ ", Name : " + student.getName() + " ]");
	}
}
STEP 5.

验证输出

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


O啦~~~

转帖请保留出处:http://blog.csdn.net/u011638883/article/details/13093701

谢谢!!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
access如何设置验证规则access如何设置验证规则Apr 10, 2024 am 10:59 AM

Access 验证规则是一种数据验证工具,用于确保数据符合特定条件,防止输入无效数据。设置验证规则的步骤:1. 选择要设置验证规则的字段;2. 打开“字段属性”对话框并切换到“查找”选项卡;3. 在“验证规则”字段中输入验证规则;4. 在“验证文本”字段中输入不符合规则时的错误消息;5. 单击“确定”保存更改。

microsoft access是什么软件microsoft access是什么软件Mar 03, 2023 am 11:37 AM

microsoft access是由微软发布的关系数据库管理系统;它结合了MicrosoftJet Database Engine和图形用户界面两项特点,是Microsoft Office的系统程序之一。

access和trunk端口的区别是什么access和trunk端口的区别是什么Oct 31, 2023 pm 05:59 PM

access和trunk端口的区别:1、Access端口用于连接终端设备,提供单个VLAN的接入,而Trunk端口用于连接交换机之间,提供多个VLAN的传输;2、Access端口只传输属于指定VLAN的数据,而Trunk端口可以传输多个VLAN的数据,并使用VLAN标签进行区分。

vb中怎么连接access数据库vb中怎么连接access数据库Oct 09, 2023 am 11:38 AM

vb中连接access数据库的步骤包括引用必要的命名空间、创建连接字符串、创建连接对象、打开连接、执行SQL语句和关闭连接。详细介绍:1、引用必要的命名空间,在VB项目中,首先需要引用“System.Data`和`Microsoft.Office.Interop.Access”命名空间,以便使用ADO.NET和Access相关的类和方法,可以在VB项目的引用中添加这些命名等等。

access数据库的扩展名是什么access数据库的扩展名是什么Apr 10, 2024 am 11:10 AM

Access 数据库文件的扩展名为 .accdb,自 Microsoft Access 2007 起开始使用,用于识别包含结构化数据的容器文件,如表、查询和窗体。

access是什么软件access是什么软件Apr 10, 2024 am 10:55 AM

Microsoft Access 是一款关系型数据库管理系统 (RDBMS),用于存储、管理和分析数据。它主要用于数据管理、导入/导出、查询/报表生成、用户界面设计和应用程序开发。Access 优势包括易用性、集成数据库管理、强大灵活、与 Office 集成和可扩展性。

access数据库有什么功能access数据库有什么功能Apr 10, 2024 pm 12:29 PM

Microsoft Access 是一款用于创建、管理和查询数据库的关系型数据库管理系统,提供以下功能:数据存储和管理数据查询和检索表单和报表创建数据分析和可视化关系数据库管理自动化和宏多用户支持数据库安全可移植性

access数据库有几种数据类型access数据库有几种数据类型Apr 10, 2024 pm 12:17 PM

Access 数据库提供了多种数据类型,用于存储不同类型的数据,包括:文本、数字、日期和时间、布尔、二进制等。其中,文本类型包括 Text、Memo 和 Hyperlink;数字类型包括 Byte、Integer、Long Integer、Single、Double 和 Currency;日期和时间类型包括 Date、Time 和 Date/Time;布尔类型为 Yes/No;二进制类型为 OLE Object 和 Attachment。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft