Java中的数据访问对象(DAO)

本文翻译自:Data access object (DAO) in Java

I was going through a document and I came across a term called DAO . 我正在查看一个文档,遇到了一个称为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. 我知道这是一种用于从不同类型的源访问数据的接口,在我的这项小小的研究中,我碰到了一个称为数据源或数据源对象的概念,然后我脑子里一团糟。

I really want to know what a DAO is programmatically in terms of where it is used. 我真的很想知道DAO在程序上的用途。 How it is used? 如何使用? Any links to pages that explain this concept from the very basic stuff is also appreciated. 任何从最基本的东西解释这个概念的页面的链接也将受到赞赏。


#1楼

参考:https://stackoom.com/question/1IMsk/Java中的数据访问对象-DAO


#2楼

The Data Access Object is basically an object or an interface that provides access to an underlying database or any other persistence storage. 数据访问对象基本上是提供对基础数据库或任何其他持久性存储的访问的对象或接口。

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

Check also the sequence diagram here: http://www.oracle.com/technetwork/java/dataaccessobject-138824.html 也可以在此处检查序列图: 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;
    }

}

The employee entities will be persisted into a corresponding Employee table in a database. 雇员实体将被持久保存到数据库中相应的Employee表中。 A simple DAO interface to handle the database operation required to manipulate an employee entity will be like: 一个简单的DAO接口可以处理操作雇员实体所需的数据库操作,如下所示:

interface EmployeeDAO {

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

}

Next we have to provide a concrete implementation for that interface to deal with SQL server, and another to deal with flat files, etc. 接下来,我们必须为该接口提供一个具体的实现以处理SQL Server,并提供另一个实现以处理平面文件等。


#3楼

DAO (Data Access Object) is a very used design pattern in enterprise applications. DAO(数据访问对象)是企业应用程序中非常常用的设计模式。 It basically is the module that is used to access data from every source (DBMS, XML and so on). 基本上,它是用于从每个源(DBMS,XML等)访问数据的模块。 I suggest you to read some examples, like this one: 我建议您阅读一些示例,例如:

DAO Example DAO示例

Please note that there are different ways to implements the original DAO Pattern , and there are many frameworks that can simplify your work. 请注意,有多种方法可以实现原始DAO模式 ,并且有许多框架可以简化您的工作。 For example, the ORM (Object Relational Mapping) frameworks like iBatis or Hibernate, are used to map the result of SQL queries to java objects. 例如,诸如iBatis或Hibernate之类的ORM(对象关系映射)框架用于将SQL查询的结果映射到java对象。

Hope it helps, Bye! 希望能有所帮助,再见!


#4楼

I think the best example (along with explanations) you can find on the oracle website : here . 我认为最好的例子(连同解释)可以在oracle网站上找到: here Another good tuturial could be found here . 这里可以找到另一种很好的教学方法。


#5楼

Data Access Object Pattern or DAO pattern is used to separate low level data accessing API or operations from high level business services. 数据访问对象模式或DAO模式用于将底层数据访问API或操作与高层业务服务分开。 Following are the participants in Data Access Object Pattern. 以下是数据访问对象模式的参与者。

Data Access Object Interface - This interface defines the standard operations to be performed on a model object(s). 数据访问对象接口-此接口定义要在模型对象上执行的标准操作。

Data Access Object concrete class -This class implements above interface. 数据访问对象具体类-此类实现上述接口。 This class is responsible to get data from a datasource which can be database / xml or any other storage mechanism. 此类负责从可以是数据库/ xml或任何其他存储机制的数据源获取数据。

Model Object or Value Object - This object is simple POJO containing get/set methods to store data retrieved using DAO class. 模型对象或值对象-此对象是简单的POJO,其中包含用于存储使用DAO类检索的数据的get / set方法。

Sample code here .. 示例代码在这里 ..


#6楼

The Data Access Object manages the connection with the data source to obtain and store data.It abstracts the underlying data access implementation for the Business Object to enable transparent access to the data source. 数据访问对象管理与数据源的连接以获取和存储数据,它抽象化业务对象的基础数据访问实现以实现对数据源的透明访问。 A data source could be any database such as an RDBMS, XML repository or flat file system etc. 数据源可以是任何数据库,例如RDBMS,XML存储库或平面文件系统等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值