浅谈Struts分页中的Hibernate如何实现

在进行web应用开发的时候经常要进行分页处理,经常看到一些人在问分页处理的问题,现在我把自己的处理方法写在这儿,希望能对需要进行分页处理的朋友有所帮助。
首页 新闻资讯 行业资讯 浅谈Struts分页中的Hibernate如何实现

在进行web应用开发的时候经常要对Struts分页处理,经常看到一些人在问Struts分页处理的问题,现在我把自己的处理方法写在这儿,希望能对需要进行Struts分页处理的朋友有所帮助。

一、在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.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.


三、具体实现

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();

}

}

}

  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

  • 28.

  • 29.

  • 30.

  • 31.

  • 32.

  • 33.

  • 34.

  • 35.

  • 36.

  • 37.

  • 38.

  • 39.

  • 40.

  • 41.

  • 42.

  • 43.

  • 44.

  • 45.

  • 46.

  • 47.

  • 48.

  • 49.

  • 50.

  • 51.

  • 52.

  • 53.

  • 54.

  • 55.

  • 56.

  • 57.

  • 58.

  • 59.

  • 60.

  • 61.

  • 62.

  • 63.

  • 64.

  • 65.

  • 66.

  • 67.

  • 68.

  • 69.

  • 70.

  • 71.

  • 72.

  • 73.

  • 74.

  • 75.

  • 76.

  • 77.

  • 78.

  • 79.

  • 80.

  • 81.

  • 82.

  • 83.

  • 84.

  • 85.

  • 86.

  • 87.

  • 88.

  • 89.

  • 90.

  • 91.

  • 92.

  • 93.

  • 94.

  • 95.

  • 96.

  • 97.

  • 98.

  • 99.

  • 100.

  • 101.

  • 102.

  • 103.

  • 104.

  • 105.

  • 106.

  • 107.

  • 108.

  • 109.

  • 110.

  • 111.

  • 112.

  • 113.

  • 114.

  • 115.

  • 116.

  • 117.

  • 118.

  • 119.

  • 120.

  • 121.

  • 122.

  • 123.

  • 124.

  • 125.

  • 126.

  • 127.

  • 128.

  • 129.

  • 130.

  • 131.

  • 132.

  • 133.

  • 134.

  • 135.

  • 136.

  • 137.

  • 138.

  • 139.

  • 140.

  • 141.

  • 142.

  • 143.

  • 144.

  • 145.

  • 146.

  • 147.

  • 148.

  • 149.

  • 150.

  • 151.

  • 152.

  • 153.

  • 154.

  • 155.

  • 156.

  • 157.

  • 158.

  • 159.

  • 160.

  • 161.

  • 162.

  • 163.

  • 164.

  • 165.

  • 166.

  • 167.

  • 168.

  • 169.

  • 170.

  • 171.

  • 172.

  • 173.

  • 174.

  • 175.

  • 176.

  • 177.

  • 178.

  • 179.

  • 180.

  • 181.

  • 182.

  • 183.

  • 184.

  • 185.

  • 186.

  • 187.

  • 188.

  • 189.

  • 190.

  • 191.

  • 192.

  • 193.

  • 194.

  • 195.

  • 196.

  • 197.

  • 198.

  • 199.

  • 200.

  • 201.

  • 202.

  • 203.

  • 204.

  • 205.

  • 206.

  • 207.

  • 208.

  • 209.

  • 210.

  • 211.

  • 212.

  • 213.

  • 214.

  • 215.

  • 216.

  • 217.

  • 218.

  • 219.

  • 220.

  • 221.

  • 222.

  • 223.

  • 224.

  • 225.

  • 226.

  • 227.

  • 228.

  • 229.

  • 230.

  • 231.

  • 232.

  • 233.

  • 234.

  • 235.

  • 236.

  • 237.

  • 238.

  • 239.

  • 240.

  • 241.

  • 242.

  • 243.

  • 244.

  • 245.

  • 246.

  • 247.

  • 248.

  • 249.

  • 250.

  • 251.

  • 252.

  • 253.

  • 254.

  • 255.

  • 256.

  • 257.

  • 258.

  • 259.

  • 260.

  • 261.

  • 262.

  • 263.

  • 264.

  • 265.

  • 266.

  • 267.

  • 268.

  • 269.

  • 270.

  • 271.

  • 272.

  • 273.

  • 274.

  • 275.

  • 276.

  • 277.

  • 278.

  • 279.

  • 280.

  • 281.

  • 282.

  • 283.

  • 284.

  • 285.

  • 286.

  • 287.

  • 288.

  • 289.

  • 290.

  • 291.

  • 292.

  • 293.

  • 294.

  • 295.

  • 296.

  • 297.

  • 298.

  • 299.

  • 300.

  • 301.

  • 302.

  • 303.

  • 304.

  • 305.

  • 306.

  • 307.

  • 308.

  • 309.

  • 310.

  • 311.

  • 312.

  • 313.

  • 314.

  • 315.

  • 316.

  • 317.

  • 318.

  • 319.

  • 320.

  • 321.

  • 322.

  • 323.

  • 324.

  • 325.

  • 326.

  • 327.

  • 328.

  • 329.

  • 330.

  • 331.

  • 332.

  • 333.

  • 334.

  • 335.

  • 336.

  • 337.

  • 338.

  • 339.

  • 340.

  • 341.

  • 342.

  • 343.

  • 344.

  • 345.

  • 346.

  • 347.

  • 348.

  • 349.

  • 350.

  • 351.

  • 352.

  • 353.

  • 354.

  • 355.

  • 356.

  • 357.

  • 358.

  • 359.

  • 360.

  • 361.

  • 362.

  • 363.

  • 364.

  • 365.

  • 366.

  • 367.

  • 368.

  • 369.

  • 370.

  • 371.

  • 372.

  • 373.

  • 374.

  • 375.

  • 376.

  • 377.

  • 378.

  • 379.

  • 380.

  • 381.

  • 382.

  • 383.

  • 384.

  • 385.

  • 386.



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;

}

}

  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

  • 28.

  • 29.

  • 30.

  • 31.

  • 32.

  • 33.

  • 34.

  • 35.

  • 36.

  • 37.

  • 38.

  • 39.

  • 40.

  • 41.

  • 42.

  • 43.

  • 44.

  • 45.

  • 46.

  • 47.

  • 48.

  • 49.

  • 50.

  • 51.

  • 52.

  • 53.

  • 54.

  • 55.

  • 56.

  • 57.

  • 58.

  • 59.

  • 60.

  • 61.

  • 62.

  • 63.

  • 64.

  • 65.

  • 66.

  • 67.

  • 68.

  • 69.

  • 70.

  • 71.

  • 72.

  • 73.

  • 74.

  • 75.

  • 76.

  • 77.

  • 78.

  • 79.

  • 80.

  • 81.

  • 82.

  • 83.

  • 84.

  • 85.

  • 86.

  • 87.

  • 88.

  • 89.

  • 90.

  • 91.

  • 92.

  • 93.

  • 94.

  • 95.

  • 96.

  • 97.

  • 98.

  • 99.

  • 100.

  • 101.

  • 102.

  • 103.

  • 104.

  • 105.

  • 106.

  • 107.

  • 108.

  • 109.

  • 110.

  • 111.

  • 112.

  • 113.

  • 114.

  • 115.

  • 116.

  • 117.

  • 118.

  • 119.

  • 120.

  • 121.

  • 122.

  • 123.

  • 124.

  • 125.

  • 126.

  • 127.

  • 128.

  • 129.

  • 130.

  • 131.

  • 132.

  • 133.

  • 134.

  • 135.

  • 136.

  • 137.

  • 138.

  • 139.

  • 140.

  • 141.

  • 142.

  • 143.

  • 144.

  • 145.

  • 146.



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;

}

}

  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

  • 28.

  • 29.

  • 30.

  • 31.

  • 32.

  • 33.

  • 34.

  • 35.

  • 36.

  • 37.

  • 38.

  • 39.

  • 40.

  • 41.

  • 42.

  • 43.

  • 44.

  • 45.

  • 46.

  • 47.

  • 48.

  • 49.

  • 50.

  • 51.

  • 52.

  • 53.

  • 54.

  • 55.

  • 56.

  • 57.

  • 58.

  • 59.

  • 60.

  • 61.

  • 62.

  • 63.

  • 64.

  • 65.

  • 66.

  • 67.

  • 68.

  • 69.

  • 70.

  • 71.

  • 72.

  • 73.

  • 74.

  • 75.

  • 76.

  • 77.

  • 78.

  • 79.

  • 80.

  • 81.

  • 82.

  • 83.

  • 84.

  • 85.

  • 86.

  • 87.

  • 88.

  • 89.

  • 90.

  • 91.

  • 92.

  • 93.

  • 94.

  • 95.

  • 96.

  • 97.

  • 98.

  • 99.

  • 100.

  • 101.

  • 102.

  • 103.

  • 104.

  • 105.

  • 106.

  • 107.

  • 108.

  • 109.

  • 110.

  • 111.

  • 112.

  • 113.

  • 114.

  • 115.

  • 116.

  • 117.

  • 118.

  • 119.

  • 120.

  • 121.

  • 122.

  • 123.

  • 124.

  • 125.

  • 126.

  • 127.

  • 128.

  • 129.

  • 130.

  • 131.

  • 132.

  • 133.

  • 134.

  • 135.

  • 136.

  • 137.

  • 138.

  • 139.

  • 140.

  • 141.

  • 142.

  • 143.

  • 144.

  • 145.

  • 146.

  • 147.

  • 148.

  • 149.

  • 150.

  • 151.

  • 152.

  • 153.

  • 154.

  • 155.

  • 156.

  • 157.

  • 158.

  • 159.

  • 160.

  • 161.

  • 162.

  • 163.

  • 164.

  • 165.

  • 166.

  • 167.

  • 168.

  • 169.

  • 170.

  • 171.

  • 172.

  • 173.

  • 174.

  • 175.

  • 176.

  • 177.

  • 178.

  • 179.

  • 180.

  • 181.

  • 182.

  • 183.

  • 184.

  • 185.

  • 186.

  • 187.

  • 188.

  • 189.

  • 190.

  • 191.

  • 192.

  • 193.

  • 194.

  • 195.

  • 196.

  • 197.

  • 198.

  • 199.

  • 200.

  • 201.

  • 202.

  • 203.

  • 204.

  • 205.

  • 206.

  • 207.

  • 208.

  • 209.

  • 210.

  • 211.

  • 212.

  • 213.

  • 214.

  • 215.

  • 216.

  • 217.

  • 218.

  • 219.

  • 220.

  • 221.

  • 222.

  • 223.

  • 224.

  • 225.

  • 226.

  • 227.

  • 228.

  • 229.

  • 230.

  • 231.

  • 232.

  • 233.

  • 234.

  • 235.

  • 236.

  • 237.

  • 238.

  • 239.

  • 240.

  • 241.

  • 242.

  • 243.

  • 244.

  • 245.

  • 246.



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);

}

  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

  • 28.

  • 29.

  • 30.

  • 31.

  • 32.

  • 33.

  • 34.

  • 35.

  • 36.

  • 37.

  • 38.

  • 39.

  • 40.

  • 41.

  • 42.

  • 43.

  • 44.

  • 45.

  • 46.

  • 47.

  • 48.

  • 49.

  • 50.

  • 51.

  • 52.

  • 53.

  • 54.

  • 55.

  • 56.

  • 57.

  • 58.

  • 59.

  • 60.

  • 61.

  • 62.

  • 63.

  • 64.

  • 65.

  • 66.

  • 67.

  • 68.

  • 69.

  • 70.

  • 71.

  • 72.

  • 73.

  • 74.

  • 75.

  • 76.

  • 77.

  • 78.

  • 79.

  • 80.

  • 81.

  • 82.

  • 83.

  • 84.

  • 85.

  • 86.

  • 87.

  • 88.

  • 89.

  • 90.

  • 91.

  • 92.

  • 93.

  • 94.

  • 95.

  • 96.

  • 97.

  • 98.

  • 99.

  • 100.

  • 101.

  • 102.

  • 103.

  • 104.

  • 105.

  • 106.

  • 107.

  • 108.

  • 109.

  • 110.

  • 111.

  • 112.

  • 113.

  • 114.

  • 115.

  • 116.

  • 117.

  • 118.

  • 119.

  • 120.

  • 121.

  • 122.

  • 123.

  • 124.

  • 125.

  • 126.

  • 127.

  • 128.

  • 129.

  • 130.

  • 131.

  • 132.

  • 133.

  • 134.

  • 135.

  • 136.



查询语句select count(*) from VehicleProperty 也可以换成你需要的任意的条件(select count(*) from VehicleProperty where ..)

5.JSP页面使用

下面就是在JSP中的应用了: 


复制

="/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=first
  • 1.

复制

" paramName="PAGER" paramProperty="currentPage" paramId="currentPage">首页
  • 1.


 

解释一下这一行:"/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=first


复制

method=queryWithPage
  • 1.

是由于我的Action继承的是DispatchAction,需要一个method参数


pageMethod=first 是用来在PageHelper类中判断执行哪个操作

四、Struts分页总结

我做的这个也只是一个借鉴,还有很多没有实现的,比如还可以加一下 go 直接到第n页的功能。

其实最关键的是把当前页号和要执行的是功能(上一页,下一页)的参数从页面传进来,在Action中就可以根据这两个参数去取下一个页面上要显示的记录集了

【编辑推荐】

  1. 在Eclipse中开发struts应用程序

  2. 手把手教你在Eclipse中配置开发Struts

  3. Eclipse下开发struts完整解决乱码问题

  4. Struts相关背景介绍

  5. 使用Easy Struts for Eclipse开发Struts

41    2009-06-05 09:52:25    struts分页 Hibernate