hibernate+Spring 中使用sql語句


          以下兩個方法是DAO成的方法:
           1public List queryBySQL(PageInfo page,String deptids) {
           2            final String[] params={};
           3            final  String sql= " From TDatasrc t1 Where t1.TDept.deptid In ("+deptids+")";
           4            
           5            System.out.println("sql=========="+sql);
           6            System.out.println("params=========="+params);
           7            
           8        int rowsCount = queryListCountForJDBC(sql, params).intValue();
           9        
          10        System.out.println("rowsCount=========="+rowsCount);
          11        page.setRowsCount(rowsCount);
          12
          13        final int rowNumber = page.getRowNumber();
          14        final int firstReslult = page.getFirstIndex();
          15//        return    this.getSession().createSQLQuery(hql).addEntity(TFunction.class).list();
          16        return getHibernateTemplate().executeFind(new HibernateCallback() {
          17
          18            public Object doInHibernate(Session session)
          19                    throws HibernateException, SQLException {
          20                Query query = session.createQuery(sql);
          21                if(params!=null){
          22                    for (int i = 0; i < params.length; i++{
          23                        query.setParameter(i, params[i]);
          24                    }

          25                }

          26
          27                if (rowNumber > 0{
          28                    query.setFirstResult(firstReslult);
          29                    query.setMaxResults(rowNumber);
          30                }

          31
          32                return query.list();
          33            }

          34
          35        }
          );
          36}


          1public List queryDeptidBySql(String deptid){
          2        String sql="Select * "+
          3            "From t_Dept "+
          4            "Connect By Prior Deptcode = Parentcode "+
          5            "Start With Deptid ='"+deptid+"'";
          6        System.out.println(""+sql);
          7//        List list = getHibernateTemplate().find(sql);
          8        return this.getSession().createSQLQuery(sql).addEntity(TDept.class).list();
          9    }


          該方法是Service層的方法,通過調用DAO層的方法實現對數據庫的操作
           1public List qureyDatasrcBySql(PageInfo page,String deptid) {
           2     
           3        List<String> list_deptid = new ArrayList<String>();
           4        TDept dept=new TDept();
           5        List list_dept=datasrcManageDAO.queryDeptidBySql(deptid);
           6        for(int i=0;i<list_dept.size();i++){
           7           dept=(TDept)list_dept.get(i);
           8           list_deptid.add(dept.getDeptid());
           9        
          10        }

          11        
          12       String deptids="";
          13      
          14       for(int i=0;i<list_deptid.size();i++){
          15         if(deptids.equals(""))
          16           deptids="'"+list_deptid.get(i)+"'";
          17         else 
          18         deptids=deptids+",'"+list_deptid.get(i)+"'";
          19       }

          20       System.out.println("********* deptids[0]*******************"+ deptids);
          21        return datasrcManageDAO.queryBySQL(page,deptids);
          22    }


          posted on 2008-10-06 15:53 魯勝迪 閱讀(1854) 評論(0)  編輯  收藏 所屬分類: 一點點

          <2008年10月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導航

          統計

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          文章分類

          新聞分類

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 江门市| 台中市| 玉田县| 溆浦县| 鄢陵县| 油尖旺区| 洪泽县| 无为县| 恩平市| 汉源县| 江孜县| 新疆| 台南县| 贵南县| 温泉县| 锡林郭勒盟| 云霄县| 苏尼特左旗| 郓城县| 崇礼县| 黄陵县| 衡阳县| 客服| 射洪县| 昔阳县| 贵港市| 时尚| 会昌县| 六枝特区| 繁昌县| 龙州县| 藁城市| 甘南县| 衡东县| 遵义县| 湾仔区| 鄂托克旗| 贵溪市| 眉山市| 涟源市| 获嘉县|