MySql实现分页

(1)创建分页模型pageBean,里面有三个值,当前页page,每页记录数pageSize,起始页start=(page-1)*pageSize
(2)在dao层实现从数据库查询和获取记录数的方法:sql.append(" limit "+pagebean.getStart()+","+pagebean.getPageSize());
(3)在servlet层实现分页代码,这里传入的值有当前页page,总记录数total,以及从properties中取得的pageSize值
(4)在jsp显示页面使用el表达式获取pageCode值,这个值是生成的分页的html代码,String类型。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String page=request.getParameter("page");
		if(StringUtil.isEmpty(page)){
			page="1";
		}
		
		Connection con=null;
		PageBean pb=new PageBean(Integer.parseInt(page),Integer.parseInt(PropertiesUtil.getValue("pageSize")));    //取出保存在properties中的每页大小
		try {
			con=DBUtil.geCon();
			int total=DiaryDao.diaryCount(con);    //计算总记录数
			String pageCode=this.genPagation(total, Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue("pageSize")));
			request.setAttribute("pageCode", pageCode);
			request.getRequestDispatcher("mainTemp.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 生成分页代码
	 * @param totalNum
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	private String genPagation(int totalNum,int currentPage,int pageSize){
		int totalPage=totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;    //计算总页数
		StringBuffer pageCode=new StringBuffer();
		pageCode.append("
首页"); //传值到当前页面 if(currentPage==1){ pageCode.append(" 上一页"); }else{ pageCode.append(" 上一页"); } for(int i=currentPage-2;itotalPage){ continue; } if(i==currentPage){ pageCode.append(" "+i+""); }else{ pageCode.append(" "+i+""); } } if(currentPage==totalPage){ pageCode.append(" 下一页"); }else{ pageCode.append(" 下一页"); } pageCode.append(" 尾页"); return pageCode.toString(); } public static List diaryList(Connection con,PageBean pb) throws Exception{ List diaryList=new ArrayList(); StringBuffer sb=new StringBuffer("select * from t_diary t1,t_diarytype t2 where t1.typeId=t2.diaryTypeId"); sb.append(" order by t1.releaseDate desc"); if(pb!=null){ sb.append(" limit "+pb.getStart()+","+pb.getPageSize()); } PreparedStatement pstmt=con.prepareStatement(sb.toString()); ResultSet rs=pstmt.executeQuery(); while(rs.next()){ Diary diary=new Diary(); diary.setDiaryId(rs.getInt("diaryId")); diary.setTitle(rs.getString("title")); diary.setContent(rs.getString("content")); diary.setReleaseDate(DateUtil.formatString(rs.getString("releaseDate"), "yyyy-MM-dd HH:mm:ss")); diaryList.add(diary); } return diaryList; } /** * 获取记录总数 * @param con * @return * @throws Exception */ public static int diaryCount(Connection con) throws Exception{ StringBuffer sb=new StringBuffer("select count(*) as total from t_diary t1,t_diarytype t2 where t1.typeId=t2.diaryTypeId"); PreparedStatement pstmt=con.prepareStatement(sb.toString()); ResultSet rs=pstmt.executeQuery(); if(rs.next()){ return rs.getInt("total"); }else{ return 0; } } package com.diary.model; public class PageBean { private int page;//第几页 private int pageSize;//每页记录数 private int start;//起始页 public PageBean(int page, int pageSize) { super(); this.page = page; this.pageSize = pageSize; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getStart() { return (page-1)*pageSize; } public void setStart(int start) { this.start = start; } }

你可能感兴趣的:(JSP)