这篇文章主要讲解了“Struts+Hibernate分页怎么实现”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Struts+Hibernate分页怎么实现”吧!
一、在Struts中分页有两种结构:
1. 在Action中通过DAO查询出所有的记录,然后加到session或request对象中,传到客户端,由JSP进行分页。这种方法对于在数据量少的时候很方便,也不影响速度。
2.在Action中每次通过DAO只查询出一页的记录,再传给JSP页面。这种结构对于数据量大的程序很好,但对于数据量小的情况,会增加对服务器的请求,加大服务器的负载。
二、Hibernate查询
由于在Hibernate中直接提供了对数据库定点定量的查询方法,所以我采用的是第2种方法。
如:从第1万条开始取出100条记录
Query q = session.createQuery("from Cat as c"); q.setFirstResult(10000);
q.setMaxResults(100);
List l = q.list(); |
三、具体实现
1.Pager类
package com.jpcf.db.helper;
import java.math.*;
public class Pager {
private int totalRows; //总行数
private int pageSize = 10; //每页显示的行数
private int currentPage; //当前页号
private int totalPages; //总页数
private int startRow; //当前页在数据库中的起始行
public Pager() {
}
public Pager(int _totalRows) {
totalRows = _totalRows;
totalPages=totalRows/pageSize;
int mod=totalRows%pageSize;
if(mod>0){
totalPages++;
}
currentPage = 1;
startRow = 0;
}
public int getStartRow() {
return startRow;
}
public int getTotalPages() {
return totalPages;
}
public int getCurrentPage() {
return currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRows() {
return totalRows;
}
public void first() {
currentPage = 1;
startRow = 0;
}
public void previous() {
if (currentPage == 1) {
return;
}
currentPage--;
startRow = (currentPage - 1) * pageSize;
}
public void next() {
if (currentPage < totalPages) {
currentPage++;
}
startRow = (currentPage - 1) * pageSize;
}
public void last() {
currentPage = totalPages;
startRow = (currentPage - 1) * pageSize;
}
public void refresh(int _currentPage) {
currentPage = _currentPage;
if (currentPage > totalPages) {
last();
}
}}
Pager类用于计算首页、前一页、下一页、尾页的在数据库中的起始行,当前的页码。
2.PagerHelp类
package com.jpcf.db.helper; import javax.servlet.http.*; public class PagerHelper { public static Pager getPager(HttpServletRequest httpServletRequest,
int totalRows) { |
//定义pager对象,用于传到页面
Pager pager = new Pager(totalRows); |
//从Request对象中获取当前页号
String currentPage = httpServletRequest.getParameter("currentPage"); |
//如果当前页号为空,表示为***查询该页
//如果不为空,则刷新pager对象,输入当前页号等信息
if (currentPage != null) {
pager.refresh(Integer.parseInt(currentPage));
} |
//获取当前执行的方法,首页,前一页,后一页,尾页。
String pagerMethod = httpServletRequest.getParameter("pageMethod"); if (pagerMethod != null) {
if (pagerMethod.equals("first")) {
pager.first();
} else if (pagerMethod.equals("previous")) {
pager.previous();
} else if (pagerMethod.equals("next")) {
pager.next();
} else if (pagerMethod.equals("last")) {
pager.last();
}
}
return pager;
}
} |
PageHelper这个类,我不用说应该也知道用来干嘛了
3.DAO类
package com.jpcf.db.dao;
import com.jpcf.db.model.*;
import com.jpcf.db.helper.HibernateUtil;
import net.sf.hibernate.*;
import java.util.*;
import com.jpcf.db.controller.*;
public class VehiclePropertyDAO {
public Collection findWithPage(int pageSize, int startRow) throws
HibernateException {
Collection vehicleList = null;
Transaction tx = null;
try {
Session session = HibernateUtil.currentSession();
tx = session.beginTransaction();
Query q = session.createQuery("from VehicleProperty vp");
q.setFirstResult(startRow);
q.setMaxResults(pageSize);
vehicleList = q.list();
tx.commit();
} catch (HibernateException he) {
if (tx != null) {
tx.rollback();
}
throw he;
} finally {
HibernateUtil.closeSession();
}
return vehicleList;
}
public int getRows(String query) throws
HibernateException {
int totalRows = 0;
Transaction tx = null;
try {
Session session = HibernateUtil.currentSession();
tx = session.beginTransaction();
totalRows = ((Integer) session.iterate(query).next()).
intValue();
tx.commit();
} catch (HibernateException he) {
if (tx != null) {
tx.rollback();
}
throw he;
} finally {
HibernateUtil.closeSession();
}
return totalRows;
}
}
DAO类我就贴这些分页需要的代码了。
“from VehicleProperty vp”也可以用一个参数传进来,有兴趣的自己改一下吧
4.Action
下面是在Action中用到的代码:
public ActionForward queryWithPage(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletresponse) { |
Collection clInfos = null;//用于输出到页面的记录集合
int totalRows;//记录总行数
VehiclePropertyDAO vehicleDAO = new VehiclePropertyDAO(); |
//取得当前表中的总行数
try {
totalRows = vehicleDAO.getRows("select count(*) from VehicleProperty");
} catch (Exception ex) {
servlet.log(ex.toString());
return actionMapping.findForward(Constants.FAILURE);
} |
//通过PagerHelper类来获取用于输出到页面的pager对象
Pager pager=PagerHelper.getPager(httpServletRequest,totalRows); |
//取出从startRow开始的pageSize行记录
try {
clInfos = vehicleDAO.findWithPage(pager.getPageSize(), pager.getStartRow());
} catch (Exception ex) {
servlet.log(ex.toString());
return actionMapping.findForward(Constants.FAILURE);
} |
//把输出的记录集和pager对象保存到request对象中
httpServletRequest.setAttribute("CLINFOS", clInfos);
httpServletRequest.setAttribute("PAGER", pager); return actionMapping.findForward(Constants.SUCCESS); |
查询语句select count(*) from VehicleProperty 也可以换成你需要的任意的条件(select count(*) from VehicleProperty where ..)
5.JSP页面使用
下面就是在JSP中的应用了:
第页
共页
="/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=first
上一页
下一页
尾页
" paramName="PAGER" paramProperty="currentPage" paramId="currentPage">首页 |
解释一下这一行:"/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=first
method=queryWithPage 是由于我的Action继承的是DispatchAction,需要一个method参数
pageMethod=first 是用来在PageHelper类中判断执行哪个操作
感谢各位的阅读,以上就是“Struts+Hibernate分页怎么实现”的内容了,经过本文的学习后,相信大家对Struts+Hibernate分页怎么实现这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。