1. First define an interface in the DaO layer:
Public Interface Basedao {
< T > Void Add (t object );
< T > Void Delete (class clazz, serializable ID );
< T > Void Update (T clazz );
< T > T get (Class < T > Clazz, serializable ID );
< T > List < T > Search (Class < T > Clazz );
< T > List < T > Search (Class < T > Clazz, t condition );
}
2. implement this interface in the Dao. impl package:
Public Class Basedaosupport implements basedao {
PrivateSession session;
Protected Session getsession (){
Return Hibernatesessionfactory. getsession ();
}
Public Void Setsession (session ){
This . Session = Session;
}
/* *
* Added
*/
Public < T > Void Add (T Object ){
Transaction TS = Null ;
Try {
TS = Getsession (). begintransaction ();
Getsession (). Save ( Object );
TS. Commit ();
} Catch (Hibernateexception e ){
If ( Null ! = TS ){
TS. rollback ();
}
E. printstacktrace ();
} Finally {
Hibernatesessionfactory. closesession ();
}
}
/* *
* Delete
*/
Public < T > Void Delete (class clazz, serializable ID ){
Transaction TS = Null ;
Try {
TS = Getsession (). begintransaction ();
Getsession (). Delete ( Get (Clazz, ID ));
TS. Commit ();
} Catch (Hibernateexception e ){
If ( Null ! = TS ){
TS. rollback ();
}
E. printstacktrace ();
} Finally {
Hibernatesessionfactory. closesession ();
}
}
/* *
* Update
*/
Public < T > Void Update (T clazz ){
Transaction TS = Null ;
Try {
TS = Getsession (). begintransaction ();
Getsession (). Update (clazz );
TS. Commit ();
} Catch (Hibernateexception e ){
If ( Null ! = TS ){
TS. rollback ();
}
E. printstacktrace ();
} Finally {
Hibernatesessionfactory. closesession ();
}
}
/* *
* Query a single
*/
Public < T > T Get (Class < T > Clazz, serializable ID ){
Return (T) getsession (). Get (Clazz, ID );
}
/* *
* Query multiple
*/
Public < T > List < T > Search (Class < T > Clazz ){
List < T > List = Getsession (). createcriteria (clazz). List ();
Return List;
}
/* *
* Query multiple 2
*/
Public < T > List < T > Search (Class < T > Clazz, t condition ){
List < T > List = Getsession (). createcriteria (clazz). Add (example. Create (condition). List ();
Return List;
}
Common addition, deletion, query, and modification methods based on Hibernate are available, and the modification method can be further expanded.