軟件藝術(shù)思考者  
          混沌,彷徨,立志,蓄勢(shì)...
          公告
          日歷
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導(dǎo)航

          隨筆分類(lèi)(86)

          隨筆檔案(85)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

           
          ?
          引用"Spring"手冊(cè)上的話說(shuō): Hibernate+Spring顯然是天生的結(jié)合.

          下面是我用spring處理的一個(gè)HibernateDAO實(shí)例,可以看到,代碼量大大減少了.

          java代碼:?


          package infoweb.dao;

          import java.util.List;
          import java.util.Iterator;

          import infoweb.pojo.Info;


          import net.sf.hibernate.HibernateException;
          import net.sf.hibernate.Query;
          import net.sf.hibernate.Session;

          import org.springframework.orm.hibernate.HibernateCallback;
          import org.springframework.orm.hibernate.support.HibernateDaoSupport;


          /**
          * <p>Title: </p>
          * <p>Description: </p>
          * <p>Copyright: Copyright (c) 2004</p>
          * <p>Company: </p>
          * @author 段洪杰
          * @version 1.0
          */


          publicclass InfoDAOImpl extends HibernateDaoSupport implements IInfoDAO {
          ? /**
          ? ?* 構(gòu)造函數(shù)
          ? ?*/

          ? public InfoDAOImpl(){
          ??? super();
          ? }


          ? /**
          ? ?* 增加記錄
          ? ?* @param info Info
          ? ?*/

          ? publicvoid setInfo(Info info)throwsException{
          ??? getHibernateTemplate().save(info);
          ? }


          ? /**
          ? ?* 通過(guò)ID取得記錄
          ? ?* @param id String
          ? ?* @return Info
          ? ?*/

          ? public Info getInfoById(String id)throwsException{
          ??? Info info = (Info) getHibernateTemplate().load(Info.class, id);
          ??? return info;
          ? }


          ? /**
          ? ?* 修改記錄
          ? ?* @param Info info
          ? ?*/

          ? publicvoid modifyInfo(Info info)throwsException{
          ??? getHibernateTemplate().update(info);
          ? }


          ? /**
          ? ?* 刪除記錄
          ? ?* @param Info info
          ? ?*/

          ? publicvoid removeInfo(Info info)throwsException{
          ??? getHibernateTemplate().delete(info);
          ? }


          ? ////////////////////////////////////////////////////////
          ? /////??????????????????????????????????????????????? ///
          ? /////以下部份不帶審核功能????????????????????????????? ///
          ? /////??????????????????????????????????????????????? ///
          ? ////////////////////////////////////////////////////////

          ? /**
          ? ?* 取記錄總數(shù)
          ? ?* @return int
          ? ?*/

          ? publicint getInfosCount()throwsException{
          ??? int count = 0;
          ??? String queryString = "select count(*) from Info";
          ??? count = ((Integer) getHibernateTemplate().iterate(queryString).next()).
          ??????????? intValue();
          ??? return count;
          ? }


          ? /**
          ? ?* 取所有記錄集合
          ? ?* @return Iterator
          ? ?*/

          ? publicIterator getAllInfos()throwsException{
          ??? Iterator iterator = null;
          ??? String queryString = " select info from Info as info order by info.id desc";
          ??? List list = getHibernateTemplate().find(queryString);
          ??? iterator = list.iterator();
          ??? return iterator;
          ? }


          ? /**
          ? ?* 取記錄集合
          ? ?* @return Iterator
          ? ?* @param int position, int length
          ? ?*/

          ? publicIterator getInfos(int position, int length)throwsException{
          ??? Iterator iterator = null;
          ??? String queryString = " select info from Info as info order by info.id desc";
          ??? Query query = getHibernateTemplate().createQuery(getSession(), queryString);
          ??? //設(shè)置游標(biāo)的起始點(diǎn)
          ??? query.setFirstResult(position);
          ??? //設(shè)置游標(biāo)的長(zhǎng)度
          ??? query.setMaxResults(length);
          ??? //記錄生成
          ??? List list = query.list();
          ??? //把查詢到的結(jié)果放入迭代器
          ??? iterator = list.iterator();
          ??? return iterator;
          ? }


          ? /**
          ? ?* 取第一條記錄
          ? ?* @throws Exception
          ? ?* @return Station
          ? ?*/

          ? public Info getFirstInfo()throwsException{
          ??? Iterator iterator = null;
          ??? Info info = null;
          ??? String queryString = "select info from Info as info order by info.id desc";
          ??? Query query = getHibernateTemplate().createQuery(getSession(), queryString);
          ??? //記錄生成
          ??? List list = query.list();
          ??? //把查詢到的結(jié)果放入迭代器
          ??? iterator = list.iterator();
          ??? if(iterator.hasNext()){
          ????? info = (Info) iterator.next();
          ??? }
          ??? return info;
          ? }


          ? /**
          ? ?* 取最后一條記錄
          ? ?* @throws Exception
          ? ?* @return Station
          ? ?*/

          ? public Info getLastInfo()throwsException{
          ??? Iterator iterator = null;
          ??? Info info = null;
          ??? String queryString = "select info from Info as info order by info.id asc";
          ??? Query query = getHibernateTemplate().createQuery(getSession(), queryString);
          ??? //記錄生成
          ??? List list = query.list();
          ??? //把查詢到的結(jié)果放入迭代器
          ??? iterator = list.iterator();
          ??? if(iterator.hasNext()){
          ????? info = (Info) iterator.next();
          ??? }
          ??? return info;

          ? }


          ? ////////////////////////////////////////////////////////
          ? /////??????????????????????????????????????????????? ///
          ? ///// 以下部份表中要有特定字段才能?吩誦袪 牳鋈撕推笠禒 ? ?///
          ? /////??????????????????????????????????????????????? ///
          ? ////////////////////////////////////////////////////////

          ? /**
          ? ?* 取符合條件記錄總數(shù), [表中要有 isperson 字段]
          ? ?* @return int
          ? ?* @param int isPerson
          ? ?*/


          ? publicint getInfosCountByIsperson(int isPerson)throwsException{
          ??? int count = 0;
          ??? String queryString =
          ??????? "select count(*) from Info as info where info.isperson =" + isPerson;
          ??? count = ((Integer) getHibernateTemplate().iterate(queryString).next()).
          ??????????? intValue();
          ??? return count;
          ? }


          ? /**
          ? ?* 取所有符合條件記錄集合, 模糊查詢條件.[表中要有 isperson 字段]
          ? ?* @return Iterator
          ? ?* @param int isPerson
          ? ?*/


          ? publicIterator getAllInfosByIsperson(int isPerson)throwsException{
          ??? Iterator iterator = null;
          ??? String queryString = " select info from Info as info where info.isperson =" +
          ??????????????????????? ?isPerson + " order by info.id desc";
          ??? List list = getHibernateTemplate().find(queryString);
          ??? //把查詢到的結(jié)果放入迭代器
          ??? iterator = list.iterator();
          ??? return iterator;
          ? }


          ? /**
          ? ?* 取符合條件記錄集合, 模糊查詢條件.[表中要有 isperson 字段]
          ? ?* @return Iterator
          ? ?* @param int isPerson,int position, int length
          ? ?*/


          ? publicIterator getInfosByIsperson(int isPerson, int position, int length)throws
          ????? Exception{
          ??? Iterator iterator = null;
          ??? String queryString = " select info from Info as info where info.isperson =" +
          ??????????????????????? ?isPerson + " order by info.id desc";
          ??? //創(chuàng)建查詢
          ??? Query query = getHibernateTemplate().createQuery(getSession(), queryString);
          ??? //設(shè)置游標(biāo)的起始點(diǎn)
          ??? query.setFirstResult(position);
          ??? //設(shè)置游標(biāo)的長(zhǎng)度
          ??? query.setMaxResults(length);
          ??? //記錄生成
          ??? List list = query.list();
          ??? //把查詢到的結(jié)果放入迭代器
          ??? iterator = list.iterator();
          ??? return iterator;
          ? }


          ? ////////////////////////////////////////////////////////
          ? /////??????????????????????????????????????????????? ///
          ? ///// 以下部份表中要有特定字段才能?吩誦袪 ?查詢部份????? ///
          ? /////??????????????????????????????????????????????? ///
          ? ///////////////////////////////////////////////////////
          ? /**
          ? ?* 取符合條件記錄總數(shù), 模糊查詢條件.[表中要有 title 字段]
          ? ?* @return int
          ? ?* @param String text
          ? ?*/

          ? publicint getInfosCount(String text)throwsException{
          ??? int count = 0;
          ??? count = ((Integer) getHibernateTemplate().iterate(
          ??????? "select count(*) from Info as info where info.title like '%" + text +
          ??????? "%'
          ").next()).intValue();
          ??? return count;
          ? }


          ? /**
          ? ?* 取所有符合條件記錄集合, 模糊查詢條件.[表中要有 title 字段]
          ? ?* @return Iterator
          ? ?* @param String text
          ? ?*/


          ? publicIterator getAllInfos(String text)throwsException{
          ??? Iterator iterator = null;
          ??? String queryString =
          ??????? " select info from Info as info where info.title like '%" + text +
          ??????? "%'
          order by info.id desc";
          ??? //創(chuàng)建查詢
          ??? Query query = getHibernateTemplate().createQuery(getSession(), queryString);
          ??? //記錄生成
          ??? List list = query.list();
          ??? //把查詢到的結(jié)果放入迭代器
          ??? iterator = list.iterator();
          ??? return iterator;
          ? }


          ? /**
          ? ?* 取符合條件記錄集合, 模糊查詢條件.[表中要有 title 字段]
          ? ?* @return Iterator
          ? ?* @param String text,int position, int length
          ? ?*/

          ? publicIterator getInfos(String text, int position, int length)throws
          ????? Exception{
          ??? Iterator iterator = null;
          ??? String queryString =
          ??????? " select info from Info as info where info.title like '%" + text +
          ??????? "%'
          order by info.id desc";

          ??? //創(chuàng)建查詢
          ??? Query query = getHibernateTemplate().createQuery(getSession(), queryString);
          ??? //設(shè)置游標(biāo)的起始點(diǎn)
          ??? query.setFirstResult(position);
          ??? //設(shè)置游標(biāo)的長(zhǎng)度
          ??? query.setMaxResults(length);
          ??? //記錄生成
          ??? List list = query.list();
          ??? //把查詢到的結(jié)果放入迭代器
          ??? iterator = list.iterator();
          ??? return iterator;
          ? }


          ? ////////////////////////////////////////////////////////
          ? /////??????????????????????????????????????????????? ///
          ? ///// 以下部份表中要有特定字段才能?吩誦袪 犠⒉嵯喙貭 ??? ///
          ? /////??????????????????????????????????????????????? ///
          ? ////////////////////////////////////////////////////////

          ? /**
          ? ?* 取符合條件記錄總數(shù).[ 表中要有 registername 字段]
          ? ?* @return int
          ? ?* @param String text
          ? ?*/

          ? publicint getInfosCountByRegisterName(String registerName)throwsException{
          ??? int count = 0;
          ??? count = ((Integer) getHibernateTemplate().iterate(
          ??????? "select count(*) from Info as info where info.registername = '" +
          ??????? registerName + "'
          ").next()).intValue();
          ??? return count;
          ? }


          ? /**
          ? ?* 通過(guò)注冊(cè)名取得一條記錄,如有多條,只取第一條.[表中要有 registername字段]
          ? ?* @param registername String
          ? ?* @return Info
          ? ?*/

          ? public Info getInfoByRegisterName(String registerName)throwsException{
          ??? Iterator iterator = null;
          ??? Info info = null;
          ??? String queryString =
          ??????? " select info from Info as info where info.registername='" +
          ??????? registerName + "'
          order by info.id desc";
          ??? //創(chuàng)建查詢
          ??? Query query = getHibernateTemplate().createQuery(getSession(), queryString);
          ??? //記錄生成
          ??? List list = query.list();
          ??? //把查詢到的結(jié)果放入迭代器
          ??? iterator = list.iterator();
          ??? if(iterator.hasNext()){
          ????? info = (Info) iterator.next();
          ??? }
          ??? return info;
          ? }


          ? /**
          ? ?* 通過(guò)注冊(cè)名取得所有記錄集合.[表中要有 registername字段]
          ? ?* @param registername String
          ? ?* @return Iterator
          ? ?*/

          ? publicIterator getAllInfosByRegisterName(String registerName)throws
          ????? Exception{
          ??? Iterator iterator = null;
          ??? String queryString =
          ??????? " select info from Info as info where info.registername='" +
          ??????? registerName + "'
          order by info.id desc";
          ??? //創(chuàng)建查詢
          ??? Query query = getHibernateTemplate().createQuery(getSession(), queryString);
          ??? //記錄生成
          ??? List list = query.list();
          ??? //把查詢到的結(jié)果放入迭代器
          ??? iterator = list.iterator();
          ??? return iterator;
          ? }


          ? /**
          ? ?* 通過(guò)注冊(cè)名取得記錄列表.[表中要有 registername字段]
          ? ?* @param registername String
          ? ?* @return Iterator
          ? ?*/

          ? publicIterator getInfosByRegisterName(String registerName, int position,
          ??????????????????????????????????????? ?int length)throwsException{
          ??? Iterator iterator = null;
          ??? String queryString =
          ??????? " select info from Info as info where info.registername='" +
          ??????? registerName + "'
          order by info.id desc";
          ??? //創(chuàng)建查詢
          ??? Query query = getHibernateTemplate().createQuery(getSession(), queryString);
          ??? //設(shè)置游標(biāo)的起始點(diǎn)
          ??? query.setFirstResult(position);
          ??? //設(shè)置游標(biāo)的長(zhǎng)度
          ??? query.setMaxResults(length);
          ??? //記錄生成
          ??? List list = query.list();
          ??? //把查詢到的結(jié)果放入迭代器
          ??? iterator = list.iterator();
          ??? return iterator;
          ? }


          ? ////////////////////////////////////////////////////////
          ? /////??????????????????????????????????????????????? ///
          ? ///// 以下部份表中要有特定字段才能?吩誦袪 ? 犑饜桶嬋闋 ? ?///
          ? /////??????????????????????????????????????????????? ///
          ? ////////////////////////////////////////////////////////

          ? /**
          ? ?* 取記錄總數(shù).[ 表中要有 board_id 字段]
          ? ?* @return int
          ? ?* @param String boardId
          ? ?*/

          ? publicint getInfosCountByBoard(String boardId)throwsException{
          ??? int count = 0;

          ??? count = ((Integer) getHibernateTemplate().iterate(
          ??????? "select count(*) from Info as info where info.boardId = '" + boardId +
          ??????? "'
          ").next()).intValue();

          ??? return count;
          ? }


          ? /**
          ? ?* 通過(guò)版塊名取得所有記錄集合.[表中要有 board_id字段]
          ? ?* @param BoardId String
          ? ?* @return Iterator
          ? ?*/

          ? publicIterator getAllInfosByBoard(String boardId)throwsException{
          ??? Iterator iterator = null;
          ??? String queryString = " select info from Info as info where info.boardId='" +
          ??????????????????????? ?boardId + "'
          order by info.id desc";
          ??? //創(chuàng)建查詢
          ??? Query query = getHibernateTemplate().createQuery(getSession(), queryString);
          ??? //記錄生成
          ??? List list = query.list();
          ??? //把查詢到的結(jié)果放入迭代器
          ??? iterator = list.iterator();
          ??? return iterator;
          ? }


          ? /**
          ? ?* 通過(guò)版塊名取得記錄列表.[表中要有 board_id字段]
          ? ?* @param BoardId String
          ? ?* @return Iterator
          ? ?*/

          ? publicIterator getInfosByBoard(String boardId, int position, int length)throws
          ????? Exception{
          ??? Iterator iterator = null;
          ??? String queryString = " select info from Info as info where info.boardId='" +
          ??????????????????????? ?boardId + "'
          order by info.id desc";

          ??? //創(chuàng)建查詢
          ??? Query query = getHibernateTemplate().createQuery(getSession(), queryString);
          ??? //設(shè)置游標(biāo)的起始點(diǎn)
          ??? query.setFirstResult(position);
          ??? //設(shè)置游標(biāo)的長(zhǎng)度
          ??? query.setMaxResults(length);
          ??? //記錄生成
          ??? List list = query.list();
          ??? //把查詢到的結(jié)果放入迭代器
          ??? iterator = list.iterator();

          ??? return iterator;

          ? }


          ? /**
          ? ?* 取符合條件記錄總數(shù).[ 表中要有 board_id 字段,title]? 模糊查詢title
          ? ?* @return int
          ? ?* @param String boardId ,String text
          ? ?*/

          ? publicint getInfosCountByBoard(String boardId, String text)throwsException{
          ??? int count = 0;

          ??? count = ((Integer) getHibernateTemplate().iterate(
          ??????? "select count(*) from Info as info where info.boardId='" + boardId +
          ??????? "'
          and info.title like '%" + text + "%'").next()).intValue();

          ??? return count;

          ? }


          ? /**
          ? ?* 通過(guò)版塊名取得記錄列表.[表中要有 board_id字段]? 模糊查詢title
          ? ?* @param String boardID,int position, int length
          ? ?* @return Iterator
          ? ?*/

          ? publicIterator getInfosByBoard(String boardId, int position, int length,
          ????????????????????????????????? String text)throwsException{
          ??? Iterator iterator = null;
          ??? String queryString = " select info from Info as info where info.boardId='" +
          ??????????????????????? ?boardId + "'
          and info.title like '%" + text +
          ??????????????????????? ?"%'
          order by info.id desc";

          ??? //創(chuàng)建查詢
          ??? Query query = getHibernateTemplate().createQuery(getSession(), queryString);
          ??? //設(shè)置游標(biāo)的起始點(diǎn)
          ??? query.setFirstResult(position);
          ??? //設(shè)置游標(biāo)的長(zhǎng)度
          ??? query.setMaxResults(length);
          ??? //記錄生成
          ??? List list = query.list();
          ??? //把查詢到的結(jié)果放入迭代器
          ??? iterator = list.iterator();
          ??? return iterator;

          ? }


          ? ////////////////////////////////////////////////////////
          ? /////??????????????????????????????????????????????? ///
          ? /////以下部份帶有審核功能????????????????????????????? ///
          ? /////??????????????????????????????????????????????? ///
          ? ////////////////////////////////////////////////////////

          ? /**
          ? ?* 取記錄總數(shù)
          ? ?* @return int
          ? ?* @param int isAuditing
          ? ?*/

          ? publicint getInfosCount(int isAuditing)throwsException{
          ???
          posted on 2006-07-25 11:23 智者無(wú)疆 閱讀(628) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): about hibernateabout spring
           
          Copyright © 智者無(wú)疆 Powered by: 博客園 模板提供:滬江博客


             觀音菩薩贊

          主站蜘蛛池模板: 会东县| 黄骅市| 和林格尔县| 浙江省| 民权县| 论坛| 青阳县| 荃湾区| 桓台县| 江津市| 辉县市| 江孜县| 江北区| 连平县| 翁牛特旗| 莱西市| 浠水县| 赤水市| 志丹县| 桂平市| 时尚| 左贡县| 雅江县| 洪湖市| 东光县| 连平县| 兴国县| 蒲城县| 枣阳市| 达拉特旗| 绵阳市| 禹城市| 化德县| 汉寿县| 阳高县| 余姚市| 蓬溪县| 遂宁市| 雅安市| 崇文区| 交口县|