On the understanding of DAO design pattern

In order to reduce the coupling, a design pattern of Dao encapsulating database operation is proposed.

It can separate business logic from database access. Relatively speaking, the database is relatively stable, in which Dao components depend on the database system and provide the interface for database access.

General Dao encapsulation consists of the following other principles:

・ a table corresponds to a table and encapsulates a Dao class accordingly. ・ Dao interfaces must be defined by specific types. This avoids being called incorrectly.

In the Dao mode, the persistence of data is extracted to the Dao layer, exposing the service layer for programmers to use. On the one hand, it avoids mixing JDBC call statements in business code, making the implementation of business more clear.

Usually, we can create a package to save the Dao interface, and then create a package to save the implementation class of the Dao interface.

The following shows a Dao interface instance, manage:

Next, we create an implementation class for manage for a specific table. For example, the table name is test and the database name is bean.

Other implementations are almost the same. If a bean type collection is returned, the data of each row will be read from the resultset.

Store a row of data into a java bean (create a member variable of the corresponding type for the bean), and store the set bean into the add collection.

Then read the data of the next row, repeat the operation, and finally return to the collection.

summary

The above is all about the understanding of DAO design pattern in this paper. I hope it will be helpful to you. Interested friends can refer to this site: the writing example code of basejdbc and cruddao, the code example of data access object layer (DAO) using JDBC, etc. if you have any questions, you can leave a message at any time. The editor will reply to you in time. Thank you for reading!

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
分享
二维码
< <上一篇
下一篇>>