學習筆記

          Simple is beautiful.

          導航

          <2007年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          統計

          公告

          ...

          常用鏈接

          留言簿(1)

          隨筆分類(2)

          隨筆檔案(56)

          Weblog

          搜索

          最新評論

          評論排行榜

          [ZT]Hibernate + Spring 對DAO的處理實例

          key words : hibernate spring dao

          come from : http://lpacec.javaeye.com/blog/46220
             1.  package infoweb.dao;      
             
          2.      
             
          3import java.util.List;      
             
          4import java.util.Iterator;      
             
          5.      
             
          6import infoweb.pojo.Info;      
             
          7.      
             
          8import net.sf.hibernate.HibernateException;      
             
          9import net.sf.hibernate.Query;      
            
          10import net.sf.hibernate.Session;      
            
          11.      
            
          12import org.springframework.orm.hibernate.HibernateCallback;      
            
          13import org.springframework.orm.hibernate.support.HibernateDaoSupport;      
            
          14.      
            
          15/**     
            16.  * 

                Title: 
                    
            17.  * 

                Description: 
                    
            18.  * 

                Copyright: Copyright (c) 2004
                    
            19.  * 

                Company: 
                    
            20.  * 
          @author 段洪杰     
            21.  * 
          @version 1.0     
            22.  
          */
                
            
          23.      
            
          24public class InfoDAOImpl extends HibernateDaoSupport implements IInfoDAO {      
            
          25.   /**     
            26.    * 構造函數     
            27.    
          */
                
            
          28.   public InfoDAOImpl() {      
            
          29.     super();      
            
          30.   }
                
            
          31.      
            
          32.   /**     
            33.    * 增加記錄     
            34.    * 
          @param info Info     
            35.    
          */
                
            
          36.   public void setInfo(Info info) throws Exception {      
            
          37.     getHibernateTemplate().save(info);      
            
          38.   }
                
            
          39.      
            
          40.   /**     
            41.    * 通過ID取得記錄     
            42.    * 
          @param id String     
            43.    * 
          @return Info     
            44.    
          */
                
            
          45.   public Info getInfoById(String id) throws Exception {      
            
          46.     Info info = (Info) getHibernateTemplate().load(Info.class, id);      
            
          47.     return info;      
            
          48.   }
                
            
          49.      
            
          50.   /**     
            51.    * 修改記錄     
            52.    * 
          @param Info info     
            53.    
          */
                
            
          54.   public void modifyInfo(Info info) throws Exception {      
            
          55.     getHibernateTemplate().update(info);      
            
          56.   }
                
            
          57.      
            
          58.   /**     
            59.    * 刪除記錄     
            60.    * 
          @param Info info     
            61.    
          */
                
            
          62.   public void removeInfo(Info info) throws Exception {      
            
          63.     getHibernateTemplate().delete(info);      
            
          64.   }
                
            
          65.      
            
          66.   ////////////////////////////////////////////////////////      
            67.   /////                                                ///      
            68.   /////以下部份不帶審核功能                              ///      
            69.   /////                                                ///      
            70.   ////////////////////////////////////////////////////////      
            71.      
            
          72.   /**     
            73.    * 取記錄總數     
            74.    * 
          @return int     
            75.    
          */
                
            
          76.   public int getInfosCount() throws Exception {      
            
          77.     int count = 0;      
            
          78.     String queryString = "select count(*) from Info";      
            
          79.     count = ((Integer) getHibernateTemplate().iterate(queryString).next()).      
            
          80.             intValue();      
            
          81.     return count;      
            
          82.   }
                
            
          83.      
            
          84.   /**     
            85.    * 取所有記錄集合     
            86.    * 
          @return Iterator     
            87.    
          */
                
            
          88.   public Iterator getAllInfos() throws Exception {      
            
          89.     Iterator iterator = null;      
            
          90.     String queryString = " select info from Info as info order by info.id desc";      
            
          91.     List list = getHibernateTemplate().find(queryString);      
            
          92.     iterator = list.iterator();      
            
          93.     return iterator;      
            
          94.   }
                
            
          95.      
            
          96.   /**     
            97.    * 取記錄集合     
            98.    * 
          @return Iterator     
            99.    * 
          @param int position, int length     
           100.    
          */
                
           
          101.   public Iterator getInfos(int position, int length) throws Exception {      
           
          102.     Iterator iterator = null;      
           
          103.     String queryString = " select info from Info as info order by info.id desc";      
           
          104.     Query query = getHibernateTemplate().createQuery(getSession(), queryString);      
           
          105.     //設置游標的起始點      
           106.     query.setFirstResult(position);      
           
          107.     //設置游標的長度      
           108.     query.setMaxResults(length);      
           
          109.     //記錄生成      
           110.     List list = query.list();      
           
          111.     //把查詢到的結果放入迭代器      
           112.     iterator = list.iterator();      
           
          113.     return iterator;      
           
          114.   }
                
           
          115.      
           
          116.   /**     
           117.    * 取第一條記錄     
           118.    * 
          @throws Exception     
           119.    * 
          @return Station     
           120.    
          */
                
           
          121.   public Info getFirstInfo() throws Exception {      
           
          122.     Iterator iterator = null;      
           
          123.     Info info = null;      
           
          124.     String queryString = "select info from Info as info order by info.id desc";      
           
          125.     Query query = getHibernateTemplate().createQuery(getSession(), queryString);      
           
          126.     //記錄生成      
           127.     List list = query.list();      
           
          128.     //把查詢到的結果放入迭代器      
           129.     iterator = list.iterator();      
           
          130.     if (iterator.hasNext()) {      
           
          131.       info = (Info) iterator.next();      
           
          132.     }
                
           
          133.     return info;      
           
          134.   }
                
           
          135.      
           
          136.   /**     
           137.    * 取最后一條記錄     
           138.    * 
          @throws Exception     
           139.    * 
          @return Station     
           140.    
          */
                
           
          141.   public Info getLastInfo() throws Exception {      
           
          142.     Iterator iterator = null;      
           
          143.     Info info = null;      
           
          144.     String queryString = "select info from Info as info order by info.id asc";      
           
          145.     Query query = getHibernateTemplate().createQuery(getSession(), queryString);      
           
          146.     //記錄生成      
           147.     List list = query.list();      
           
          148.     //把查詢到的結果放入迭代器      
           149.     iterator = list.iterator();      
           
          150.     if (iterator.hasNext()) {      
           
          151.       info = (Info) iterator.next();      
           
          152.     }
                
           
          153.     return info;      
           
          154.      
           
          155.   }
                
           
          156.      
           
          157.   ////////////////////////////////////////////////////////      
           158.   /////                                                ///      
           159.   ///// 以下部份表中要有特定字段才能正確運行   個人和企業     ///      
           160.   /////                                                ///      
           161.   ////////////////////////////////////////////////////////      
           162.      
           
          163.   /**     
           164.    * 取符合條件記錄總數, [表中要有 isperson 字段]     
           165.    * 
          @return int     
           166.    * 
          @param int isPerson     
           167.    
          */
                
           
          168.      
           
          169.   public int getInfosCountByIsperson(int isPerson) throws Exception {      
           
          170.     int count = 0;      
           
          171.     String queryString =      
           
          172.         "select count(*) from Info as info where info.isperson =" + isPerson;      
           
          173.     count = ((Integer) getHibernateTemplate().iterate(queryString).next()).      
           
          174.             intValue();      
           
          175.     return count;      
           
          176.   }
                
           
          177.      
           
          178.   /**     
           179.    * 取所有符合條件記錄集合, 模糊查詢條件.[表中要有 isperson 字段]     
           180.    * 
          @return Iterator     
           181.    * 
          @param int isPerson     
           182.    
          */
                
           
          183.      
           
          184.   public Iterator getAllInfosByIsperson(int isPerson) throws Exception {      
           
          185.     Iterator iterator = null;      
           
          186.     String queryString = " select info from Info as info where info.isperson =" +      
           
          187.                          isPerson + " order by info.id desc";      
           
          188.     List list = getHibernateTemplate().find(queryString);      
           
          189.     //把查詢到的結果放入迭代器      
           190.     iterator = list.iterator();      
           
          191.     return iterator;      
           
          192.   }
                
           
          193.      
           
          194.   /**     
           195.    * 取符合條件記錄集合, 模糊查詢條件.[表中要有 isperson 字段]     
           196.    * 
          @return Iterator     
           197.    * 
          @param int isPerson,int position, int length     
           198.    
          */
                
           
          199.      
           
          200.   public Iterator getInfosByIsperson(int isPerson, int position, int length) throws      
           
          201.       Exception {      
           
          202.     Iterator iterator = null;      
           
          203.     String queryString = " select info from Info as info where info.isperson =" +      
           
          204.                          isPerson + " order by info.id desc";      
           
          205.     //創建查詢      
           206.     Query query = getHibernateTemplate().createQuery(getSession(), queryString);      
           
          207.     //設置游標的起始點      
           208.     query.setFirstResult(position);      
           
          209.     //設置游標的長度      
           210.     query.setMaxResults(length);      
           
          211.     //記錄生成      
           212.     List list = query.list();      
           
          213.     //把查詢到的結果放入迭代器      
           214.     iterator = list.iterator();      
           
          215.     return iterator;      
           
          216.   }
                
           
          217.      
           
          218.   ////////////////////////////////////////////////////////      
           219.   /////                                                ///      
           220.   ///// 以下部份表中要有特定字段才能正確運行   查詢部份      ///      
           221.   /////                                                ///      
           222.   ///////////////////////////////////////////////////////      
           223.   /**     
           224.    * 取符合條件記錄總數, 模糊查詢條件.[表中要有 title 字段]     
           225.    * 
          @return int     
           226.    * 
          @param String text     
           227.    
          */
                
           
          228.   public int getInfosCount(String text) throws Exception {      
           
          229.     int count = 0;      
           
          230.     count = ((Integer) getHibernateTemplate().iterate(      
           
          231.         "select count(*) from Info as info where info.title like '%" + text +      
           
          232.         "%'").next()).intValue();      
           
          233.     return count;      
           
          234.   }
                
           
          235.      
           
          236.   /**     
           237.    * 取所有符合條件記錄集合, 模糊查詢條件.[表中要有 title 字段]     
           238.    * 
          @return Iterator     
           239.    * 
          @param String text     
           240.    
          */
                
           
          241.      
           
          242.   public Iterator getAllInfos(String text) throws Exception {      
           
          243.     Iterator iterator = null;      
           
          244.     String queryString =      
           
          245.         " select info from Info as info where info.title like '%" + text +      
           
          246.         "%' order by info.id desc";      
           
          247.     //創建查詢      
           248.     Query query = getHibernateTemplate().createQuery(getSession(), queryString);      
           
          249.     //記錄生成      
           250.     List list = query.list();      
           
          251.     //把查詢到的結果放入迭代器      
           252.     iterator = list.iterator();      
           
          253.     return iterator;      
           
          254.   }
                
           
          255.      
           
          256.   /**     
           257.    * 取符合條件記錄集合, 模糊查詢條件.[表中要有 title 字段]     
           258.    * 
          @return Iterator     
           259.    * 
          @param String text,int position, int length     
           260.    
          */
                
           
          261.   public Iterator getInfos(String text, int position, int length) throws      
           
          262.       Exception {      
           
          263.     Iterator iterator = null;      
           
          264.     String queryString =      
           
          265.         " select info from Info as info where info.title like '%" + text +      
           
          266.         "%' order by info.id desc";      
           
          267.      
           
          268.     //創建查詢      
           269.     Query query = getHibernateTemplate().createQuery(getSession(), queryString);      
           
          270.     //設置游標的起始點      
           271.     query.setFirstResult(position);      
           
          272.     //設置游標的長度      
           273.     query.setMaxResults(length);      
           
          274.     //記錄生成      
           275.     List list = query.list();      
           
          276.     //把查詢到的結果放入迭代器      
           277.     iterator = list.iterator();      
           
          278.     return iterator;      
           
          279.   }
                
           
          280.      
           
          281.   ////////////////////////////////////////////////////////      
           282.   /////                                                ///      
           283.   ///// 以下部份表中要有特定字段才能正確運行   注冊相關      ///      
           284.   /////                                                ///      
           285.   ////////////////////////////////////////////////////////      
           286.      
           
          287.   /**     
           288.    * 取符合條件記錄總數.[ 表中要有 registername 字段]     
           289.    * 
          @return int     
           290.    * 
          @param String text     
           291.    
          */
                
           
          292.   public int getInfosCountByRegisterName(String registerName) throws Exception {      
           
          293.     int count = 0;      
           
          294.     count = ((Integer) getHibernateTemplate().iterate(      
           
          295.         "select count(*) from Info as info where info.registername = '" +      
           
          296.         registerName + "'").next()).intValue();      
           
          297.     return count;      
           
          298.   }
                
           
          299.      
           
          300.   /**     
           301.    * 通過注冊名取得一條記錄,如有多條,只取第一條.[表中要有 registername字段]     
           302.    * 
          @param registername String     
           303.    * 
          @return Info     
           304.    
          */
                
           
          305.   public Info getInfoByRegisterName(String registerName) throws Exception {      
           
          306.     Iterator iterator = null;      
           
          307.     Info info = null;      
           
          308.     String queryString =      
           
          309.         " select info from Info as info where info.registername='" +      
           
          310.         registerName + "' order by info.id desc";      
           
          311.     //創建查詢      
           312.     Query query = getHibernateTemplate().createQuery(getSession(), queryString);      
           
          313.     //記錄生成      
           314.     List list = query.list();      
           
          315.     //把查詢到的結果放入迭代器      
           316.     iterator = list.iterator();      
           
          317.     if (iterator.hasNext()) {      
           
          318.       info = (Info) iterator.next();      
           
          319.     }
                
           
          320.     return info;      
           
          321.   }
                
           
          322.      
           
          323.   /**     
           324.    * 通過注冊名取得所有記錄集合.[表中要有 registername字段]     
           325.    * 
          @param registername String     
           326.    * 
          @return Iterator     
           327.    
          */
                
           
          328.   public Iterator getAllInfosByRegisterName(String registerName) throws      
           
          329.       Exception {      
           
          330.     Iterator iterator = null;      
           
          331.     String queryString =      
           
          332.         " select info from Info as info where info.registername='" +      
           
          333.         registerName + "' order by info.id desc";      
           
          334.     //創建查詢      
           335.     Query query = getHibernateTemplate().createQuery(getSession(), queryString);      
           
          336.     //記錄生成      
           337.     List list = query.list();      
           
          338.     //把查詢到的結果放入迭代器      
           339.     iterator = list.iterator();      
           
          340.     return iterator;      
           
          341.   }
                
           
          342.      
           
          343.   /**     
           344.    * 通過注冊名取得記錄列表.[表中要有 registername字段]     
           345.    * 
          @param registername String     
           346.    * 
          @return Iterator     
           347.    
          */
                
           
          348.   public Iterator getInfosByRegisterName(String registerName, int position,      
           
          349.                                          int length) throws Exception {      
           
          350.     Iterator iterator = null;      
           
          351.     String queryString =      
           
          352.         " select info from Info as info where info.registername='" +      
           
          353.         registerName + "' order by info.id desc";      
           
          354.     //創建查詢      
           355.     Query query = getHibernateTemplate().createQuery(getSession(), queryString);      
           
          356.     //設置游標的起始點      
           357.     query.setFirstResult(position);      
           
          358.     //設置游標的長度      
           359.     query.setMaxResults(length);      
           
          360.     //記錄生成      
           361.     List list = query.list();      
           
          362.     //把查詢到的結果放入迭代器      
           363.     iterator = list.iterator();      
           
          364.     return iterator;      
           
          365.   }
                
           
          366.      
           
          367.   ////////////////////////////////////////////////////////      
           368.   /////                                                ///      
           369.   ///// 以下部份表中要有特定字段才能正確運行     樹型版塊     ///      
           370.   /////                                                ///      
           371.   ////////////////////////////////////////////////////////      
           372.      
           
          373.   /**     
           374.    * 取記錄總數.[ 表中要有 board_id 字段]     
           375.    * 
          @return int     
           376.    * 
          @param String boardId     
           377.    
          */
                
           
          378.   public int getInfosCountByBoard(String boardId) throws Exception {      
           
          379.     int count = 0;      
           
          380.      
           
          381.     count = ((Integer) getHibernateTemplate().iterate(      
           
          382.         "select count(*) from Info as info where info.boardId = '" + boardId +      
           
          383.         "'").next()).intValue();      
           
          384.      
           
          385.     return count;      
           
          386.   }
                
           
          387.      
           
          388.   /**     
           389.    * 通過版塊名取得所有記錄集合.[表中要有 board_id字段]     
           390.    * 
          @param BoardId String     
           391.    * 
          @return Iterator     
           392.    
          */
                
           
          393.   public Iterator getAllInfosByBoard(String boardId) throws Exception {      
           
          394.     Iterator iterator = null;      
           
          395.     String queryString = " select info from Info as info where info.boardId='" +      
           
          396.                          boardId + "' order by info.id desc";      
           
          397.     //創建查詢      
           398.     Query query = getHibernateTemplate().createQuery(getSession(), queryString);      
           
          399.     //記錄生成      
           400.     List list = query.list();      
           
          401.     //把查詢到的結果放入迭代器      
           402.     iterator = list.iterator();      
           
          403.     return iterator;

          posted on 2007-04-14 09:18 Ecko 閱讀(335) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 长沙县| 泊头市| 陵水| 楚雄市| 紫阳县| 九寨沟县| 塘沽区| 德安县| 静乐县| 万全县| 新宾| 怀安县| 云龙县| 屏南县| 邯郸市| 比如县| 昌邑市| 北宁市| 和静县| 桃江县| 房产| 余庆县| 蕉岭县| 玉田县| 兴仁县| 瑞金市| 田林县| 互助| 阿拉善左旗| 西贡区| 临江市| 远安县| 黎平县| 门头沟区| 谢通门县| 武山县| 类乌齐县| 固原市| 瑞丽市| 上犹县| 南城县|