Hibernate和Spring对DAO处理的实例

引用"Spring"手册上的话说: Hibernate+Spring显然是天生的结合.

下面是我用spring处理的一个HibernateDAO实例,可以看到,代码量大大减少了.

12   package infoweb.dao;
 3   
 4   import java.util.List;
 5   import java.util.Iterator;
 6   
 7   import infoweb.pojo.Info;
 8   
 9   
 10  import net.sf.hibernate.HibernateException;
 11  import net.sf.hibernate.Query;
 12  import net.sf.hibernate.Session;
 13  
 14  import org.springframework.orm.hibernate.HibernateCallback;
 15  import org.springframework.orm.hibernate.support.HibernateDaoSupport;
 16  
 17  
 18  /**
 19   * <p>Title: </p>
 20   * <p>Description: </p>
 21   * <p>Copyright: Copyright (c) 2004</p>
 22   * <p>Company: </p>
 23   * @author 段洪杰
 24   * @version 1.0
 25   */

 26  
27  public class InfoDAOImpl extends HibernateDaoSupport implements IInfoDAO {

  28    /**
 29     * 构造函数
 30     */

31    public InfoDAOImpl ( ) {
  32      super ( );
  33    }
  34  
  35  
  36    /**
 37     * 增加记录
 38     * @param info Info
 39     */

40    public void setInfo (Info info ) throws Exception {
  41      getHibernateTemplate ( ). save (info );
  42    }
  43  
  44  
  45    /**
 46     * 通过ID取得记录
 47     * @param id String
 48     * @return Info
 49     */

50    public Info getInfoById ( String id ) throws Exception {
  51      Info info = (Info ) getHibernateTemplate ( ). load (Info. class, id );
  52      return info;
  53    }
  54  
  55  
  56    /**
 57     * 修改记录
 58     * @param Info info
 59     */

60    public void modifyInfo (Info info ) throws Exception {
  61      getHibernateTemplate ( ). update (info );
  62    }
  63  
  64  
  65    /**
 66     * 删除记录
 67     * @param Info info
 68     */

69    public void removeInfo (Info info ) throws Exception {
  70      getHibernateTemplate ( ). delete (info );
  71    }
  72  
  73  
  74   
  75    /                                                ///
  76    /以下部份不带审核功能                             ///
  77    /                                                ///
  78   
  79  
  80    /**
 81     * 取记录总数
 82     * @return int
 83     */

84    public int getInfosCount ( ) throws Exception {
  85      int count = 0;
  86      String queryString = "select count (* ) from Info";
  87      count = ( ( Integer ) getHibernateTemplate ( ). iterate (queryString ). next ( ) ).
  88              intValue ( );
  89      return count;
  90    }
  91  
  92  
  93    /**
 94     * 取所有记录集合
 95     * @return Iterator
 96     */

97    public Iterator getAllInfos ( ) throws Exception {
  98      Iterator iterator = null;
  99      String queryString = " select info from Info as info order by info. id desc";
  100     List list = getHibernateTemplate ( ). find (queryString );
  101     iterator = list. iterator ( );
  102     return iterator;
  103   }
  104 
  105 
  106   /**
 107    * 取记录集合
 108    * @return Iterator
 109    * @param int position, int length
 110    */

111   public Iterator getInfos ( int position, int length ) throws Exception {
  112     Iterator iterator = null;
  113     String queryString = " select info from Info as info order by info. id desc";
  114     Query query = getHibernateTemplate ( ). createQuery (getSession ( ), queryString );
  115     //设置游标的起始点
  116     query. setFirstResult (position );
  117     //设置游标的长度
  118     query. setMaxResults (length );
  119     //记录生成
  120     List list = query. list ( );
  121     //把查询到的结果放入迭代器
  122     iterator = list. iterator ( );
  123     return iterator;
  124   }
  125 
  126 
  127   /**
 128    * 取第一条记录
 129    * @throws Exception
 130    * @return Station
 131    */

132   public Info getFirstInfo ( ) throws Exception {
  133     Iterator iterator = null;
  134     Info info = null;
  135     String queryString = "select info from Info as info order by info. id desc";
  136     Query query = getHibernateTemplate ( ). createQuery (getSession ( ), queryString );
  137     //记录生成
  138     List list = query. list ( );
  139     //把查询到的结果放入迭代器
  140     iterator = list. iterator ( );
141     if (iterator. hasNext ( ) ) {
  142       info = (Info ) iterator. next ( );
  143     }
  144     return info;
  145   }
  146 
  147 
  148   /**
 149    * 取最后一条记录
 150    * @throws Exception
 151    * @return Station
 152    */

153   public Info getLastInfo ( ) throws Exception {
  154     Iterator iterator = null;
  155     Info info = null;
  156     String queryString = "select info from Info as info order by info. id asc";
  157     Query query = getHibernateTemplate ( ). createQuery (getSession ( ), queryString );
  158     //记录生成
  159     List list = query. list ( );
  160     //把查询到的结果放入迭代器
  161     iterator = list. iterator ( );
162     if (iterator. hasNext ( ) ) {
  163       info = (Info ) iterator. next ( );
  164     }
  165     return info;
  166 
  167   }
  168 
  169 
  170  
  171   /                                                ///
  172   / 以下部份表中要有特定字段才能                         ///
  173   /                                                ///
  174  
  175 
  176   /**
 177    * 取符合条件记录总数, [表中要有 isperson 字段]
 178    * @return int
 179    * @param int isPerson
 180    */

  181 
182   public int getInfosCountByIsperson ( int isPerson ) throws Exception {
  183     int count = 0;
  184     String queryString =
  185         "select count (* ) from Info as info where info. isperson =" + isPerson;
  186     count = ( ( Integer ) getHibernateTemplate ( ). iterate (queryString ). next ( ) ).
  187             intValue ( );
  188     return count;
  189   }
  190 
  191 
  192   /**
 193    * 取所有符合条件记录集合, 模糊查询条件.[表中要有 isperson 字段]
 194    * @return Iterator
 195    * @param int isPerson
 196    */

  197 
198   public Iterator getAllInfosByIsperson ( int isPerson ) throws Exception {
  199     Iterator iterator = null;
  200     String queryString = " select info from Info as info where info. isperson =" +
  201                          isPerson + " order by info. id desc";
  202     List list = getHibernateTemplate ( ). find (queryString );
  203     //把查询到的结果放入迭代器
  204     iterator = list. iterator ( );
  205     return iterator;
  206   }
  207 
  208 
  209   /**
 210    * 取符合条件记录集合, 模糊查询条件.[表中要有 isperson 字段]
 211    * @return Iterator
 212    * @param int isPerson,int position, int length
 213    */

  214 
  215   public Iterator getInfosByIsperson ( int isPerson, int position, int length ) throws
216       Exception {
  217     Iterator iterator = null;
  218     String queryString = " select info from Info as info where info. isperson =" +
  219                          isPerson + " order by info. id desc";
  220     //创建查询
  221     Query query = getHibernateTemplate ( ). createQuery (getSession ( ), queryString );
  222     //设置游标的起始点
  223     query. setFirstResult (position );
  224     //设置游标的长度
  225     query. setMaxResults (length );
  226     //记录生成
  227     List list = query. list ( );
  228     //把查询到的结果放入迭代器
  229     iterator = list. iterator ( );
  230     return iterator;
  231   }
  232 
  233 
  234  
  235   /                                                ///
  236   / 以下部份表中要有特定字段才能            查询部份      ///
  237   /                                                ///
  238   ///
  239   /**
 240    * 取符合条件记录总数, 模糊查询条件.[表中要有 title 字段]
 241    * @return int
 242    * @param String text
 243    */

244   public int getInfosCount ( String text ) throws Exception {
  245     int count = 0;
  246     count = ( ( Integer ) getHibernateTemplate ( ). iterate (
  247         "select count (* ) from Info as info where info. title like '%" + text +
 248         "%'
" ). next ( ) ). intValue ( );
  249     return count;
  250   }
  251 
  252 
  253   /**
 254    * 取所有符合条件记录集合, 模糊查询条件.[表中要有 title 字段]
 255    * @return Iterator
 256    * @param String text
 257    */

  258 
259   public Iterator getAllInfos ( String text ) throws Exception {
  260     Iterator iterator = null;
  261     String queryString =
  262         " select info from Info as info where info. title like '%" + text +
 263         "%'
order by info. id desc";
  264     //创建查询
  265     Query query = getHibernateTemplate ( ). createQuery (getSession ( ), queryString );
  266     //记录生成
  267     List list = query. list ( );
  268     //把查询到的结果放入迭代器
  269     iterator = list. iterator ( );
  270     return iterator;
  271   }
  272 
  273 
  274   /**
 275    * 取符合条件记录集合, 模糊查询条件.[表中要有 title 字段]
 276    * @return Iterator
 277    * @param String text,int position, int length
 278    */

  279   public Iterator getInfos ( String text, int position, int length ) throws
280       Exception {
  281     Iterator iterator = null;
  282     String queryString =
  283         " select info from Info as info where info. title like '%" + text +
 284         "%'
order by info. id desc";
  285 
  286     //创建查询
  287     Query query = getHibernateTemplate ( ). createQuery (getSession ( ), queryString );
  288     //设置游标的起始点
  289     query. setFirstResult (position );
  290     //设置游标的长度
  291     query. setMaxResults (length );
  292     //记录生成
  293     List list = query. list ( );
  294     //把查询到的结果放入迭代器
  295     iterator = list. iterator ( );
  296     return iterator;
  297   }
  298 
  299 
  300  
  301   /                                                ///
  302   / 以下部份表中要有特定字段才能                        ///
  303   /                                                ///
  304  
  305 
  306   /**
 307    * 取符合条件记录总数.[ 表中要有 registername 字段]
 308    * @return int
 309    * @param String text
 310    */

311   public int getInfosCountByRegisterName ( String registerName ) throws Exception {
  312     int count = 0;
  313     count = ( ( Integer ) getHibernateTemplate ( ). iterate (
  314         "select count (* ) from Info as info where info. registername = '" +
 315         registerName + "'
" ). next ( ) ). intValue ( );
  316     return count;
  317   }
  318 
  319 
  320   /**
 321    * 通过注册名取得一条记录,如有多条,只取第一条.[表中要有 registername字段]
 322    * @param registername String
 323    * @return Info
 324    */

325   public Info getInfoByRegisterName ( String registerName ) throws Exception {
  326     Iterator iterator = null;
  327     Info info = null;
  328     String queryString =
  329         " select info from Info as info where info. registername= '" +
 330         registerName + "'
order by info. id desc";
  331     //创建查询
  332     Query query = getHibernateTemplate ( ). createQuery (getSession ( ), queryString );
  333     //记录生成
  334     List list = query. list ( );
  335     //把查询到的结果放入迭代器
  336     iterator = list. iterator ( );
337     if (iterator. hasNext ( ) ) {
  338       info = (Info ) iterator. next ( );
  339     }
  340     return info;
  341   }
  342 
  343 
  344   /**
 345    * 通过注册名取得所有记录集合.[表中要有 registername字段]
 346    * @param registername String
 347    * @return Iterator
 348    */

  349   public Iterator getAllInfosByRegisterName ( String registerName ) throws
350       Exception {
  351     Iterator iterator = null;
  352     String queryString =
  353         " select info from Info as info where info. registername= '" +
 354         registerName + "'
order by info. id desc";
  355     //创建查询
  356     Query query = getHibernateTemplate ( ). createQuery (getSession ( ), queryString );
  357     //记录生成
  358     List list = query. list ( );
  359     //把查询到的结果放入迭代器
  360     iterator = list. iterator ( );
  361     return iterator;
  362   }
  363 
  364 
  365   /**
 366    * 通过注册名取得记录列表.[表中要有 registername字段]
 367    * @param registername String
 368    * @return Iterator
 369    */

  370   public Iterator getInfosByRegisterName ( String registerName, int position,
371                                           int length ) throws Exception {
  372     Iterator iterator = null;
  373     String queryString =
  374         " select info from Info as info where info. registername= '" +
 375         registerName + "'
order by info. id desc";
  376     //创建查询
  377     Query query = getHibernateTemplate ( ). createQuery (getSession ( ), queryString );
  378     //设置游标的起始点
  379     query. setFirstResult (position );
  380     //设置游标的长度
  381     query. setMaxResults (length );
  382     //记录生成
  383     List list = query. list ( );
  384     //把查询到的结果放入迭代器
  385     iterator = list. iterator ( );
  386     return iterator;
  387   }
  388 
  389 
  390  
  391   /                                                ///
  392   / 以下部份表中要有特定字段才能                        ///
  393   /                                                ///
  394  
  395 
  396   /**
 397    * 取记录总数.[ 表中要有 board_id 字段]
 398    * @return int
 399    * @param String boardId
 400    */

401   public int getInfosCountByBoard ( String boardId ) throws Exception {
  402     int count = 0;
  403 
  404     count = ( ( Integer ) getHibernateTemplate ( ). iterate (
  405         "select count (* ) from Info as info where info. boardId = '" + boardId +
 406         "'
" ). next ( ) ). intValue ( );
  407 
  408     return count;
  409   }
  410 
  411 
  412   /**
 413    * 通过版块名取得所有记录集合.[表中要有 board_id字段]
 414    * @param BoardId String
 415    * @return Iterator
 416    */

417   public Iterator getAllInfosByBoard ( String boardId ) throws Exception {
  418     Iterator iterator = null;
  419     String queryString = " select info from Info as info where info. boardId= '" +
 420                          boardId + "'
order by info. id desc";
  421     //创建查询
  422     Query query = getHibernateTemplate ( ). createQuery (getSession ( ), queryString );
  423     //记录生成
  424     List list = query. list ( );
  425     //把查询到的结果放入迭代器
  426     iterator = list. iterator ( );
  427     return iterator;
  428   }
  429 
  430 
  431   /**
 432    * 通过版块名取得记录列表.[表中要有 board_id字段]
 433    * @param BoardId String
 434    * @return Iterator
 435    */

  436   public Iterator getInfosByBoard ( String boardId, int position, int length ) throws
437       Exception {
  438     Iterator iterator = null;
  439     String queryString = " select info from Info as info where info. boardId= '" +
 440                          boardId + "'
order by info. id desc";
  441 
  442     //创建查询
  443     Query query = getHibernateTemplate ( ). createQuery (getSession ( ), queryString );
  444     //设置游标的起始点
  445     query. setFirstResult (position );
  446     //设置游标的长度
  447     query. setMaxResults (length );
  448     //记录生成
  449     List list = query. list ( );
  450     //把查询到的结果放入迭代器
  451     iterator = list. iterator ( );
  452 
  453     return iterator;
  454 
  455   }
  456 
  457 
  458   /**
 459    * 取符合条件记录总数.[ 表中要有 board_id 字段,title]  模糊查询title
 460    * @return int
 461    * @param String boardId ,String text
 462    */

463   public int getInfosCountByBoard ( String boardId, String text ) throws Exception {
  464     int count = 0;
  465 
  466     count = ( ( Integer ) getHibernateTemplate ( ). iterate (
  467         "select count (* ) from Info as info where info. boardId= '" + boardId +
 468         "'
and info. title like '%" + text + "%'" ). next ( ) ). intValue ( );
  469 
  470     return count;
  471 
  472   }
  473 
  474 
  475   /**
 476    * 通过版块名取得记录列表.[表中要有 board_id字段]  模糊查询title
 477    * @param String boardID,int position, int length
 478    * @return Iterator
 479    */

  480   public Iterator getInfosByBoard ( String boardId, int position, int length,
481                                   String text ) throws Exception {
  482     Iterator iterator = null;
  483     String queryString = " select info from Info as info where info. boardId= '" +
 484                          boardId + "'
and info. title like '%" + text +
 485                          "%'
order by info. id desc";
  486 
  487     //创建查询
  488     Query query = getHibernateTemplate ( ). createQuery (getSession ( ), queryString );
  489     //设置游标的起始点
  490     query. setFirstResult (position );
  491     //设置游标的长度
  492     query. setMaxResults (length );
  493     //记录生成
  494     List list = query. list ( );
  495     //把查询到的结果放入迭代器
  496     iterator = list. iterator ( );
  497     return iterator;
  498 
  499   }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值