java中如何实现分页跳页查询
分析:
select t.*,rownum n from emp t
currPage当前页,totalSize总记录数, totalPage总页数, PageSize每页显⽰数,
totalPage =totalSize/PageSize
currPage=1 totalSize=9 totalPage=3 PageSize=3 –第⼀页…
currPage*PageSize  (currPage-1)*PageSize
select * from (select t.*,rownum n from emp t where rownum<=9) t where t.n>6 –第三页
select * from (select t.*,rownum n from emp t where rownum<=6) t where t.n>3 –第⼆页
select * from (select t.*,rownum n from emp t where rownum<=3) t where t.n>0 –第⼀页
步骤
1.提供⼀个PageUtils类,其中包含四个属性
currPage当前页,totalSize总记录数, totalPage总页数, PageSize每页显⽰数,
实现四个属性的set和get⽅法,
注意totalPage的get⽅法应写成getTotalPage(alSize)
{totalPage =totalSize/PageSize 注:且需分情况进⾏考虑(即能否除尽)},
2.提供⼀个可以获取totalSize 总记录数的⽅法 例如 getTotalSize{}
3.提供⼀个可以分页查询结果的⽅法
例如:public List
findByPage(pageUtils page) {
注意sql语句应写成:
String sql = “select * from (select t.*,rownum n from t_addressBook t where rownum<=?) t where t.n>?”;
第⼀个问号是CurrPage() * PageSize()
第⼆个问号是CurrPage() - 1) * PageSize()
4.实例化PageUtils类得到⼀个pageUtils对象 ,把pageUtils 对象 的当前页设置为从前端页⾯获取 传来的currPage当前页(默认为1,即第⼀页),
把pageUtils 对象 的PageSize 设置为 每页要显⽰的数量,
把pageUtils 对象 的totalSize 设置为 调⽤getTotalSize⽅法得到的totalSize,
把pageUtils 对象 的totalPage 设置为SetTotalPage(传得到的totalSize)得到totalPage,
5.调⽤findByPage(pageUtils)分页查询⽅法,将得到的集合对象List
以及
PageUtils对象存进requese.setAttribute中,跳转到前端页⾯进⾏显⽰
前端页⾯的写法:
1:简单分页写法:
<tr>
<td colspan="5">
共${alSize}条记录
每页显⽰${pageutils.pageSize}条
总页数${alPage}
当前页${pageutils.currPage}
<a href="AddressServlet?list=list">⾸页</a>
<c:if test="${pageutils.currPage>1}">
<a href="AddressServlet?list=list&oper=${pageutils.currPage-1}">上⼀页</a>                                  </c:if> <c:if test="${pageutils.currPage&alPage}">
<a href="AddressServlet?list=list&oper=${pageutils.currPage+1}">下⼀页</a>                                  </c:if>
</td>
</tr>
2:分页且实现跳页写法:
function go(num){
var ElementById("page");
if(num==0){
pageInput.value=1;
}
if(num==-1){
pageInput.value=parseInt(pageInput.value)-1;
}
if(num==1){
pageInput.value=parseInt(pageInput.value)+1;
}
if(num==2){
pageInput.ElementById("totalPage").value;
}
if(num==3){
var ElementById("num").value;
var ElementById("totalPage").value;
number=parseInt(number);
max=parseInt(max);
if(number>max){
number=max;
}
pageInput.value=number;
}
}
<tr>
分页查询插件<td colspan="5">
<a href="javaScript:void(0)" onclick="go(0)">⾸页</a>
<c:if test="${pageUtils.currPage>1}">
<a href="javaScript:void(0)" onclick="go(-1)">上⼀页</a>
</c:if>
<c:if test="${pageUtils.currPage&alPage}">
<a href="javaScript:void(0)" onclick="go(1)">下⼀页</a>
</c:if>
<a href="javaScript:void(0)" onclick="go(2)">尾页</a>
跳页: <input type="text"  id="num" />
<input type="button" value="Go" onclick="go(3)"/>    </td>
</tr>
</table>