It is very troublesome to write a corresponding Dao for each pojo object every time you create a project. Although it can be generated by IDE or some quick generation tools, it is inevitable to change it. For this reason, I wrote a general generic Dao to encapsulate some basic operations, including adding, deleting, modifying, and querying objects. Implemented based on the spring hibernate template. For Beginners, if you have any bad things, please make a lot of bricks!
Java Code
/**
* The Dao operation base class implements common data operations at the DaO layer.
*
* @ Author Huang Lei
*
* @ Param <t>
* Pojo object
* @ Param <ID>
* ID
*/
Public class basehibernatedao <t, Id extends serializable> extends hibernatedaosupport {
Private Static final logger = logger. getlogger (basehibernatedao. Class );
/**
* Save the specified object class
*
* @ Param entityobj
* Entity class
*/
Public void save (T entity ){
Try {
Gethibernatetemplate (). Save (entity );
} Catch (runtimeexception e ){
Logger. Error ("Saving entity exceptions", e );
Throw E;
}
}
/**
* Delete a specified object
*
* @ Param entityobj
* Entity class
*/
Public void Delete (T entity ){
Try {
Gethibernatetemplate (). Delete (entity );
} Catch (runtimeexception e ){
Logger. Error ("Object deletion exception", e );
Throw E;
}
}
/**
* Update or save a specified object
*
* @ Param entity class
*/
Public void saveorupdate (T entity ){
Try {
Gethibernatetemplate (). saveorupdate (entity );
} Catch (runtimeexception e ){
Logger. Error ("An error occurred while updating or saving objects", e );
Throw E;
}
}
/**
* Find the object class with the specified ID
*
* @ Param entityclass
* Entity class
* @ Param ID
* Object ID
* @ Return object
*/
@ Suppresswarnings ("unchecked ")
Public t findbyid (class <t> entityclass, Id ID ){
Try {
Return (t) gethibernatetemplate (). Get (entityclass, ID );
} Catch (runtimeexception e ){
Logger. Error ("An error occurred while searching for the specified ID entity. ID:" + id, e );
Throw E;
}
}
/**
* Find the object set of the specified attribute
*
* @ Param entityclass
* Entity
* @ Param propertyname
* Attribute name
* @ Param Value
* Condition
* @ Return object set
*/
@ Suppresswarnings ("unchecked ")
Public list <t> findbyproperty (class <t> entityclass, string propertyname,
Object value ){
Try {
String querystr = "from" + entityclass. getname ()
+ "As model where model." + propertyname + "=? ";
Return gethibernatetemplate (). Find (querystr, value );
} Catch (runtimeexception e ){
Logger. Error ("An error occurred while searching for the specified condition entity set. condition:" + propertyname, e );
Throw E;
}
}
/**
* Query the paging data set of a specified hql statement
*
* @ Param hsql
* Hql statements
* @ Param firstrow
* Start Record Number
* @ Param maxrow
* Maximum Record Number
* @ Return the paging data set
* @ Throws exception
* Throw an exception
*/
@ Suppresswarnings ("unchecked ")
Public list <t> findbypage (final string hsql, final int firstrow,
Final int maxrow ){
Try {
Return gethibernatetemplate(cmd.exe cutefind (New hibernatecallback (){
Public object doinhibernate (Session S)
Throws hibernateexception, sqlexception {
Query query = S. createquery (hsql );
Query. setfirstresult (firstrow );
Query. setmaxresults (maxrow );
List list = query. List ();
Return list;
}
});
} Catch (runtimeexception e ){
Logger. Error ("Paging query exception, hql:" + hsql, e );
Throw E;
}
}
/**
* Get this class object from spring Context
* This method may have thread concurrency issues (to be tested)
*
* @ Param context spring Context
* @ Return this class Object
*/
@ Suppresswarnings ("unchecked ")
Public static basehibernatedao getfromapplicationcontext (webapplicationcontext context ){
Return (basehibernatedao) Context. getbean ("basehibernatedao ");
}
}