zoukankan      html  css  js  c++  java
  • hibernate分页实现

    1.创建分页实体类
    
    public class PageBean {
    
    private int page; // 页码
    private int rows; // 每页显示行数
    private int start; // 某一页从第几条开始
    
    
    public PageBean(int page, int rows) {
    super();
    this.page = page;
    this.rows = rows;
    }
    
    public int getPage() {
    return page;
    }
    public void setPage(int page) {
    this.page = page;
    }
    public int getRows() {
    return rows;
    }
    public void setRows(int rows) {
    this.rows = rows;
    }
    public int getStart() {
    return (page-1)*rows;
    }
    
    
    }
    
    2.daoImpl
    
    public List<Grade> gradeList(PageBean pageBean,Grade grade)throws Exception{
    List<Grade> gradeList=null;
    Session session=this.getSession();
    StringBuffer sb=new StringBuffer("from Grade g");
    if(grade!=null && StringUtil.isNotEmpty(grade.getGradeName())){
    sb.append(" and g.gradeName like '%"+grade.getGradeName()+"%'");
    }
    Query query=session.createQuery(sb.toString().replaceFirst("and", "where"));
    if(pageBean!=null){
    query.setFirstResult(pageBean.getStart());
    query.setMaxResults(pageBean.getRows());
    }
    gradeList=(List<Grade>)query.list();
    return gradeList;
    }
    
    3.Action
    
    public String execute() throws Exception {
    PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
    try{
    if(grade==null){
    grade=new Grade();
    }
    grade.setGradeName(s_gradeName);
    JSONObject result=new JSONObject();
    List<Grade> gradeList=gradeService.gradeList(pageBean, grade);
    JSONArray jsonArray=new JSONArray();
    for(int i=0;i<gradeList.size();i++){
    Grade grade=(Grade)gradeList.get(i);
    JSONObject jsonObject=new JSONObject();
    jsonObject.put("id", grade.getId());
    jsonObject.put("gradeName", grade.getGradeName());
    jsonObject.put("gradeDesc", grade.getGradeDesc());
    jsonArray.add(jsonObject);
    }
    int total=gradeService.gradeCount(grade);
    result.put("rows", jsonArray);
    result.put("total", total);
    ResponseUtil.write(ServletActionContext.getResponse(), result);
    }catch(Exception e){
    e.printStackTrace();
    }
    return null;
    }
  • 相关阅读:
    分享自Allen 《打开excel,word发送没反映OUTLOOK无法打开》
    AD域只禁用USB存储器而开放其他USB设备
    sql2000安装sp4补丁包教程_sql2000sp4
    为帮助保护您的安全,Internet Explorer已经阻止从此站点下载文件
    IExplore.exe应用程序错误解决方法
    单网卡双IP,同时上内外网
    关于
    七伤拳
    <转>在外企混的,一定要懂“外企潜台词”
    习惯
  • 原文地址:https://www.cnblogs.com/luoxiaolei/p/5146579.html
Copyright © 2011-2022 走看看