@SuppressWarnings("unchecked")
// 执行Query的list方法返回分页的结果集
public List select(int nowPage, int pageSize)
{
Session session = null
Transaction trans =null
Query query =null
try {
session = this.getSessionFactory().openSession()
trans= session.getTransaction()
trans.begin()
String hql = "from SrtPayManagement"
query= session.createQuery(hql)
query.setFirstResult((nowPage - 1) * pageSize)
query.setMaxResults(pageSize)
trans.commit()
return query.list()
} finally {
//下面代码为避免hibernate占用内存资源过多而做的处理
session.clear()
session.close()
}
}
@SuppressWarnings("unchecked")
public int getPageCount() throws Exception {
String hql="from SrtPayManagement"
List list=this.getHibernateTemplate().find(hql)
if(null==list || list.size()<=0){
return 0
}
return list.size()
}
action方法:
/***********分页参数 start************/
private int pageSize =10//每页显示多少条数据
private int currentPage=1//当前页
@SuppressWarnings("unused")
private int totalPages=0//总条数
private int pageCount=0//总页数
/***********分页参数 end************/
public synchronized String findPaymentManage(){
try {
payManageListTwo = paymentManage.findPaymentInfo()
totalPages = paymentManage.getPageCount()
pageCount = totalPages / pageSize
if (totalPages % pageSize != 0) {
pageCount++
}
SessionHelp.getSession().setAttribute("pageCount", pageCount)
if(currentPage >= pageCount){
payManageList= paymentManage.select(pageCount, pageSize)
}else {
if(currentPage <=1){
payManageList= paymentManage.select(1, pageSize)
}else {
payManageList= paymentManage.select(currentPage, pageSize)
}
}
} catch (Exception e) {
errorInfo = e.getMessage()
logger.error(e)
return "unity_failure"
}
SessionHelp.getSession().setAttribute("pageCount", pageCount)
SessionHelp.getSession().setAttribute("totalPages", totalPages)
return "payment"
}
//queryString HQL语句 pageSize每页显示多少条 page当前所在也public List<Object>queryListObjectAllForPage(String queryString,int pageSize,int page){
Session session = this.getHibernateTemplate().getSessionFactory().openSession()
Query query = session.createQuery(queryString)
query.setFirstResult((page-1)*pageSize)
query.setMaxResults(pageSize)
List<Object>list= (List<Object>)query.list()
session.close()
return list
}
他们大体相同:criteria中如下:
criteria.setFirstResult(0)
criteria.setMaxResults(10)
hql中如下,其中Object代表你所要查询的对象名:
String hql = "from Object"
Query query = getSession().createQuery(hql)
query.setFirstResult(0)
query.setMaxResults(10)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)