JSP
int totalPages = -1;//总页数
int totalCount = 0;//总条数
int pageNew= request.getparamter("pageNew");//当前页数
int pageContain = 20;//每页条数
totalCount= selectCount();
totalPages= getAllPageNumber(totalCount,pageContain);
if(pageNew<1)pageNew=1;
if(pageNew>totalPages) pageNew=totalPages;
if(totalPages==-1) {out.print("没有符合条件的数据!"); return; }
<div align="center">
<a href="applyinfo.jsp?pageNew=1">首页</a>
<a href="applyinfo.jsp?pageNew=<%=((pageNew-1)>1?(pageNew-1):1)%>">上一页</a>
<a href="applyinfo.jsp?pageNew=<%=((pageNew+1)<totalPages?(pageNew+1):totalPages)%>">下一页</a>
<a href="applyinfo.jsp?pageNew=<%=totalPages%>">末页</a>第<%=pageNew%>/<%=totalPages%>页
<table border="1" cellspacing="0"cellpadding="4" class="a"></div>
//通用计算页数函数 XXX.java
publicstaticfinalint getAllPageNumber(int allResultNumber,int pageContain)
{
int allPageNumber = 0;
if(pageContain==-1)
allPageNumber = 1;
elseif(allResultNumber< pageContain)
allPageNumber = 1;
elseif(allResultNumber%pageContain!=0)
allPageNumber = (int)allResultNumber/pageContain+ 1;
else
allPageNumber = (int)allResultNumber/pageContain;
return allPageNumber;
}
Ibtatis配置文件
<select id="select "parameterClass=" StaffApply" resultMap=" applyResultMap">
<![CDATA[
select two.*
from (select rownum rn, one.*
from(
SELECT *
from STAFF ]]>
<dynamic prepend="where">
<isNotNull prepend="and" property="school_id">
SCHOOL_ID = $school_id$
</isNotNull>
</dynamic>
<![CDATA[ )one
) two
where 1 = 1 and two.rn > 20 * ($pageNum$- 1) andtwo.rn < 20 * $pageNum$ + 1
]]>
</select>
//注意,<![CDATA[ ]]>
不写可能会报错
//计算总页数
<select id="countByExample" resultClass="java.lang.Integer">
<![CDATA[
select count(*) from STAFF
]]>
</select>