Http://www.cnblogs.com/lwbqqyumidi/p/3837629.html
Http://www.blogjava.net/icewee/archive/2012/04/27/376741.html
1 PackageCom.zq.www.common;2 3 ImportJava.lang.reflect.ParameterizedType;4 Importjava.util.List;5 6 ImportJavax.annotation.Resource;7 8 ImportOrg.hibernate.Query;9 Importorg.hibernate.Session;Ten Importorg.hibernate.SessionFactory; One Importorg.springframework.transaction.annotation.Transactional; A - //using generics - @Transactional the Public classBasedao<t> { - - //spring annotations for automatic injection (auto new) - @Resource + Privatesessionfactory sessionfactory; - + A /* at * Get session, all additions and deletions from session start - */ - - Publicsessionfactory getsessionfactory () { - returnsessionfactory; - } in - to Public voidsetsessionfactory (sessionfactory sessionfactory) { + This. Sessionfactory =sessionfactory; - } the * PublicSession GS () { $ return This. Sessionfactory.getcurrentsession ();Panax Notoginseng } - the + A /* the * based on T to get the real class type, use reflection + */ - protectedClass<t>Entityclass; $ $ protectedClass Getentityclass () { - if(Entityclass = =NULL) { -Entityclass = (class<t>) ((Parameterizedtype) getclass () the. Getgenericsuperclass ()). Getactualtypearguments () [0]; - }Wuyi returnEntityclass; the } - Wu - About $ /* - * Add - */ - @Transactional A Public voidAdd (T t) { + GS (). Save (t); the } - $ /* the * Update the */ the @Transactional the Public voidUpdate (T t) { - GS (). Update (t); in } the the /* About * Delete the */ the @Transactional the Public voidDelete (Integer id) { +GS (). Delete ( This. Get (ID)); - } the Bayi the /* the * Enquiry - */ - @Transactional the PublicT get (Integer ID) { theT instance =(T) GS (). Get (Getentityclass (), id); the returninstance; the - } the the the 94 /* the * Automatically judge add or modify, save the */ the @Transactional98 Public voidSave (T t) { About GS (). Saveorupdate (t); - }101 102 103 /*104 * Search All the */106 @Transactional107 PublicList<t>Listall () {108List<t> result=GS (). Createcriteria (Getentityclass ()). List ();109 returnresult; the }111 the 113 /* the * General Enquiry the */ the @Transactional117 PublicList<t> GETLISTBYHQL (FinalString hqlstring,FinalList values) {118Query query =GS (). CreateQuery (hqlstring);119 if(Values! =NULL) { -object[] VVS = Values.toarray (Newobject[values.size ()]);121 for(inti = 0; i < vvs.length; i++) {122 Query.setparameter (i, vvs[i]);123 }124 } the returnquery.list ();126 }127 - /*execute a complex SQL query129 * the * Get a list of:list<string>131 * the * Get multi-column:list<string[]>133 */134@Transactional (readOnly =true)135 PublicList Getlistbysql (FinalString sqlString,FinalList values) {136Query query =GS (). Createsqlquery (sqlString);137 if(Values! =NULL) {138object[] VVS = Values.toarray (Newobject[values.size ()]);139 for(inti = 0; i < vvs.length; i++) { $ Query.setparameter (i, vvs[i]);141 }142 }143 returnquery.list ();144 }145 146 147 148 /*perform complex SQL update, delete, add149 * Max * Get the number of rows affected151 * the */153 @Transactional154 PublicInteger ExecuteSQL (FinalString sqlString,FinalList values) {155Query query =GS (). Createsqlquery (sqlString);156 if(Values! =NULL) {157object[] VVS = Values.toarray (Newobject[values.size ()]);158 for(inti = 0; i < vvs.length; i++) {159 Query.setparameter (i, vvs[i]); the }161 }162 returnquery.executeupdate ();163 }164 165 166}
//
list<t> result=GS (). Createcriteria (Getentityclass ()). List ();??????
Summary of generics--to be continued