文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Struts+hibernate分页显示(资料四)[DAO]

Struts+hibernate分页显示(资料四)[DAO]

时间:2007-08-08  来源:sdwsyjp

Action中PagerHelp类以及DAO

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

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

}
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载