Java中常见的分页类别有物理分页和逻辑分页。对于一般的model项目可以使用逻辑分页,对于数据比较多的,建议使用物理分页。
- 物理分页
只从数据库中查询出当前页面的数据,不占用很多内存,但效率比较低。 - 逻辑分页
从数据库中找到所有的数据, 存储到内存当中。展示的页面数据直接从内存中间读取,效率高,但是占用很大内存。
实现代码
物理分页的实现
1.使用jdbc完成,使用滚动结果集,可以跨数据库,但是性能低。
2.使用数据库本身的分页操作。也就是限定返回条数。
MySQL:limit
sqlservlet:top
oracle:rownum
使用以上数据库代码编写,例如limit
select * from 表 limit m,n;
m:从第几行开始。
n:查询几条。
例如,每页显示6条,查询第二页的数据
select * from 表 limit (页码-1)*6,6;
-
分页分析
1,页码自定义,默认第一页
2,每页条数,自定义
3,总条数,count(*)查询
4,总页数, 总页数=总条数/每页条数的向上取整
(总条数%每页条数==0?总条数/每页条数:总条数/每页条数+1)
5,当前页面数据,
代码分析
jsp:
<a href="${pageContext.request.contextPath}/findAllByPage">查看所有客户信息(分页展示)</a><br
servlet完成分页
可以先建立一个bean来保存分页所要显示的数据,包括页面数据,页码,总页数等。可以建立一个bean来封装这些数据。
public class PageBean {
private int pageNum; // 页码
private int currentPage; // 每页条数
private int totalPage; // 总页数
private int totalCount; // 总条数
private List<Customer> cs; // 每页数据
// 分页操作
// pageNum 页码
// currentPage 每页条数
public PageBean findByPage(int pageNum, int currentPage)
throws SQLException {
PageBean pb = new PageBean();
List<Customer> cs = dao.findByPage(pageNum, currentPage);
// 查询总条数:
int totalCount = dao.findAllCount();
// 得到总页数
int totalPage = (int) Math.ceil(totalCount * 1.0 / currentPage);
pb.setTotalCount(totalCount); // 封装总条数
pb.setTotalPage(totalPage);// 封装总页数
pb.setCs(cs);// 封装当前页数据.
pb.setCurrentPage(currentPage); // 封装每页条数
pb.setPageNum(pageNum);// 封装当前页码
return pb;
}
显示页面的jsp
<body>
<c:if test="${empty pb.cs}">
无客户信息
</c:if>
<c:if test="${not empty pb.cs}">
<table border="1" align="center" width="85%">
<tr>
<td>客户编号</td>
<td>客户姓名</td>
<td>客户性别</td>
<td>客户生日</td>
<td>客户电话</td>
<td>客户邮箱</td>
<td>客户爱好</td>
<td>客户类型</td>
<td>客户备注</td>
</tr>
<c:forEach items="${pb.cs}" var="c">
<tr>
<td>${c.id }</td>
<td>${c.name}</td>
<td>${c.gender }</td>
<td>${c.birthday }</td>
<td>${c.cellphone }</td>
<td>${c.email }</td>
<td>${c.preference }</td>
<td>${c.type }</td>
<td>${c.description }</td>
</tr>
</c:forEach>
<tr>
<td colspan="9" align="center"><a
href="/findAllByPage?pageNum=1¤tPage=${pb.currentPage}">首页</a>
<c:if test="${pb.pageNum==1}">
上一页
</c:if> <c:if test="${pb.pageNum!=1}">
<a
href="/findAllByPage?pageNum=${pb.pageNum-1}¤tPage=${pb.currentPage}">上一页</a>
</c:if> <c:if test="${pb.pageNum==pb.totalPage}">
下一页
</c:if> <c:if test="${pb.pageNum!=pb.totalPage}">
<a
href="/findAllByPage?pageNum=${pb.pageNum+1 }¤tPage=${pb.currentPage}">下一页</a>
</c:if> <a
href="/findAllByPage?pageNum=${pb.totalPage }¤tPage=${pb.currentPage}">尾页</a>
<select name="currentPage"
onchange="changeCurrentPage(this.value);">
<option>--请选择每页条数--</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
</select>
</td>
</tr>
<tr>
<td colspan="9" align="center"><c:forEach begin="1"
end="${pb.totalPage}" var="n" step="1">
<c:if test="${n==pb.pageNum}">
<a
href="/findAllByPage?pageNum=${n}¤tPage=${pb.currentPage}"><font
color='red'>第${n}页</font> </a>
</c:if>
<c:if test="${n!=pb.pageNum}">
<a
href="/findAllByPage?pageNum=${n}¤tPage=${pb.currentPage}">第${n}页</a>
</c:if>
</c:forEach>
</td>
</tr>
<tr>
<td colspan="9" align="center"><my:page pb="${pb}" />
</td>
</tr>
</table>
</c:if>
</body>