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; }