跪求:hibernate的分页例子或jdbc分页的例子
发布网友
发布时间:2022-04-09 03:21
我来回答
共2个回答
热心网友
时间:2022-04-09 04:50
action:
查询按钮:public ActionForward search(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
session.removeAttribute("pb");
session.removeAttribute("totalRows");
session.removeAttribute("map");
UserForm frm = (UserForm) form;
String user_id = frm.getUser_id();
String username = frm.getUsername();
MyMap map = new MyMap();
map.put("user_id", user_id);
map.setLike("username", username);
session.setAttribute("map", map);
DB db = new DB();
int totalRows = db.getRows("user.rows", map);
session.setAttribute("totalRows", totalRows);
PageBean pb = new PageBean(totalRows);
pb = new PageBean(totalRows);
int pageRecorders = pb.getPageRecorders();
List list = db.selectpart("select.userinfor", map, 0, pageRecorders);
db.close();
request.setAttribute("fmm", list);
session.setAttribute("pb", pb);
return mapping.findForward("user");
}
下一页:
public ActionForward nextPage(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
PageBean pb = (PageBean) session.getAttribute("pb");
pb.nextPage();
MyMap map = (MyMap) session.getAttribute("map");
int totalRows = pb.getTotalRows();
int currentPage = pb.getCurrentPage();
pb = new PageBean(totalRows, currentPage);
// int startRow = pb.getCurrentPage() * 5;
int pageRecorders = pb.getPageRecorders();
int startRow = pb.getPageStartRow();
// System.out.println(startRow);
DB db = new DB();
List list = db.selectpart("select.userinfor", map, startRow,
pageRecorders);
db.close();
request.setAttribute("fmm", list);
session.setAttribute("pb", pb);
return mapping.findForward("user");
}
前一页:
public ActionForward previousPage(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
PageBean pb = (PageBean) session.getAttribute("pb");
pb.previousPage();
MyMap map = (MyMap) session.getAttribute("map");
int totalRows = pb.getTotalRows();
int currentPage = pb.getCurrentPage();
pb = new PageBean(totalRows, currentPage);
DB db = new DB();
// int startRow = (pb.getCurrentPage()-1) * 5;
int startRow = pb.getPageStartRow();
int pageRecorders = pb.getPageRecorders();
List list = db.selectpart("select.userinfor", map, startRow,
pageRecorders);
db.close();
request.setAttribute("fmm", list);
session.setAttribute("pb", pb);
return mapping.findForward("user");
}
页面jsp :
<logic:notEmpty name="pb">
<logic:equal name="pb" property="hasPreviousPage" value="true">
<html:link page="/user.do?method=previousPage">
前一页
</html:link>
</logic:equal>
<logic:equal name="pb" property="hasNextPage" value="true">
<html:link page="/user.do?method=nextPage">
下一页
</html:link>
</logic:equal>
</logic:notEmpty>
分页算法类:
package method;
public class PageBean {
private int currentPage = 1; // 当前页
private int totalPages = 0; // 总页数
private int pageRecorders = 5;// 每页5条数据
private int totalRows = 0; // 总数据数
private int pageStartRow = 0;// 每页的起始数
private int pageEndRow = 0; // 每页显示数据的终止数
private boolean hasNextPage = false; // 是否有下一页
private boolean hasPreviousPage = false; // 是否有前一页
private int nextPage = 0;// 下一页的页码
private int previousPage = 0;// 上一页的页码
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public PageBean(int totalRows) {
this.totalRows = totalRows;
this.currentPage = 1;
hasPreviousPage = false;
if ((totalRows % pageRecorders) == 0) {
totalPages = totalRows / pageRecorders;
} else {
totalPages = totalRows / pageRecorders + 1;
}
if (totalRows >= pageRecorders) {
hasNextPage = true;
nextPage = 2;
this.pageEndRow = pageRecorders;
} else {
this.pageEndRow = totalRows;
hasNextPage = false;
nextPage = 1;
}
this.pageStartRow = 0;
previousPage = 1;
}
public PageBean(int totalRows,int currentPage) {
this.totalRows = totalRows;
this.currentPage = currentPage;
if(currentPage < 2)
hasPreviousPage = false;
else
hasPreviousPage = true;
if ((totalRows % pageRecorders) == 0) {
totalPages = totalRows / pageRecorders;
} else {
totalPages = totalRows / pageRecorders + 1;
}
if (currentPage < totalPages) {
hasNextPage = true;
nextPage = currentPage + 1;
pageStartRow = (currentPage - 1)*pageRecorders;
this.pageEndRow = pageStartRow + pageRecorders;
} else if(currentPage == totalPages){
pageStartRow = (currentPage - 1)*pageRecorders;
this.pageEndRow = totalRows;
hasNextPage = false;
nextPage = currentPage;
}
if(currentPage < 2){
previousPage = currentPage;
hasPreviousPage = false;
}else if(currentPage > 1){
previousPage = currentPage-1;
hasPreviousPage = true;
}
}
// 然后在按下一页或者上一页的时候需要如下函数处理
public void nextPage() {
if (hasNextPage == true)
currentPage = currentPage + 1;
if ((currentPage - 1) > 0) {
hasPreviousPage = true;
} else {
hasPreviousPage = false;
}
if (currentPage >= totalPages) {
hasNextPage = false;
this.nextPage = currentPage;
} else {
hasNextPage = true;
nextPage = currentPage + 1;
}
this.pageStartRow = (currentPage - 1) * pageRecorders;
if (hasNextPage == true)
this.pageEndRow = pageStartRow + 5;
else {
this.pageEndRow = this.totalPages;
}
previousPage = currentPage - 1;
}
public void previousPage() {
if (hasPreviousPage == true)
currentPage = currentPage - 1;
if (currentPage == 0) {
currentPage = 1;
}
if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
nextPage = currentPage + 1;
if ((currentPage - 1) > 0) {
hasPreviousPage = true;
previousPage = currentPage - 1;
} else {
hasPreviousPage = false;
previousPage = currentPage;
}
this.pageStartRow = (currentPage - 1) * pageRecorders;
if (hasNextPage == true)
this.pageEndRow = pageStartRow + 5;
else {
this.pageEndRow = this.totalPages;
}
}
public int getPageEndRow() {
return pageEndRow;
}
public void setPageEndRow(int pageEndRow) {
this.pageEndRow = pageEndRow;
}
public int getPageStartRow() {
return pageStartRow;
}
public void setPageStartRow(int pageStartRow) {
this.pageStartRow = pageStartRow;
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public int getPageRecorders() {
return pageRecorders;
}
public void setPageRecorders(int pageRecorders) {
this.pageRecorders = pageRecorders;
}
public int getPreviousPage() {
return previousPage;
}
public void setPreviousPage(int previousPage) {
this.previousPage = previousPage;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
}
热心网友
时间:2022-04-09 06:08
如果要个复杂点的分页的话 在这提问题是没用的 自己上书找或者是去csdn找去
比方说要个简单点的分页的话 仔细想想如何运用手头的SQL语句来实现(尽量不要在结果里分页)
select top 4 from 表 where id not in(select top pageskip from 表 order by id)order by id
比方说 你要一页显示4条语句 那么 pageskip就是4的倍数 剩下的自己推去吧