What exactly is DAO? Why does it have to exist?

Source: Internet
Author: User

The data access Object is the interface that accesses the database method

1.DAOUsed to encapsulate the data source: For example, Connection conn = Daofacotry.createconnection ().
You can put driver. Url. Username, Passpword, these are put inDAOIn
You want to change the type of the database later. For example, to replace MSSQL with Oracle. Just change the driver.url inside the CreateConnection () inside the daofacory.
2.DAOIt also encapsulates the operation of the database (for example, the most basic crud operations).
Like whatsayYou want you to insert a new user: So. InDAOWe only need to provide a insertuser (user user) this way. The specific operationis aInDAOImplemented in the ...
So, for a call toDAO. We just need to know Insertuser (User)is aUsed to insert a new user ... And don't need to knowis aHow it is implemented:

Belowis aThree fields of form (that is, the user will lose three registration information)
FirstName
LastName
Password
correspond to the three fields in the database, respectively:
FirstName
LastName
Password:password

When a user submits a registration form, I get a frombean, after I have completed the verification of this formbean (validate), my control goes to Action's execute (), and here I have two options:
*****************************************************************
1: When there is no use toDAOI will complete this formbean in execute () directly below the database submissionis aMy pseudo-code:
Sesseion session= ...;
Session.save (Formbean);
*****************************************************************
2: When useful toDAOWhenis aIt's not like this (I'm wrong about it, don't hit me)
A: First define aDAO
public class userdao{
private static session sessioon= ...;(define a static hibernate session and initialize)
Private String FirstName;
Private String LastName;
private String password;
Public Userdao (String firstname,string lastname,string password) {
This.firstname=firstname;
This.lastname=lastname;
This.password=password;
}
Public Userdao (Userformbean Usebean) {
This.firstname=usebean.firstname;
...
....
}
public void Deluser () {
Session.delete (this);
Session.close ();
}
public void AddUser () {
Session.saveorupdate (this);
Session.close ();
}
.....
...
}
B: Execute () in action
Userdao usedao=new Userdao (Userformbean);
Usedao.save ()
DAO is generallyUsed in conjunction with factory and abstract mode.
Factory to build a database and locate specificDAO(such assayis aUserdao. Customerdao ...).

Abstract is used to define the comparison of interface methods:
Public interface Userdao {
Public Insertuser (Formbean);
Public UpdateUser (Formbean);
}
And then we realizeDAOThe interface:
public class Userdaoimpl implements Userdao {
Public Insertuser (Formbean) {
.. //..
Session.save (USERPO);
.. //..
return Formbean;
}
Public Formbean UpdateUser (Formbean) {
.. //..
Session.update (USERPO);
.. //..
return Formbean;
}
}
Define your PO at the end:
public class Userpo {
String FirstName, LastName, Password .....
}

What exactly is DAO? Why does it have to exist?

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.