分页处理

1、分析要点:

每页显示的记录数——pageSize

总共记录数——totalCount

总共页数——totalPage

totalPage = totalCount%pageSize == 0?otalCount/pageSize:otalCount/pageSize+1

当前页数——currentPage

2、将上面的要点封装:PageUtil类

package cn.yuchao.entity;

public class Page {

	private int pageSize;
	private int totalPage;
	private int totalCount;
	private int currentPage;
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotalPage() {
		return totalPage = (totalCount%pageSize==0)?(totalCount/pageSize):(totalCount/pageSize+1);
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	
}

3、mysql数据库分页语句

通过limit来实现

Limit index size:从下标index开始查询,查询size条记录

如:每页显示3条记录,显示第二页的数据  那么sql语句应该为 limit 3,3;

语句传参写法 limit (currentPage-1)*pageSize,pageSize

4、修改查询语句

public class NovelDao  extends BaseDao{

	//查询一页书籍
	public List<Novel> getAll(Page page) {
		List<Novel> novels = new ArrayList<Novel>();
		String sql = "select * from novel limit ?,?";
		ResultSet res = this.executeQuery(sql,(page.getCurrentPage()-1)*page.getPageSize(),page.getPageSize());
		try {
			if(res != null) {
				while(res.next()) {
					novels.add(new Novel(res.getInt(1),res.getString(2),res.getString(3),res.getInt(4),res.getDouble(5),res.getDate(6)));
				}
			}
			return novels;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close();
		}
		return null;
	}

5、Servlet处理

protected void list(HttpServletRequest request, HttpServletResponse response) {
		//设置page属性
		page.setPageSize(10);
		//默认为第一页
		int currentPage = 1;
		if(request.getParameter("currentPage")!=null)
			currentPage = Integer.parseInt(request.getParameter("currentPage"));
		page.setCurrentPage(currentPage);
		//获得总记录数
		int totalCount = noveldao.count();
		page.setTotalCount(totalCount);
		request.setAttribute("page", page);
		
		//查询全部书籍存入request中
		List<Novel> novellist = noveldao.getAll(page);
		List<Category> categorylist = categorydao.getAll();
		if(novellist != null)
			request.setAttribute("novellist", novellist);
		if(categorylist != null)
			request.setAttribute("categorylist", categorylist);
		try {
			request.getRequestDispatcher("list.jsp").forward(request, response);
		} catch (ServletException | IOException e) {
			e.printStackTrace();
		}
	}

6、页面处理

<body>
	<h1 align="center">欢迎:${sessionScope.user }</h1>
	<table align="center">
		<tr align="right">
			<td colspan="6" style="text-align:right;"><a href="add.jsp">添加书籍</a></td>
		</tr>
		<tr>
			<td>编号</td>
			<td>书名</td>
			<td>作者</td>
			<td>类型</td>
			<td>价格</td>
			<td>出版日期</td>
		</tr>
		<c:forEach items="${requestScope.novellist }" var="bean" varStatus="status">
			<tr <c:if test="${status.index%2 == 0 }">style="background:#999999"</c:if>>
			<td>${bean.id }</td>
			<td>${bean.name }</td>
			<td>${bean.author }</td>
			<c:forEach items="${requestScope.categorylist }" var="cate">
				<c:if test="${cate.id == bean.category }">
					<td>${cate.category }</td>
				</c:if>
			</c:forEach>
			<td><fmt:formatNumber value="${bean.price }" type="currency"/></td>
			<td><fmt:formatDate value="${bean.pubDate }" pattern="yyyy-MM-dd"/></td>
			<td><a href="novel?option=query&id=${bean.id }">修改</a></td>
			<td><a href="novel?option=delete&id=${bean.id }">删除</a></td>
		</tr>
		</c:forEach>
		<tr>
			<td colspan="8" style="text-align:right;">
			<a href="novel?pageSize=${page.currentPage-1 }"
				<c:if test="${page.currentPage == 1 }">style="color:#ccc;" onclick="JavaScript:return false;"</c:if>
			>上一页</a>
			
			<c:forEach begin="1" step="1" end="${page.totalPage }" var ="pa">
				<a href="novel?pageSize=${pa}">${pa}</a>
			</c:forEach>
			
			<a href="novel?pageSize=${page.currentPage+1 }"
				<c:if test="${page.currentPage == page.totalPage }">style="color:#ccc;" onclick="JavaScript:return false;"</c:if>
			>下一页</a>
			
			总共${page.totalPage }页
			
			<input id="fin" type="number" max="${page.totalPage }" min="1" name="pageSize" style="width:50px"/>
			<input type="button" value="跳转" onclick="find()"/>
			</td>
		</tr>
	</table>
	
	<script type="text/javascript">
		function find(){
			var b = document.getElementById("fin").value;
			location.href = "novel?pageSize="+b;
		}
	</script>
</body>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值