Detailed explanation and code example of Dao mode in Java

What is Dao mode?

DAO As its name implies, data access object is an object that provides an abstract interface for database or other persistence mechanisms. It provides various data access operations without exposing the implementation details of the underlying persistence scheme. In actual development, all access operations to data sources should be abstracted and encapsulated in a public API. In programming language , is to establish an interface that defines all transaction methods that will be used in this application. In this application, when you need to interact with the data source, you use this interface, and write a separate class to implement this interface. Logically, this class corresponds to a specific data store. Dao mode actually includes two modes: data accessor and data object. The former solves the problem of how to access data, while the latter solves the problem of how to encapsulate data with objects.

1、 Development architecture of information system

Customer layer ------ display layer ------ business layer ------ data layer ------ database

1. Client layer: the client layer is the client, simply the browser. 2. Display layer: JSP / servlet, which is used to display to the browser. 3. Business layer: integrate atomic operations in the data layer. 4. Data layer: atomic operations on the database, such as adding and deleting;

2、 Introduction to Dao (data access object)

Dao is applied in the data layer (atomic operations on the database, such as adding, deleting, etc.), A class used to access and operate on a database.

3、 Structure of DAO design pattern

DAO design patterns are generally divided into several categories:

1. VO (value object): a class used to store a row of data of a web page, that is, a record. For example, if a web page wants to display a user's information, this class is the user's class. 2. Databaseconnection: used to open and close the database. 3. Dao interface: used to declare operations on the database. 4. Daoimpl: the Dao interface must be implemented, and the functions of the Dao interface must be truly implemented, but it does not include the opening and closing of the database. 5. Daoproxy: it also implements Dao interface, but it only needs daoimpl, but it includes opening and closing the database. 6. Daofactory: factory class, containing getinstance() to create a proxy class.

4、 Benefits of Dao

The advantage of Dao is that the interface provided to users is only the Dao interface. Therefore, if users want to add data, they only need to call the create function without database operation.

5、 Dao package naming

For Dao, package naming and class naming must be hierarchical.

1.Emp. Java

2.DatabaseConnection. java

3.IEmpDAO. java

4.EmpDAOImpl. java

5.EmpDAOProxy. java

6.DAOFactory. java

7.TestDAO. java

Through DAO design pattern, the operation of database connection can be shielded in JSP to achieve the effect that JSP is only responsible for display.

summary

The above is all about the detailed explanation of Dao mode and code examples of Java in this article. I hope it will be helpful to you. Welcome to: the abstract factory code example of Java design mode notes, the detailed explanation of thread communication producer consumer mode and waiting wake-up mechanism code of Java multithreading, the static internal class method example of Java singleton mode, etc, If you have any questions, you can leave a message at any time. Thank you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>