问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

跪求: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的倍数 剩下的自己推去吧
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
若cpu一秒执行n次运算需耗能5w,那执行2n次运算要耗能是大于小于还是等于... 大神们,N5G跟N5W哪里不一样? 刚出生的婴儿会感冒吗 刚出生宝宝感冒的症状 怀庄永佳福代理,什么品牌白酒好呢? 做白酒品牌代理,有怀庄永佳福牌子怎么样? 怀庄永佳福白酒喝得人多吗?代理怎么样? 为什么很多女性都把男闺密当作结婚对象? 关于电影“罗生门”的问题 实际利率法是指什么 企业挂牌转让是什么意思? 企业网络信用平台查询结果是什么 不同层次的挂牌股票分别有哪些转让方式? 公开转让与协议转让的区别? 网上买了的硅胶笼布高温后真的安全吗? 为什么外地人打电话讲话的声音特大? 为什么人生气的时候会大声说话? 有什么健康方便简单的瘦大腿的方法吗? 为什么说话声音大 频率就高 怎么样才瘦大腿根啊??帮帮!~~拜托各位大神 人在生气的时候为什么会说话那么大声? 人生气的时候或吵架的时候说话声音为什么会很大? 为什么说话声音大的人会让我心烦意乱 所谓“7号篮球”是标准篮球吗? 为什么很多人认为自己说话声音大,别人就会怕自己? 为什么有的人说话大嗓门?怎么声音温柔一点? 为什么有的学生上课时讲话声音很大?像平时说话一样,他们没有意识到老师和其他学生也能听到吗? 为什么我们说话总那么大声麻烦告诉我 7号在足球里代表什么 打哪个位置 为什么有些人平时说话会很大声,心理学上怎么分析 吃什么东西有助于提高记忆力和智商? 怎么提高自己的智商? 衡水信都学校附近名吃 怎么提高记忆力和智商 如何有效提高智商与记忆力? 怎样可以提高智商和记忆力? 北京天安门附近有什么美味小吃? 西华大学附近好吃的小吃? 雅图ACTO DX226怎么链接笔记本 什么牌子的投影仪好 打算把KTV包房的电视换成国产投影机 有什么好的介绍 增删改查实现的前提下,在servlet里的dopost方法里写东西 喝菊花枸杞茶对身体有什么好处 我想知道抖音视频是怎么玩的?怎么样可以直播? 抖音直播女团是什么 玩法怎么玩? 小升初学生个人简历优秀模版-精华版 奶牛烂脚丫怎么治,站不起来,还不肯吃食。 悬浮拼装地板缺点有哪些 软连接悬浮式拼装地板跟普通的悬浮地板有什么区别? 悬浮地板尺寸一般是多少