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存儲庫或平面文件系統等。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章