Dao
PackageCom.xiaofan.reflect;Importjava.io.Serializable; Public InterfaceDao<t> { /*** Add entity (entity--domain) data to the database *@paramT*/ voidSave (T T); /*** Modify the entities in the database *@paramT*/ voidUpdate (T T); /*** Delete records according to primary key *@paramPK*/ voidDelete (Serializable PK); /*** Query A record according to the primary key *@paramPK *@return */T Find (Serializable PK); }Dao
Daobase
PackageCom.xiaofan.reflect;Importjava.io.Serializable;ImportJava.lang.reflect.ParameterizedType;Importorg.hibernate.Session; Public Abstract classDaobase<t>ImplementsDao<t> { PrivateSession session; PrivateClass Clazz; //Reflection Generics Publicdaobase () {Class CLZ= This. GetClass ();//specific DAO implementation class Com.xiaofan.reflect.BookDaoImpl//System.out.println (Clz.getname ());Parameterizedtype pt = (parameterizedtype) clz.getgenericsuperclass ();//daobase<book>//System.out.println (PT);Clazz = (Class) pt.getactualtypearguments () [0];//Book.class//System.out.println (clazz); } Public voidSave (T t) {Session.save (t); } Public voidUpdate (T t) {session.update (t); } Public voidDelete (Serializable pk) {T T=(T) session.get (clazz, PK); Session.delete (t); } PublicT Find (Serializable PK) {return(T) session.get (clazz, PK); }}Daobase
Bookdao
Package Com.xiaofan.reflect; Public Interface extends Dao<book>{ /** * Query by criteria @param where */ void querybycondition (String where);}
Bookdao
Bookdaoimpl
Package Com.xiaofan.reflect; Public class extends Implements bookdao{ publicvoid querybycondition (String where) { }}
Bookdaoimpl
Book
Package Com.xiaofan.reflect; Public class Book {}
Book
Customerdao
Package Com.xiaofan.reflect; Public Interface extends Dao<customer>{}
Customerdao
Customerdaoimpl
Package Com.xiaofan.reflect; Public class extends Implements Customerdao { }
Customerdaoimpl
Customer
Package Com.xiaofan.reflect; Public class Customer {}
Customer
Client
Package Com.xiaofan.reflect; Public class Client { publicstaticvoid main (string[] args) { new Bookdaoimpl (); Bdao.querybycondition ("condition"); New Customerdaoimpl (); Cdao.find (001); }}
Client
Reflection generics, DAO Design Model (required for encoding)