六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 64|回复: 0

Hibernate 多表查询分页核心代码

[复制链接]

升级  76%

10

主题

10

主题

10

主题

童生

Rank: 1

积分
38
 楼主| 发表于 2013-1-27 05:49:10 | 显示全部楼层 |阅读模式
<div class="code_title">java 代码<div class="dp-highlighter"><div class="bar" />
       
  • /**      
  •   * 多表分页,然后影射成JAVABEAN     
  •   * @param sqlcount  HSQL计算多表查询的总数     
  •   * @param sqllist  HSQL多表查询的语句     
  •   * @return  List     
  •   * @throws InvocationTargetException      
  •   * @throws IllegalAccessException      
  •   * @throws InstantiationException      
  •   * @throws IllegalArgumentException      
  •   * @throws ClassNotFoundException      
  •   * @throws NoSuchMethodException      
  •   * @throws SecurityException      
  •   * @throws DaoException     
  •   */     
  •  public List mutiTableList(String sqlcount,String sqllist,String classname,Class partypes[]) throws DataAccessException, HibernateException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, SecurityException, NoSuchMethodException {      
  •    HttpServletRequest httpRequest = BaseAction.getHttpRequest();      
  •    System.out.println("执行次数"+(count++));      
  •    List res = new ArrayList();//将用于存放保存的结果集合      
  •    org.hibernate.ScrollableResults srs = null;      
  •    Session session = this.getSession();      
  •    int total = queryTotalCount(sqlcount);      
  •    records.setTotalNum(total);      
  • <span />//   records.setParam(condition);      
  •    String pageSize = RequestUtil.getParam(httpRequest, "pageSize", String.valueOf(PagedList.DEFAULT_PAGE_SIZE));      
  •    records.setPageSize(Integer.parseInt(pageSize));      
  •    int totalPage = records.getTotalPage();      
  •    RequestUtil.setParam(httpRequest, "pageSize", pageSize);      
  •    //  用自定义的辅助类来取参数值,如果参数为空,则返回指定的默认值      
  •    String page = RequestUtil.getParam(httpRequest, "page", "1");      
  •    if (Integer.parseInt(page) > totalPage) {      
  •     page = "" + totalPage;      
  •    }      
  •    // 设置请求页码      
  •    records.setPage(Integer.parseInt(page));      
  •      //下面创建一个匿名Query实例并调用它的scroll()方法返回以ScrollableResults形式组织的查询结果      
  •     Query q = session.createQuery(sqllist);      
  •              q.setFirstResult(records.getQueryStartNum()-1);      
  •        q.setMaxResults(records.getQueryEndNum());      
  •        srs = q.scroll();      
  •              //将查询结果放入List保存      
  •              while(srs.next()){      
  •                Class  cl=Class.forName(classname);      
  •                Constructor cs = cl.getConstructor(partypes);      
  •                Object ob[]=new Object[partypes.length];      
  •                for(int i=0;i     
  •                 if (partypes == String.class){      
  •                  ob=srs.getString(i);      
  •                 }      
  •                 else if (partypes ==int.class){      
  •                   ob=srs.getInteger(i);      
  •                 }      
  •                 else if (partypes ==long.class){       
  •                  ob=srs.getLong(i);      
  •                 }      
  •                 else if (partypes ==short.class){       
  •                  ob=srs.getShort(i);      
  •                 }      
  •                 else if (partypes ==float.class){      
  •                  ob=srs.getFloat(i);      
  •                 }      
  •                 else if (partypes ==double.class){      
  •                  ob=srs.getDouble(i);      
  •                 }      
  •                 else if (partypes ==byte.class){      
  •                  ob=srs.getDouble(i);      
  •                 }      
  •                 else if (partypes ==char.class){       
  •                  ob=srs.getDouble(i);      
  •                 }      
  •                 else if (partypes ==boolean.class){      
  •                  ob=srs.getDouble(i);      
  •                 }       
  •                 else if (partypes ==Date.class){      
  •                  ob=srs.getDate(i);      
  •                 }       
  •                }      
  •                Object ob2 =cs.newInstance(ob);      
  •                res.add(ob2);      
  •              }      
  •              records.setStart(records.getQueryStartNum());      
  •              records.setSource(res);      
  •            String url = httpRequest.getRequestURI();      
  •        String query = CharUtil.IsoToGbk(httpRequest.getQueryString());      
  •        records.setRequestURL(url + RequestUtil.getParamQueryString(query));      
  •        //设置分页参数      
  •        httpRequest.setAttribute("records", records);      
  •          return records.getList();      
  •      }  
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表