1. DAO Layer Interface
PackageCom.life.dao;Importjava.util.List;ImportJava.util.Map;ImportOrg.hibernate.Query; Public InterfaceIdao<t> { /*** Find entities by ID * *@paramClazz *@paramID *@return */ PublicT Find (class<t> clazz,intID); Public voidCreate (T T); Public voidSave (T T); Public voidDelete (T T); /*** Query a page entity *@paramHQL Query statement *@paramFirstresult starting from the first few, note that the index starts from 0 *@parammaxResults Maximum number of data bars returned *@parammap Parameter Key value pairs *@return */ PublicList<t> list (String hql,intFirstresult,intMaxResults, Map<string, object>map); PublicQuery getquery (String hql, map<string, object>map); /*** Query Entity *@paramHQL *@paramMap *@return */List<T> list (String hql, map<string, object>map); /*** Get the total number of records *@paramHQL *@paramMap *@return */ intGettotalcount (String hql, map<string, object>map); }
DAO Layer Implementation
PackageCom.life.dao.impl;Importjava.util.Collection;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Set;ImportOrg.hibernate.Query;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;ImportCom.life.dao.IDao; Public classDaoimpl<t>ImplementsIdao<t> { Privatesessionfactory sessionfactory; @Override PublicT Find (class<t> clazz,intID) {//TODO auto-generated Method Stub return(T) getsession (). Get (Clazz, id); } @Override Public voidCreate (T t) {//TODO auto-generated Method Stubgetsession (). Persist (t); } @Override Public voidSave (T t) {//TODO auto-generated Method Stubgetsession (). Saveorupdate (t); } @Override Public voidDelete (T t) {//TODO auto-generated Method Stubgetsession (). Delete (t); } @Override PublicList<t> list (String hql, map<string, object>map) { //TODO auto-generated Method StubQuery query =getquery (hql, map); List<T> list =query.list (); returnlist; } @Override Public intGettotalcount (String hql, map<string, object>map) { //TODO auto-generated Method StubQuery query =getquery (hql, map); System.out.println ("HQL" +hql); Object obj=Query.uniqueresult (); return((Long) obj). Intvalue (); } @Override PublicList<t> list (String hql,intFirstresult,intMaxResults, Map<string, object>map) { //TODO auto-generated Method StubQuery query =getquery (hql, map); List<T> list =Query.setfirstresult (Firstresult). Setmaxresults (maxResults). List (); System.out.println ("Start:" +Firstresult); System.out.println ("Maximum value:" +maxResults); System.out.println ("Actual value:" +list.size ()); returnlist; } PublicSession getsession () {returnsessionfactory.getcurrentsession (); } Publicsessionfactory getsessionfactory () {returnsessionfactory; } Public voidsetsessionfactory (sessionfactory sessionfactory) { This. Sessionfactory =sessionfactory; } @Override PublicQuery getquery (String hql, map<string, object>map) {Query Query=getsession (). CreateQuery (HQL); //TODO auto-generated Method Stub if(Map! =NULL) {Set<String> KeySet =Map.keyset (); for(String string:keyset) {Object obj=Map.get (string); //here, consider what type of parameters are passed in, different methods used by different types if(objinstanceofCollection<?>) {query.setparameterlist (String, (Collection<?>) (obj); }Else if(objinstanceofobject[]) {Query.setparameterlist (String, (object[]) obj); }Else{query.setparameter (string, obj); } } } returnquery; }}
Service Layer Interface
PackageCom.life.service;Importjava.util.List;ImportJava.util.Map;/*** This parent interface cannot be used as a bean in applicationcontext.xml, because Serviceimpl is a virtual function * *@authorJL Service Interface, basic method*/ Public InterfaceIservice<t> { /*** Find entities based on ID and object class*/ PublicT Find (class<t> clazz,intID); /*** Create Entities*/ Public voidCreate (T T); /*** Save entity*/ Public voidSave (T T); /*** Delete Entity*/ Public voidDelete (T T); /*** Paging Entity*/ PublicList<t> list (String hql,intFirstresult,intMaxResults, Map<string, object>map); intGettotalcount (String hql, map<string, object>map); List<T> list (String hql, map<string, object>map); List<T> listpage (String hql,intPageintMaxResults, Map<string, object>map);}
Service Layer Implementation
PackageCom.life.service.impl;Importjava.util.List;ImportJava.util.Map;ImportCom.life.dao.IDao;ImportCom.life.service.IService;/*** Because the Create () method for each entity class has different business logic (for example, to check if an account already exists before creating a user), the class is defined as abstract * The Create () method is defined as abstract and implemented separately by the specific service * @authorJL*/ Public Abstract classServiceimpl<t>ImplementsIservice<t>{ /*** Injected through the spring IOC*/ protectedIdao<t>DAO; PublicIdao<t>Getdao () {returnDAO; } Public voidSetdao (idao<t>DAO) { This. DAO =DAO; } @Override PublicT Find (class<t> clazz,intID) {//TODO auto-generated Method Stub returnDao.find (clazz, id); } @Override Public voidCreate (T t) {dao.create (t); }; @Override Public voidSave (T t) {//TODO auto-generated Method StubDao.save (t); } @Override Public voidDelete (T t) {//TODO auto-generated Method StubDao.delete (t); } @Override PublicList<t> list (String hql, map<string, object>map) { //TODO auto-generated Method Stub returndao.list (hql, map); } @Override Public intGettotalcount (String hql, map<string, object>map) { //TODO auto-generated Method Stub returnDao.gettotalcount (hql, map); } @Override PublicList<t> list (String hql,intFirstresult,intMaxResults, Map<string, object>map) { //TODO auto-generated Method Stub returndao.list (HQL, Firstresult, maxResults, map); } @Override PublicList<t> listpage (String hql,intPageintMaxResults, Map<string, object>map) { //TODO auto-generated Method Stub returnDao.list (HQL, maxresults* (page-1), maxResults, map); }}
Done