SSH分页

package com.whaugmentum.training.util;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.whaugmentum.training.entity.PageBean;
import com.whaugmentum.training.entity.Request;
import com.whaugmentum.training.exception.DaoException;
import com.whaugmentum.training.exception.ServiceException;

public class PagingUtil extends HibernateDaoSupport {


public PageBean getDataByPage(StringBuffer hql, int pageSize, int page, Object[] obj)
throws ServiceException {

int allRow;
try {

allRow = getAllRowCount(hql.toString(), obj); //总记录数
} catch (Exception e) {

logger.info(e.toString());
throw new ServiceException("Exception in PrepareServiceImp");
}
int totalPage = PageBean.countTotalPage(pageSize, allRow); //Count of page
final int offset = PageBean.countOffset(pageSize, page); //The first data of the page
final int length = pageSize; //every count of request
final int currentPage = PageBean.countCurrentPage(page);

//Get One page Request
List<Request> list;
try {

list = prepareDataOnePage(hql.toString(), offset, length, obj); //one page request
} catch (Exception e) {

logger.info(e.toString());
throw new ServiceException("Exception in PrepareServiceImp");
}

//put the information into pageBean(include page info and data of the specified page)
PageBean pageBean = new PageBean();
pageBean.setPageSize(pageSize);
pageBean.setCurrentPage(currentPage);
pageBean.setAllRow(allRow);
pageBean.setTotalPage(totalPage);
pageBean.setList(list);
pageBean.init();

return pageBean;
}


private int getAllRowCount(String hql, Object[] obj) throws DaoException {

try {

return getHibernateTemplate().find(hql, obj).size();
} catch (Exception e) {

e.printStackTrace();
throw new DaoException(e,
"Exception in PrepareDaoImp, and username can't get Users ");
}
}

@SuppressWarnings("unchecked")
private List<Request> prepareDataOnePage(final String hql, final int offset,
final int length, final Object[] obj) throws DaoException {

List list;
try {

list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
if(null != obj){
for(int i = 0 ; i < obj.length; i++){

query.setParameter(i, obj[i]);
}
}
List<Request> list = query.list();
return list;
}
});
} catch (Exception e) {

e.printStackTrace();
throw new DaoException(e,
"Exception in PrepareDaoImp, and username can't get Users ");
}
return list;

}

@SuppressWarnings("unchecked")
public static void main(String[] args) {

String hql = "select r.requestid, r.title, r.user.username, r.requesttime, r.approvetime, r.operator.username, r.reqType.typename, r.reqStatus.statusname FROM Request r left join r.operator where 1 = 1 and r.user.userid=? order by r.requesttime desc";
Configuration config = new Configuration().configure();
SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();
Query query = session.createQuery(hql);
query.setInteger(0, 5);
List list = query.list();
System.out.println(list);
session.close();
factory.close();
}

}
package com.whaugmentum.training.entity;

import java.util.List;

@SuppressWarnings({"unchecked", "unused"})
public class PageBean {


private List list; //要返回的某一页的记录列表

private int allRow; //总记录数
private int totalPage; //总页数
private int currentPage; //当前页
private int pageSize; //每页记录数

private boolean isFirstPage; //是否为第一页
private boolean isLastPage; //是否为最后一页
private boolean hasPreviousPage; //是否有前一页
private boolean hasNextPage; //是否有下一页

public List getList() {
return list;
}

public void setList(List list) {
this.list = list;
}

public int getAllRow() {
return allRow;
}

public void setAllRow(int allRow) {
this.allRow = allRow;
}

public int getTotalPage() {
return totalPage;
}

public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}

public int getCurrentPage() {
return currentPage;
}

public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}



public void init() {
this.isFirstPage = isFirstPage();
this.isLastPage = isLastPage();
this.hasPreviousPage = isHasPreviousPage();
this.hasNextPage = isHasNextPage();
}


public boolean isFirstPage() {
return currentPage == 1; // 如是当前页是第1页
}

public boolean isLastPage() {
return currentPage == totalPage; //如果当前页是最后一页
}

public boolean isHasPreviousPage() {
return currentPage != 1; //只要当前页不是第1页
}

public boolean isHasNextPage() {
return currentPage != totalPage; //只要当前页不是最后1页
}



public static int countTotalPage(final int pageSize, final int allRow) {
int totalPage = allRow % pageSize == 0 ? allRow / pageSize : allRow
/ pageSize + 1;
return totalPage;
}



public static int countOffset(final int pageSize, final int currentPage) {
final int offset = pageSize * (currentPage - 1);
return offset;
}



public static int countCurrentPage(int page) {
final int curPage = (page == 0 ? 1 : page);
return curPage;
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值