Java: DAO mode

Source: Internet
Author: User
Java: DAO mode
I just got my Web Media Technology job done... I have a little time to write some personal insights on Dao:

What is Dao?

Dao is a data access object data access interface, and data access: the old name of Dao is to deal with the database. Stored in the middle of the business logic and database resources. Dao functions:

1. Dao is used to encapsulate data source... for example, connection conn = daofacotry. createconnection ()..
You can put driver. url. username and passpword in Dao.
In the future, you need to change the database type. For example, if you want to change MSSQL to Oracle, you only need to change the driver. url in getconnection () in daofacory ..
2. Dao also encapsulates all database operations (such as the most basic CRUD operations) in it ..
For example, you want to insert a new user .. so. in Dao, we only need to provide an insertuser (User user) method .. the specific operation is implemented in Dao...
So when we want to call Dao, we only need to know That insertuser (User) is used to insert a new user... instead of how it is implemented.

Generally, Dao is used together with the abstract factory mode...

Factory to create a database and locate specific Dao (for example, userdao .. customerdao ..).. set getconnection 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 DaO interface: (formbean of struts... vo ..)
Public class userdaoimpl implements userdao {
Private session;
Public userdaoimpl (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 ..........
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.