javaee之分页练习

学习了数据库连接池后,我们发现除了把数据显示出来后,如果数据过大,一次性全部显示,页面会显得很臃肿,所以我们引进了分页技术,来实现分页效果。运用c3p0和dbutil的技术能够把数据用sql的limit语句实现分页

首先,核心的是创建一个分页类,PageBean.class

package example.entity;

import java.util.List;

public class Pagebean {

	private int firstPage =1; //首页
	private int prePage; //上一页
	private int nextPage; //下一页
	private int totalPage; //末页/总页数
	private int curPage; //当前页
	private List datas; //需要显示的数据
	private int curSize =3; //每页显示的数据
	private int totalSize; // 总的记录数
	
	public int getFirstPage() {
		return firstPage;
	}
	public void setFirstPage(int firstPage) {
		this.firstPage = firstPage;
	}
	public int getPrePage() {
		return this.getCurPage()==this.getFirstPage()?this.getFirstPage():this.getCurPage()-1;
	}
	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}
	public int getNextPage() {
		return this.getCurPage()==this.getTotalPage()?this.getTotalPage():this.getCurPage()+1;
	}
	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}
	public int getTotalPage() {
		return this.getTotalSize()%this.getCurSize()==0?this.getTotalSize()/this.getCurSize():this.getTotalSize()/this.getCurSize()+1;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getCurPage() {
		return curPage;
	}
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}
	public List getDatas() {
		return datas;
	}
	public void setDatas(List datas) {
		this.datas = datas;
	}
	public int getCurSize() {
		return curSize;
	}
	public void setCurSize(int curSize) {
		this.curSize = curSize;
	}
	public int getTotalSize() {
		return totalSize;
	}
	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}
	
}

然后,定义一个数据访问层dao

package example.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import example.entity.Student;
import example.util.c3p0util;

public class studentdao {

	//查询数据库的总记录数
	public int querycount() throws Exception{
		QueryRunner qr = new QueryRunner(c3p0util.getData());
		
		 Long l = qr.query("select count(*) from student", new ScalarHandler());
		 return l.intValue();
	}
	
	//计算每页显示的数据数
	public List query(int curpage,int cursize) throws Exception{
		QueryRunner qr = new QueryRunner(c3p0util.getData());
		return qr.query("select * from student limit ?,?", new BeanListHandler(Student.class), (curpage-1)*cursize,cursize);
	}
}

定义一个Servlet来接受参数

package mjf.page.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mjf.page.dao.StudentDao;
import mjf.page.entity.PageBean;

public class QueryServlet extends HttpServlet {

	
	StudentDao dao = new StudentDao();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		PageBean pb = new PageBean();
		
		
		//计算总记录数
				pb.setTotalsize(dao.queryCount());
				
		
		
		//计算当前页
				String curpage = request.getParameter("curpage");
				if(curpage==null || curpage.equals("")){
					curpage="1";
				}
				pb.setCurpage(Integer.parseInt(curpage));
		
		
		
		
		//计算每页显示的数据
		pb.setDatas(dao.queryfindAll(pb.getCurpage(), pb.getCursize()));
		
		request.setAttribute("pb", pb);
		request.getRequestDispatcher("/index.jsp").forward(request,response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}


在页面上进行数据和分页的显示
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
	
	
  
  
  
    
    	
编号 姓名 性别 部门 薪水
${pb.id } ${pb.name } ${pb.gender } ${pb.dept } ${pb.salary }
首页 上一页 下一页 尾页
跳转到
跳转到 当前为${pb.curpage }页 /共有${pb.lastpage }页 每页有${pb.cursize }数据 总记录数${pb.totalsize } 

这样的简单小示例能够跟大家分享一下分页的效果,希望能够帮助到大家的理解。实现分页的最主要的就是要定义一个分页类,和实现上一页,下一页,总记录数和每页显示数的实现。

你可能感兴趣的:(javaee)