国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Hibernate DAO基類設(shè)計(jì)
在小型項(xiàng)目中,其實(shí)最基本的操作都是增刪改查,如果能把這里做好,基本上百分之六十的問題得到解決。那么問題是怎么去簡(jiǎn)化它。以下是 項(xiàng)目的基礎(chǔ)架構(gòu),只需要繼承和注入即可。

Java代碼
  1. package com.common.base.dao;   
  2.   
  3. import java.io.Serializable;   
  4. import java.util.Collection;   
  5.   
  6. import org.hibernate.Session;   
  7. import org.hibernate.criterion.DetachedCriteria;   
  8.   
  9. public interface BaseDAOable {   
  10.     //再基本的 -增- -刪- -改- -查-   
  11.     //只有查是多變的難以控制   
  12.     public void create(Object po)throws Exception;   
  13.     public void remove(Object po)throws Exception;   
  14.     public void update(Object po)throws Exception;   
  15.        
  16.     public void removeList(java.util.List list)throws Exception;   
  17.     public void saveOrUpdateList(java.util.List list)throws Exception;   
  18.        
  19.     public void load(Object po,Serializable pk)throws Exception;   
  20.        
  21.     /*  
  22.      * 以下為查詢相關(guān)語句  
  23.      *   
  24.      */  
  25.     public Collection findByCriteriaByPage(DetachedCriteria dc,int startindex,int pagesize)throws Exception;   
  26.     public Collection findByCriteria(DetachedCriteria dc)throws Exception ;   
  27.        
  28.     public Integer countByCriteria(DetachedCriteria dc) throws Exception ;   
  29.     /**  
  30.      * 調(diào)用hibernate的get方法得到對(duì)象  
  31.      */  
  32.     public Object get(Class c,Serializable pk) throws Exception;   
  33.     /**  
  34.      * 得到 Hibernate的Session  
  35.      * @return  
  36.      */  
  37.     public Session getLocalSession() throws Exception;   
  38. }  

下面是實(shí)現(xiàn)的方法
Java代碼
  1. package com.common.base.dao;   
  2.   
  3. import java.io.Serializable;   
  4. import java.util.Collection;   
  5. import java.util.List;   
  6.   
  7. import org.hibernate.Session;   
  8. import org.hibernate.criterion.DetachedCriteria;   
  9. import org.hibernate.criterion.Projections;   
  10. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;   
  11.   
  12. public class BaseDAOimpl extends     implements BaseDAOable {   
  13.     /**  
  14.      * 得Hibernate 得session (non-Javadoc)  
  15.      *   
  16.      * @see com.common.base.dao.BaseDAOable#getLocalSession()  
  17.      */  
  18.     public Session getLocalSession() {   
  19.         return super.getSession();   
  20.     }   
  21.   
  22.     /**  
  23.      * 增加  
  24.      */  
  25.     public void create(Object po) throws Exception {   
  26.         this.getHibernateTemplate().save(po);   
  27.     }   
  28.   
  29.     /**  
  30.      * 刪除  
  31.      */  
  32.     public void remove(Object po) throws Exception {   
  33.         this.getHibernateTemplate().delete(po);   
  34.     }   
  35.   
  36.     /**  
  37.      * 修改  
  38.      */  
  39.     public void update(Object po) throws Exception {   
  40.         this.getHibernateTemplate().update(po);   
  41.     }   
  42.   
  43.     /**  
  44.      * 刪除多個(gè)對(duì)像  
  45.      */  
  46.     public void removeList(java.util.List list) throws Exception {   
  47.         this.getHibernateTemplate().deleteAll(list);   
  48.     }   
  49.   
  50.     /**  
  51.      * 保存或更新多個(gè)對(duì)像  
  52.      */  
  53.     public void saveOrUpdateList(java.util.List list) throws Exception {   
  54.         this.getHibernateTemplate().saveOrUpdateAll(list);   
  55.     }   
  56.   
  57.     /**  
  58.      * 通過記錄的主鍵載入持久數(shù)據(jù)到對(duì)象  
  59.      */  
  60.     public void load(Object po, Serializable pk) throws Exception {   
  61.            
  62.         this.getHibernateTemplate().load(po, pk);   
  63.     }   
  64.   
  65.     /**  
  66.      * 查詢,  
  67.      */  
  68.     // 對(duì)所有記錄分頁(yè)查詢   
  69.     public Collection findByCriteriaByPage(DetachedCriteria dc, int startindex,   
  70.             int pagesize) throws Exception {   
  71.         Collection result = this.getHibernateTemplate().findByCriteria(dc,   
  72.                 startindex, pagesize);   
  73.         return result;   
  74.     }   
  75.   
  76.     // 查詢所有記錄用DetachedCriteria   
  77.     public Collection findByCriteria(DetachedCriteria dc) throws Exception {   
  78.         Collection result = this.getHibernateTemplate().findByCriteria(dc);   
  79.         return result;   
  80.     }   
  81.   
  82.     /**  
  83.      * 統(tǒng)計(jì),  
  84.      */  
  85.     // 統(tǒng)計(jì)記錄數(shù)量   
  86.     public Integer countByCriteria(DetachedCriteria dc) throws Exception {   
  87.         Integer count = 0;   
  88.   
  89.         dc.setProjection(Projections.rowCount());   
  90.         List list = this.getHibernateTemplate().findByCriteria(dc);   
  91.         if (list != null && list.size() > 0) {   
  92.             count = (Integer) list.get(0);   
  93.         }   
  94.         return count;   
  95.     }   
  96.   
  97.     public Object get(Class c, Serializable pk) throws Exception {   
  98.   
  99.         return this.getHibernateTemplate().get(c, pk);   
  100.     }   
  101. }  

下面是service層了,實(shí)現(xiàn)一個(gè)接口,我這里就直接給出IMPL了,相信這里的接口大家會(huì)寫。
Java代碼
  1. package com.common.base.service;   
  2.   
  3. import java.io.Serializable;   
  4. import java.util.Collection;   
  5. import java.util.List;   
  6.   
  7. import org.hibernate.Session;   
  8. import org.hibernate.criterion.DetachedCriteria;   
  9. import org.springframework.transaction.TransactionDefinition;   
  10. import org.springframework.transaction.TransactionStatus;   
  11. import org.springframework.transaction.support.DefaultTransactionDefinition;   
  12.   
  13. import com.common.base.dao.BaseDAOable;   
  14. import com.common.exception.unchecked.ServiceException;   
  15. import com.common.util.LogManage;   
  16.   
  17. public class BaseServiceimpl implements BaseServiceable {   
  18.     private BaseDAOable basedao;   
  19.   
  20.     private ManualTransactionManager transaction;   
  21.   
  22.     /**  
  23.      * 獲得事物狀態(tài)  
  24.      */  
  25.     public TransactionStatus getTransactionStatus() {   
  26.         try {   
  27.             TransactionDefinition definition = new DefaultTransactionDefinition();   
  28.             TransactionStatus status = transaction.getTransactionManager()   
  29.                     .getTransaction(definition);   
  30.             return status;   
  31.         } catch (Exception e) {   
  32.             LogManage.errorLog("TransactionStatus getTransactionStatus() " + e);   
  33.             throw new ServiceException(e);   
  34.         }   
  35.     }   
  36.   
  37.     /**  
  38.      * 回滾事物  
  39.      */  
  40.     public void rollbackTransaction(TransactionStatus status) {   
  41.         try {   
  42.             transaction.getTransactionManager().rollback(status);   
  43.         } catch (Exception e) {   
  44.             LogManage.errorLog("rollbackTransaction(TransactionStatus status) "  
  45.                     + e);   
  46.             LogManage.warnLog("事物回滾失敗");   
  47.             throw new ServiceException(e);   
  48.         }   
  49.     }   
  50.   
  51.     /**  
  52.      * 提交事物  
  53.      */  
  54.     public void commitTransaction(TransactionStatus status) {   
  55.         try {   
  56.             transaction.getTransactionManager().commit(status);   
  57.         } catch (Exception e) {   
  58.             LogManage.errorLog("rollbackTransaction(TransactionStatus status) "  
  59.                     + e);   
  60.             LogManage.warnLog("提交事物失敗");   
  61.             this.rollbackTransaction(status);   
  62.             throw new ServiceException(e);   
  63.         }   
  64.     }   
  65.   
  66.     /**  
  67.      * 增加  
  68.      */  
  69.     public void create(Object po) throws ServiceException {   
  70.         try {   
  71.             basedao.create(po);   
  72.         } catch (Exception e) {   
  73.             LogManage.errorLog("BaseServiceimpl.create(Object po) " + e);   
  74.             throw new ServiceException(e);   
  75.         }   
  76.     }   
  77.   
  78.     /**  
  79.      * 刪除  
  80.      */  
  81.     public void delete(Object po) throws ServiceException {   
  82.         try {   
  83.             basedao.remove(po);   
  84.         } catch (Exception e) {   
  85.             LogManage.errorLog("BaseServiceimpl.delete(Object po) " + e);   
  86.             throw new ServiceException(e);   
  87.         }   
  88.     }   
  89.   
  90.     /**  
  91.      * 修改  
  92.      */  
  93.     public void update(Object po) throws ServiceException {   
  94.         try {   
  95.             basedao.update(po);   
  96.         } catch (Exception e) {   
  97.             LogManage.errorLog("BaseServiceimpl.update(Object po) " + e);   
  98.             throw new ServiceException(e);   
  99.         }   
  100.     }   
  101.   
  102.     public Object get(Object po, Serializable pk) throws ServiceException {   
  103.         try {   
  104.             return null;   
  105.         } catch (Exception e) {   
  106.             LogManage   
  107.                     .errorLog("BaseServiceimpl.loadByPK(Object po,Serializable pk) "  
  108.                             + e);   
  109.             throw new ServiceException(e);   
  110.         }   
  111.     }   
  112.   
  113.     /**  
  114.      * 在修改表數(shù)據(jù)時(shí)需先獲得該記錄對(duì)象  
  115.      */  
  116.     public void loadByPK(Object po, Serializable pk) throws ServiceException {   
  117.         try {   
  118.             basedao.load(po, pk);   
  119.             if (po == null) {   
  120.                 throw new ServiceException("Load Object \"" + po.getClass()   
  121.                         + "\" is NULL");   
  122.             }   
  123.         } catch (Exception e) {   
  124.             LogManage   
  125.                     .errorLog("BaseServiceimpl.loadByPK(Object po,Serializable pk) "  
  126.                             + e);   
  127.             throw new ServiceException(e);   
  128.         }   
  129.     }   
  130.   
  131.     /**  
  132.      * 刪除一個(gè)集合中的內(nèi)容  
  133.      */  
  134.     public void deleteList(List list) throws ServiceException {   
  135.         try {   
  136.             basedao.removeList(list);   
  137.         } catch (Exception e) {   
  138.             LogManage.errorLog("BaseServiceimpl.deleteList(List list) " + e);   
  139.             throw new ServiceException(e);   
  140.         }   
  141.     }   
  142.   
  143.     /**  
  144.      * 保存或修改一個(gè)集合中的內(nèi)容  
  145.      */  
  146.     public void saveOrUpdateList(List list) throws ServiceException {   
  147.         try {   
  148.             basedao.saveOrUpdateList(list);   
  149.         } catch (Exception e) {   
  150.             LogManage.errorLog("BaseServiceimpl.doSaveOrUpdateList(List list) "  
  151.                     + e);   
  152.             throw new ServiceException(e);   
  153.         }   
  154.     }   
  155.   
  156.     public Collection findByCriteriaByPage(DetachedCriteria dc, int startindex,   
  157.             int pagesize) throws ServiceException {   
  158.         Collection result = null;   
  159.         try {   
  160.             result = basedao.findByCriteriaByPage(dc, startindex, pagesize);   
  161.         } catch (Exception e) {   
  162.             LogManage   
  163.                     .errorLog("BaseServiceimpl.findByCriteriaByPage(Object po,int startindex,int pagesize) "  
  164.                             + e);   
  165.             throw new ServiceException(e);   
  166.         }   
  167.         return result;   
  168.     }   
  169.   
  170.     /**  
  171.      * 將WEB層構(gòu)建的 DetachedCriteria 傳到服務(wù)層進(jìn)行查詢 這樣在WEB層就偶合了hibernate api  
  172.      *   
  173.      * @param dc  
  174.      * @return  
  175.      * @throws ServiceException  
  176.      */  
  177.     public Collection findByCriteria(DetachedCriteria dc)   
  178.             throws ServiceException {   
  179.         Collection result = null;   
  180.         try {   
  181.             result = basedao.findByCriteria(dc);   
  182.         } catch (Exception e) {   
  183.             LogManage   
  184.                     .errorLog("BaseServiceimpl.findByCriteria(DetachedCriteria dc) "  
  185.                             + e);   
  186.             throw new ServiceException(e);   
  187.         }   
  188.         return result;   
  189.     }   
  190.   
  191.     /**  
  192.      * 將WEB層構(gòu)建的 DetachedCriteria 傳到服務(wù)層進(jìn)行統(tǒng)計(jì) 這樣在WEB層就偶合了hibernate api  
  193.      *   
  194.      * @param dc  
  195.      * @return  
  196.      * @throws ServiceException  
  197.      */  
  198.     public int countByCriteria(DetachedCriteria dc) throws ServiceException {   
  199.         int count = 0;   
  200.         try {   
  201.             count = basedao.countByCriteria(dc);   
  202.         } catch (Exception e) {   
  203.             LogManage   
  204.                     .errorLog("BaseServiceimpl.countByCriteria(DetachedCriteria dc) "  
  205.                             + e);   
  206.             throw new ServiceException(e);   
  207.         }   
  208.         return count;   
  209.     }   
  210.   
  211.     /**  
  212.      * 得到Hibernate的Session  
  213.      */  
  214.     public Session getSession() throws ServiceException{   
  215.         try {   
  216.             return basedao.getLocalSession();   
  217.         } catch (Exception e) {   
  218.             LogManage   
  219.                     .errorLog("BaseServiceimpl.getLocalSession() "  
  220.                             + e);   
  221.             throw new ServiceException(e);   
  222.         }   
  223.   
  224.     }   
  225.      /**  
  226.       * 查詢一個(gè)對(duì)象  
  227.       */  
  228.     public Object get(Class c, Serializable pk) throws ServiceException {   
  229.         try {   
  230.             return basedao.get(c, pk);   
  231.         } catch (Exception e) {   
  232.             LogManage   
  233.                     .errorLog("BaseServiceimpl.get() "  
  234.                             + e);   
  235.             throw new ServiceException(e);   
  236.         }   
  237.     }   
  238.   
  239.     public BaseDAOable getBasedao() {   
  240.         return basedao;   
  241.     }   
  242.   
  243.     public void setBasedao(BaseDAOable basedao) {   
  244.         this.basedao = basedao;   
  245.     }   
  246.   
  247.     public ManualTransactionManager getTransaction() {   
  248.         return transaction;   
  249.     }   
  250.   
  251.     public void setTransaction(ManualTransactionManager transaction) {   
  252.         this.transaction = transaction;   
  253.     }   
  254.   
  255. }  

希望以上內(nèi)容對(duì)初學(xué)者有用。(
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
詳解Hibernate Session & Transaction
java – Hibernate將對(duì)象保存到多個(gè)會(huì)話
spring整合JPA總結(jié)
應(yīng)用Hibernate3的DetachedCriteria實(shí)現(xiàn)分頁(yè)查詢
Hibernate回調(diào)與攔截機(jī)制
將Hibernate與Struts結(jié)合
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服