亚洲女人被黑人巨大进入-亚洲日本视频在线观看-亚洲AV秘片一区二区三3-亚洲欧美中文字幕乱码在线

<dfn id="uqq4w"><dl id="uqq4w"></dl></dfn>
  • <abbr id="uqq4w"></abbr>
    <center id="uqq4w"><cite id="uqq4w"></cite></center>
    ?
    徐州北大青鳥
    當(dāng)前位置: 主頁 > 學(xué)在青鳥 > 編程技巧 >

    分享一個Java通用的DAO及接口代碼——DAO類

    時間:2016-10-12 11:14來源:未知 作者:代碼如詩 點(diǎn)擊:
    徐州北大青鳥分享一個Java通用的DAO及接口代碼以下為DAO類代碼 ====================================================================== package com.oa.dao.support; import java.io.Serializable; import java.util.List; import

    徐州北大青鳥分享一個Java通用的DAO及接口代碼——以下為DAO類代碼

    ======================================================================

    package com.oa.dao.support;

    import java.io.Serializable;
    import java.util.List;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.hibernate.HibernateException;
    import org.hibernate.Query;
    import org.hibernate.ScrollableResults;
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    import org.springframework.orm.hibernate3.HibernateCallback;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

    import com.oa.dao.IMultiDAO;
    import com.oa.struts.PageInfo;

    public class MultiDAO extends HibernateDaoSupport implements IMultiDAO {
     private static final Log log = LogFactory.getLog(MultiDAO.class);

     /**
      * 自定義查詢方法(無參)
      *
      * @param hql
      * @return
      * @throws Exception
      */
     public List findMsg(String hql) throws Exception {
      log.debug("自定義查詢:" + hql);
      try {
       List list = getHibernateTemplate().find(hql);
       return list;
      } catch (Exception e) {
       log.error("自定義查詢:" + e.getMessage());
       throw e;
      }

     }

     /**
      * 自定義查詢方法(有參)
      *
      * @param hql
      * @param params
      * @return
      * @throws Exception
      */
     public List findMsg(String hql, Object[] params) throws Exception {
      log.debug("自定義查詢:" + hql);
      try {
       List list = getHibernateTemplate().find(hql, params);
       return list;
      } catch (Exception e) {
       log.error("自定義查詢:" + e.getMessage());
       throw e;
      }
     }

     /**
      * 自定義查詢方法
      *
      * @param cls
      * @param id
      * @return
      */
     public Object getObject(Class cls, Serializable id) throws Exception {
      log.debug("自定義查詢:" + cls.getName());
      try {
       Object obj = this.getHibernateTemplate().get(cls, id);
       return obj;
      } catch (Exception e) {
       log.error("自定義查詢:" + e.getMessage());
       throw e;
      }

     }

     /**
      * 分頁查詢方法
      *
      * @param queryString
      * @param parameters
      * @param pageInfo,必填充pageSize,pageIndex
      * @return
      */
     public PageInfo findPageByQuery(final String hql,
       final Object[] parameters, final PageInfo pageInfo) {
      List list = getHibernateTemplate().executeFind(new HibernateCallback()// 這里使用了匿名內(nèi)部類
        {
         public Object doInHibernate(Session session)// Spring進(jìn)行事務(wù)維護(hù)
           // 省去每次創(chuàng)建session和關(guān)閉session
           throws HibernateException {
          Query query = session.createQuery(hql);
          if (parameters != null) {
           for (int i = 0; i < parameters.length; i++) {
            query.setParameter(i, parameters[i]);
           }
          }
          ScrollableResults sr = query.scroll();
          sr.last();// 滾至最后一行
          int totalCount = sr.getRowNumber();// 最后記錄行數(shù),從零開始???
          int startIndex = (pageInfo.getPageIndex() - 1)
            * pageInfo.getPageSize();// 去除頁數(shù)和頁記錄數(shù)計算開始位置
          query.setMaxResults(pageInfo.getPageSize());
          query.setFirstResult(startIndex);
          int totalRec = totalCount + 1;// 獲取總記錄數(shù)
          pageInfo.setTotalRec(totalRec);// 設(shè)置總記錄數(shù)
          int totalPage = (totalRec + pageInfo.getPageSize() - 1)
            / pageInfo.getPageSize();// 計算總頁數(shù)
          pageInfo.setTotalPage(totalPage);// 設(shè)置總頁數(shù)
          pageInfo.setPrePage(pageInfo.getPageIndex() - 1);// 設(shè)置上一頁
          pageInfo.setNextPage(pageInfo.getPageIndex() + 1);// 設(shè)置下一頁
          return query.list();
         }
        });
      pageInfo.setPageList(list);
      return pageInfo;
     }

     /**
      * 自定義保存方法
      *
      * @param entity
      * @throws Exception
      */
     public void save(Object entity) throws Exception {
      log.debug("保存數(shù)據(jù):" + entity.toString());
      try {
       this.getHibernateTemplate().save(entity);
      } catch (Exception e) {
       log.error("保存錯誤:" + e.getMessage());
       throw e;
      }
     }

     /**
      * 自定義刪除方法
      *
      * @param entity
      * @throws Exception
      */
     public void delete(Object entity) throws Exception {
      log.debug("刪除操作:" + entity.toString());
      try {
       this.getHibernateTemplate().delete(entity);
      } catch (Exception e) {
       log.error("刪除錯誤:" + e.getMessage());
       throw e;
      }
     }

     /**
      * 自定義更新操作
      *
      * @param entity
      * @throws Exception
      */
     public void update(Object entity) throws Exception {
      log.debug("更新操作:" + entity.toString());
      try {
       this.getHibernateTemplate().update(entity);
      } catch (Exception e) {
       log.error("更新操作:" + e.getMessage());
       throw e;
      }
     }

     /**
      * 特殊情況多數(shù)更新刪除時
      *
      * @param hql
      * @throws Exception
      */
     public void userDefined(String hql, Object[] params) throws Exception {
      log.debug("自定義操作:" + hql);
      Session session = this.getSession();
      Transaction trans = session.beginTransaction();// 開始事務(wù)
      try {
       Query query = session.createQuery(hql);
       if (params != null) {
        for (int i = 0; i < params.length; i++) {
         query.setParameter(i, params[i]);// 填入?yún)?shù)
        }
       }
       query.executeUpdate();
       trans.commit();// 提交事務(wù)
      } catch (Exception e) {
       trans.rollback();// 回滾事務(wù)
       log.error("自定義操作" + e.getMessage());
       throw e;
      } finally {
       session.close();// 關(guān)閉Session
      }

     }

    }

     

    ======================================================================

    徐州北大青鳥分享一個Java通用的DAO及接口代碼——以下為接口代碼

    ======================================================================

    package com.oa.dao;

    import java.io.Serializable;
    import java.util.List;

    import com.oa.struts.PageInfo;


    public interface IMultiDAO {
     /**
      * 自定義查詢方法(無參)
      *
      * @param hql
      * @return
      * @throws Exception
      */
     public List findMsg(String hql) throws Exception ;
     /**
      * 自定義查詢方法(有參)
      * @param hql
      * @param params
      * @return
      * @throws Exception
      */
     public List findMsg(String hql,Object [] params) throws Exception;

     /**
      * 自定義查詢方法
      * @param cls
      * @param id
      * @return
      */
     public Object getObject(Class cls,Serializable id) throws Exception;
     /**
      * 分頁查詢方法
      * @param queryString
      * @param parameters
      * @param pageInfo,必填充pageSize,pageIndex
      * @return
      */
     public PageInfo findPageByQuery(final String hql,
       final Object[] parameters, final PageInfo pageInfo) ;
     /**
      * 自定義保存方法
      * @param entity
      * @throws Exception
      */
     public void save(Object entity) throws Exception;
     /**
      * 自定義刪除方法
      * @param entity
      * @throws Exception
      */
     public void delete(Object entity)throws Exception;
     /**
      * 自定義更新操作
      * @param entity
      * @throws Exception
      */
     public void update(Object entity) throws Exception;
     /**
      * 特殊情況多數(shù)更新刪除時
      * @param hql
      * @throws Exception
      */
     public void userDefined(String hql,Object [] params) throws Exception;
    }

    ===============================================================

    徐州北大青鳥分享

    試聽課
    (責(zé)任編輯:代碼如詩)
    ------分隔線----------------------------
    欄目列表
    推薦內(nèi)容