ibatis 分页(转)
=======分页BEAN========
import java.util.List;
/**
* ibatis分页Bean
* @author Administrator
*
*/
public class PageUtil {
private int curPage = 0; //当前页
private int pageSize = 0; //每页多少行
private int endSize ; //用于not in(select top endSize id)不在多少行内
private int totalRow ; //共多少行
private int totalPage ; //共多少页
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
int temp = pageSize * (curPage-1);
this.setEndSize(temp);
this.curPage = curPage;
}
public int getEndSize() {
return endSize;
}
public void setEndSize(int endSize) {
this.endSize = endSize;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRow() {
return totalRow;
}
public void setTotalRow(int totalRow) {
totalPage = totalRow/pageSize;
if(totalRow%pageSize > 0)
totalPage = totalPage + 1;
this.totalRow = totalRow;
}
public int getTotalPage(){
return this.totalPage;
}
public String getToolsMenu() {
StringBuffer str = new StringBuffer("");
int next, prev;
prev = curPage - 1;
next = curPage + 1;
if (curPage > 1) {
str.append(
"<a href=\"#\" onclick=\"document.forms(0).pages.value=1;document.forms(0).submit();\"& gt;首页</a> ");
} else {
str.append("<a href=\"#\">首页</a> ");
}
if (curPage > 1) {
str.append(
"<a href=\"#\" onclick='document.forms(0).pages.value=" +prev + ";document.forms(0).submit();'>上页</a> ");
} else {
str.append("<a href=\"#\">上页</a> ");
}
if (curPage < totalPage) {
str.append(
"<a href=\"#\" onclick='document.forms(0).pages.value=" +next + ";document.forms(0).submit();'>下页</a> ");
} else {
str.append("<a href=\"#\" >下页</a> ");
}
if (totalPage > 1 && curPage != totalPage) {
str.append(
"<a href=\"#\" onclick='document.forms(0).pages.value=" +totalPage + ";document.forms(0).submit();'>末页</a> ");
} else {
str.append("<a href=\"#\" >末页</a> ");
}
str.append(" 共" + totalRow + "条记录");
str.append(" 每页<SELECT size=1 name=pagesize onchange='this.form.pages.value=1;this.form.pageSize.value=this.value;this.form.submit();'>");
if (pageSize == 3) {
str.append("<OPTION value=3 selected>3</OPTION>");
} else {
str.append("<OPTION value=3>3</OPTION>");
}
if (pageSize == 10) {
str.append("<OPTION value=10 selected>10</OPTION>");
} else {
str.append("<OPTION value=10>10</OPTION>");
}
if (pageSize == 20) {
str.append("<OPTION value=20 selected>20</OPTION>");
} else {
str.append("<OPTION value=20>20</OPTION>");
}
if (pageSize == 50) {
str.append("<OPTION value=50 selected>50</OPTION>");
} else {
str.append("<OPTION value=50>50</OPTION>");
}
if (pageSize == 100) {
str.append("<OPTION value=100 selected>100</OPTION>");
} else {
str.append("<OPTION value=100>100</OPTION>");
}
str.append("</SELECT>");
str.append("条 分" + pageSize + "页显示 转到");
str.append("<SELECT size=1 name=Pagelist onchange='this.form.pages.value=this.value;this.form.submit();'>");
for (int i = 1; i < totalPage + 1; i++) {
if (i == curPage) {
str.append("<OPTION value=" + i + " selected>" + i +
"</OPTION>");
} else {
str.append("<OPTION value=" + i + ">" + i + "</OPTION>");
}
}
str.append("</SELECT>页");
str.append("<INPUT type=hidden value=" + curPage + " name=\"pages\" > ");
str.append("<INPUT type=hidden value=" + pageSize +
" name=\"pageSize\"> ");
return str.toString();
}
}
========配置文件=======
<typeAlias alias="leaveBill" type="com.luke.wz.kf.bean.LeaveBill"/>
<typeAlias alias="page" type="com.luke.wz.kf.bean.PageUtil"/>
<select id="getWaitingLeaveBillByPage" resultClass="leaveBill" parameterClass="page">
select
<dynamic prepend="">
<isNotEqual compareValue="0" property="pageSize" prepend="">
top $pageSize$
</isNotEqual>
* from leavebill
<isNotEqual prepend="where" compareValue="0" property="pageSize">
id not in (select top $endSize$ id from leavebill) and state != '已归档' and state != '已完成'
</isNotEqual>
<isEqual prepend="where" compareValue="0" property="pageSize">
state != '已归档' and state != '已完成'
</isEqual>
</dynamic>
</select>
=======DAO method========
public List getWaitingLeaveBill(PageUtil page){
List list = null;
try {
list = sqlMapClientTemplate.queryForList("getWaitingLeaveBillByPage",page);
} catch (Exception e) {
logger.error(e.getMessage());
}
return list;
}
=========具体操作=======
public ModelAndView toWaitingLeaveBillList(HttpServletRequest request, HttpServletResponse response){
PageUtil page = new PageUtil();
java.util.List list = leaveBillDao.getWaitingLeaveBill(page);
int temp = list.size();
if(null != request.getParameter("pageSize") && !"".equals(request.getParameter("pageSize"))){
page.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
}else{
page.setPageSize(3);
}
page.setTotalRow(temp);
if(null != request.getParameter("pages") && !"".equals(request.getParameter("pages"))){
page.setCurPage(Integer.parseInt(request.getParameter("pages")));
}else{
page.setCurPage(1);
}
request.setAttribute("menu",page.getToolsMenu());
request.setAttribute("waitinglist",leaveBillDao.getWaitingLeaveBill(page));
return new ModelAndView(WAITINGLIST);
}
来源:http://www.blogjava.net/wyz191/articles/156923.html