java web 分页方法(数据库用MySQL)

SQL语句:select * from table limit a,b   //从a+1 开始到b


1、写一个Page类
package com.util;

/**
 * @author lackiechan
 *
 */
public class Page {
	private static final int DEFAULT_SIZE=15;//每页默认的大小
	private int current;//当前页面
	private int total;//总页数
	private int allPage;//所有的结果
	private int pageSize;//每一页的大小
	private Page(){
		this.pageSize=DEFAULT_SIZE;
	}
	public Page(int current,int total){
		this.current=current;
		this.total=total;
		this.pageSize=DEFAULT_SIZE;
		this.allPage=(total-1)/pageSize+1;
	}
	private Page(int current,int total,int pageSize){
		this.current=current;
		this.total=total;
		this.pageSize=pageSize;
		this.allPage=(total-1)/pageSize+1;
	}
	public int getCurrent() {
		return current;
	}
	public void setCurrent(int current) {
		this.current = current;
	}

	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setAllPage(int allPage) {
		this.allPage = allPage;
	}
	public int getAllPage() {
		return allPage;
	}

	
}
2.


		int p=1;
		String indexPage=(String)request.getParameter("page");
		if(indexPage==null||"".equals(indexPage)){
			p=1;
		}
		else
			p=Integer.parseInt(indexPage);
		/***********************获取某页面的人数*****************************/
		int totalCount = readerDAO.query(null).size();
		Page page = new Page(p, totalCount);
		List<ReaderForm> ps = readerDAO.getPersonByPage(p,page);
		request.setAttribute("pageReader", ps);
		request.setAttribute("page", page);
		request.getRequestDispatcher("reader.jsp").forward(request, response);

3、



<td align="center">
       <p>
			共
			<%=currentPage.getTotal()%>
			名读者 <span>当前第<%=currentPage.getCurrent()%>页,共<span
				id="pages"><%=currentPage.getAllPage()%></span>页 </span>
				<%if(currentPage.getCurrent()>1) {%>				
				<a href="reader?action=readerQuery&page=<%=currentPage.getCurrent()-1%>">上一页</a> 
				<%} %>
				<%if(currentPage.getCurrent()<currentPage.getAllPage()){ %>
				<a href="reader?action=readerQuery&page=<%=currentPage.getCurrent()+1%>">下一页</a>
				<%} %>
		</p>
	</td>	


5、

String sql="select r.*,t.name as typename,t.number from tb_reader r left join tb_readerType t on r.typeid=t.id limit "
			+idmum+" , "+page.getPageSize();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值