hibernate 分页

hibernate分页,还是不错的,不会每次取出的指定的数据个数,不会倒置系统的问题。
下面是具体实现代码:
Dao.
/**
* 分页查询方法
*
* @param informationtypeId
* @param informationtitle
* @param pageSize
* @param startRow
* @return
*/
public List findInfoList(long informationtypeId, String informationtitle,
int pageSize, int startRow) {
final int pageSize1 = pageSize;
final int startRow1 = startRow;
log.debug("attaching clean Information instance");
try {
String sql = "from Information info where info.informationtypeId = ? ";
String sql1 = "from Information info where info.informationtypeId = ? and info.informationtitle like ?";
if (null == informationtitle || "".equals(informationtitle)) {
Query query = getSession().createQuery(sql);
query.setFirstResult(startRow1);
query.setMaxResults(pageSize1);
query.setLong(0, informationtypeId);

log.debug("attach successful");
return query.list();
} else {
Query query = getSession().createQuery(sql1);
query.setLong(0, informationtypeId);
query.setFirstResult(startRow1);
query.setMaxResults(pageSize1);
query.setString(1, "%" + informationtitle + "%");

log.debug("attach successful");
return query.list();
}

} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

/**
* 分页用到的获取记录数方法
*
* @param informationtypeId
* @param informationtitle
* @return
*/
public int getRows(long informationtypeId, String informationtitle) {
log.debug("attaching clean Information instance");
try {
String sql = "select count(info.informationId) from Information info where info.informationtypeId = ? ";
String sql1 = "select count(info.informationId) from Information info where info.informationtypeId = ? and info.informationtitle like ?";
if (null == informationtitle || "".equals(informationtitle)) {
Query query = getSession().createQuery(sql);
query.setLong(0, informationtypeId);

log.debug("attach successful");
return Integer.parseInt(query.list().get(0).toString());
} else {
Query query = getSession().createQuery(sql1);
query.setLong(0, informationtypeId);
query.setString(1, "%" + informationtitle + "%");

log.debug("attach successful");
return Integer.parseInt(query.list().get(0).toString());
}

} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
通过service注入到action,实现方法
/**
* 新闻List查询页面,messageList.jsp
*
* @return
* @throws Exception
*/
public String messageList() throws Exception {
// TODO Auto-generated method stub
// this.setInfoTypeList(iMessageManaService.finInfoTypeList());

/*
* this.setInfoList(iMessageManaService.findInfoList(informationtitle,
* Long.parseLong(informationtypeId)));
*/

int totalRow = iMessageManaService.getRows(Long
.parseLong(informationtypeId), informationtitle);
pager = pagerService.getPager(this.getCurrentPage(), this
.getPagerMethod(), totalRow);
this.setCurrentPage(String.valueOf(pager.getCurrentPage()));
this.setTotalRows(String.valueOf(totalRow));
this.setInfoList(iMessageManaService.findInfoList(Long
.parseLong(informationtypeId), informationtitle, pager
.getPageSize(), pager.getStartRow()));
return SUCCESS;

}


显示页面JSP
/**
* 新闻List查询页面,messageList.jsp
*
* @return
* @throws Exception
*/
public String messageList() throws Exception {
// TODO Auto-generated method stub
// this.setInfoTypeList(iMessageManaService.finInfoTypeList());

/*
* this.setInfoList(iMessageManaService.findInfoList(informationtitle,
* Long.parseLong(informationtypeId)));
*/

int totalRow = iMessageManaService.getRows(Long
.parseLong(informationtypeId), informationtitle);
pager = pagerService.getPager(this.getCurrentPage(), this
.getPagerMethod(), totalRow);
this.setCurrentPage(String.valueOf(pager.getCurrentPage()));
this.setTotalRows(String.valueOf(totalRow));
this.setInfoList(iMessageManaService.findInfoList(Long
.parseLong(informationtypeId), informationtitle, pager
.getPageSize(), pager.getStartRow()));
return SUCCESS;

}

Pager类
package com.xmetc.jmu.oldman.sims.common;
/**
* Pager类用于计算首页、前一页、下一页、尾页的在数据库中的起始行,当前的页码
* @author LT
*
*/

public class Pager {
private int totalRows; //总行数
private int pageSize = 5; //每页显示的行数
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();
}
}
}
PageHelper类
package com.xmetc.jmu.oldman.sims.common;
/**
* Pager类的辅助类
* @author LT
*
*/


public class PageHelper {
public Pager getPager(String currentPage,String pagerMethod,int totalRows) {
// 定义pager对象,用于传到页面
Pager pager = new Pager(totalRows);
// 如果当前页号为空,表示为首次查询该页
// 如果不为空,则刷新pager对象,输入当前页号等信息
if (currentPage != null) {
pager.refresh(Integer.parseInt(currentPage));
}
// 获取当前执行的方法,首页,前一页,后一页,尾页。
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;
}
}
这样就可以实现HIBERNATE分页,继续学习中~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值