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