Java 中的数据访问对象 (DAO) [英] Data access object (DAO) in Java

查看:27
本文介绍了Java 中的数据访问对象 (DAO)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在浏览一个文档时遇到了一个叫做 DAO 的术语.我发现它是一个数据访问对象.有人可以解释一下这究竟是什么吗?

I was going through a document and I came across a term called DAO. I found out that it is a Data Access Object. Can someone please explain me what this actually is?

我知道它是一种用于访问来自不同类型来源的数据的接口,在我的这项小小的研究中,我偶然发现了一个称为数据源或数据源对象的概念,事情变得一团糟我脑海.

I know that it is some kind of an interface for accessing data from different types of sources, in the middle of this little research of mine I bumped into a concept called data source or data source object, and things got messed up in my mind.

我真的很想知道什么是DAO 就其使用位置而言,以编程方式.它是如何使用的?任何从非常基本的东西解释这个概念的页面的链接也很感激.

I really want to know what a DAO is programmatically in terms of where it is used. How it is used? Any links to pages that explain this concept from the very basic stuff is also appreciated.

推荐答案

数据访问对象基本上是一个对象或接口,它提供对底层数据库或任何其他持久性存储的访问.

The Data Access Object is basically an object or an interface that provides access to an underlying database or any other persistence storage.

该定义来自:http://en.wikipedia.org/wiki/Data_access_object

还要检查这里的序列图:http://www.oracle.com/technetwork/java/dataaccessobject-138824.html

Check also the sequence diagram here: http://www.oracle.com/technetwork/java/dataaccessobject-138824.html

也许一个简单的例子可以帮助你理解这个概念:

Maybe a simple example can help you understand the concept:

假设我们有一个实体来代表一名员工:

Let's say we have an entity to represent an employee:

public class Employee {

    private int id;
    private String name;


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

员工实体将被持久化到数据库中相应的Employee 表中.处理操作员工实体所需的数据库操作的简单 DAO 接口如下所示:

The employee entities will be persisted into a corresponding Employee table in a database. A simple DAO interface to handle the database operation required to manipulate an employee entity will be like:

interface EmployeeDAO {

    List<Employee> findAll();
    List<Employee> findById();
    List<Employee> findByName();
    boolean insertEmployee(Employee employee);
    boolean updateEmployee(Employee employee);
    boolean deleteEmployee(Employee employee);

}

接下来我们必须为该接口提供一个具体的实现来处理 SQL 服务器,另一个来处理平面文件等.

Next we have to provide a concrete implementation for that interface to deal with SQL server, and another to deal with flat files, etc.

这篇关于Java 中的数据访问对象 (DAO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆