Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the functions of java DAO design pattern

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the functions of java DAO design pattern". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the functions of java DAO design pattern"?

What is DAO?

DAO is the Data Access Object data access interface, data access: the name implies to deal with the database. Sandwiched between business logic and database resources.

Functions of DAO:

1. DAO is used to encapsulate Data Source.. For example, Connection conn = DAOFacotry.createConnection ()..

You can put Driver. URL. Username, passpword, these are put in DAO

Change the type of database in the future. For example, if you want to replace MSSQL with Oracle.. You just need to change the Driver.URL in getConnection () in DAOFacory. Something like that.

2. DAO also encapsulates all database operations (such as the most basic CRUD operations)..

For example, if you want to insert a new user.. that。 In DAO, we only need to provide an insertUser (User user) this method.. The specific operation is implemented in DAO.

So for the time to call DAO. We just need to know that insertUser (User) is used to insert a new user. You don't need to know how it works.

Generally speaking, DAO is used in conjunction with Abstract Factory mode.

Factory to set up the database and locate specific DAO (such as UserDao..CustomerDao..).. GetConnection is generally set to static.. You can also put the public class HibernateSessionFactory in this AbstractFactory class.

Public class DAOFactory {

Private static final SessionFactory sessionFacotory

/ / define a TrheadLocal.

Static Session currentSession ().

Public UserDao getUserDAO () {return new UserDaoImpl (sesssion);}

Pulbic OtherDao getOtherDAO () {return new OtherDaoImpl (session);}

.

}

Public interface UserDao {

Public insertUser (FormBean)

Public updateUser (FormBean)

}

Then implement the interface of DAO: (from FormBean...VO of Struts.)

Public class UserDaoImpl implements UserDao {

Private Session session

Public UserDaoImpl (Session session) {

This.session = session

}...

Public insertUser (FormBean) {

.. / /..

Session.save (UserPO)

.. / /..

Return FormBean

}

Public FormBean updateUser (FormBean) {

.. / /..

Session.update (UserPO)

.. / /..

Return FormBean

}

}

Finally, define your PO:

Public class UserPO {

String firstname, lastname, password.

}

Thank you for your reading, the above is the content of "what are the functions of the java DAO design pattern". After the study of this article, I believe you have a deeper understanding of the function of the java DAO design pattern, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report