The DAO layer and service layer of SSH hibernate

Source: Internet
Author: User
Tags stub

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.