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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19154202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:34:20  来源:igfitidea点击:

Data access object (DAO) in Java

javadao

提问by Vasanth Nag K V

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?

我正在浏览一份文件,我遇到了一个叫做DAO. 我发现它是一个数据访问对象。有人可以解释一下这实际上是什么吗?

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 DAOis 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.

我真的很想知道 aDAO在它的使用位置方面以编程方式是什么。它是如何使用的?任何从非常基本的东西解释这个概念的页面的链接也受到赞赏。

采纳答案by Rami

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 Employeetable in a database. A simple DAO interface to handle the database operation required to manipulate an employee entity will be like:

员工实体将被持久化到Employee数据库中的相应表中。处理操作员工实体所需的数据库操作的简单 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 服务器,另一个来处理平面文件等。

回答by umanganiello

DAO (Data Access Object) is a very used design pattern in enterprise applications. It basically is the module that is used to access data from every source (DBMS, XML and so on). I suggest you to read some examples, like this one:

DAO(数据访问对象)是企业应用程序中非常常用的设计模式。它基本上是用于从每个源(DBMS、XML 等)访问数据的模块。我建议你阅读一些例子,比如这个:

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. For example, the ORM (Object Relational Mapping) frameworks like iBatis or Hibernate, are used to map the result of SQL queries to java objects.

请注意,实现原始DAO 模式有多种方法,并且有许多框架可以简化您的工作。例如,iBatis 或 Hibernate 等 ORM(对象关系映射)框架用于将 SQL 查询的结果映射到 java 对象。

Hope it helps, Bye!

希望能帮到你,再见!

回答by Ioan

I think the best example (along with explanations) you can find on the oracle website : here. Another good tuturial could be found here.

我认为你可以在 oracle 网站上找到最好的例子(连同解释):here。另一个很好的教程可以在这里找到。

回答by babu

Data Access Object Pattern or DAO pattern is used to separate low level data accessing API or operations from high level business services. Following are the participants in Data Access Object Pattern.

数据访问对象模式或 DAO 模式用于将低级数据访问 API 或操作与高级业务服务分开。以下是数据访问对象模式的参与者。

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..

示例代码在这里..

回答by Rohit Goyal

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 存储库或平面文件系统等。

回答by VdeX

What is DATA ACCESS OBJECT (DAO)-

什么是数据访问对象 (DAO)-

It is a object/interface, which is used to access datafrom database of data storage.

它是一个对象/接口,用于从数据存储的数据库中访问数据

WHY WE USE DAO:

为什么我们使用 DAO:

it abstracts the retrieval of data from a data resource such as a database. The concept is to "separate a data resource's client interface from its data access mechanism."

它抽象了从数据资源(例如数据库)中检索数据的过程。这个概念是“将数据资源的客户端接口与其数据访问机制分开”。

The problem with accessing data directly is that the source of the data can change. Consider, for example, that your application is deployed in an environment that accesses an Oracle database. Then it is subsequently deployed to an environment that uses Microsoft SQL Server. If your application uses stored procedures and database-specific code (such as generating a number sequence), how do you handle that in your application? You have two options:

直接访问数据的问题在于数据的来源可能会发生变化。例如,假设您的应用程序部署在访问 Oracle 数据库的环境中。然后将其部署到使用 Microsoft SQL Server 的环境中。如果您的应用程序使用存储过程和特定于数据库的代码(例如生成数字序列),您如何在应用程序中处理这些问题?您有两个选择:

  • Rewrite your application to use SQL Server instead of Oracle (or add conditional code to handle the differences), or
  • Create a layer inbetween your application logic and the data access
  • 重写您的应用程序以使用 SQL Server 而不是 Oracle(或添加条件代码来处理差异),或
  • 在应用程序逻辑和数据访问之间创建一个层


Its in all referred as DAO Pattern, It consist of following:


它统称为DAO 模式,它包括以下内容:

  • Data Access Object Interface- This interface defines the standard operationsto 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 datasourcewhich can be database / xml or any other storage mechanism.
  • Model Object or Value Object- This object is simple POJO containing get/set methodsto store data retrieved using DAO class.
  • 数据访问对象接口- 该接口定义了要在模型对象上执行的标准操作
  • 数据访问对象具体类- 此类实现上述接口。该类负责从数据源获取数据,数据源可以是数据库/xml 或任何其他存储机制。
  • 模型对象或值对象- 此对象是简单的 POJO,包含用于存储使用 DAO 类检索的数据的get/set 方法

Please check this example, This will clear things more clearly.

请检查此示例,这将使事情更清楚。

Example
I assume this things must have cleared your understanding of DAO up to certain extend.

示例
我假设这件事一定已经使您对 DAO 的理解达到了一定程度。

回答by Yasir Shabbir Choudhary

DAO is an act like as "Persistence Manager " in 3 tier architecture as well as DAO also design pattern as you can consult "Gang of Four" book. Your application service layer just need to call the method of DAO class without knowing hidden & internal details of DAO's method.

DAO 就像三层架构中的“持久化管理器”一样,DAO 也是设计模式,你可以参考“四人帮”一书。您的应用服务层只需要调用 DAO 类的方法,而无需知道 DAO 方法的隐藏和内部细节。

回答by Amrit Baghel

Pojo also consider as Model class in Java where we can create getter and setter for particular variable defined in private . Remember all variables are here declared with private modifier

Pojo 还考虑作为 Java 中的模型类,我们可以在其中为 private 中定义的特定变量创建 getter 和 setter。记住这里所有的变量都是用私有修饰符声明的

回答by Amrit Baghel

Dao clases are used to reuse the jdbc logic & Dao(Data Access Object) is a design pattern. dao is a simple java class which contains JDBC logic .

Dao 类用于重用 jdbc 逻辑 & Dao(数据访问对象)是一种设计模式。dao 是一个包含 JDBC 逻辑的简单 java 类。

Data Access Layer has proven good in separate business logic layer and persistent layer. The DAO design pattern completely hides the data access implementation from its clients

数据访问层在单独的业务逻辑层和持久层中被证明是好的。DAO 设计模式对其客户端完全隐藏了数据访问实现

The Java Data Access Object (Java DAO) is an important component in business applications. Business applications almost always need access to data from relational or object databases and the Java platform offers many techniques for accessingthis data. The oldest and most mature technique is to use the Java Database Connectivity (JDBC)API, which provides the capability to execute SQL queries against a databaseand then fetch the results, one column at a time.

Java 数据访问对象 (Java DAO) 是业务应用程序中的重要组件。业务应用程序几乎总是需要访问关系数据库或对象数据库中的数据,Java 平台提供了许多访问这些数据的技术。最古老和最成熟的技术是使用 Java 数据库连接 (JDBC) API,它提供了对数据库执行 SQL 查询然后获取结果的能力,一次一列。

回答by ArunValaven

Don't get confuse with too much explanations. DAO: From the name itself it means Accessing Data using Object. DAO is separated from other Business Logics.

不要被太多的解释所迷惑。DAO:从名称本身来看,它的意思是使用对象访问数据。DAO 与其他业务逻辑分离。