//方法描述:根据会员名称和页容量分页查询代理人
public List<HbUser> findUserByPage(int page,int pageSize, String userName)
throws Exception {
Session session = sessionFactory.getCurrentSession();
StringBuffer hql = new StringBuffer("from HbUser where isDelete = 0 and " +
"auditStatus = 3 and postId in (1,2,3,4) ");//创建一个字符缓冲 队列
//如果userName不为空 或者空字符串 就对StringBuffer进行拼接
if(userName != null && userName.trim().length() != 0){
hql.append(" and userName LIKE ‘%"+userName+"%‘");
}
Query query = session.createQuery(hql.toString());
query.setFirstResult((page - 1) * pageSize);//从page页第一条记录开始
query.setMaxResults(pageSize);//页容量
if(query.list().size() != 0){
return query.list();
}
return null;
}
查询总的页数
public int findUserTotalPage(int pageSize, String userName) throws Exception {
Session session = sessionFactory.getCurrentSession();
StringBuffer hql = new StringBuffer("select count(userId) from HbUser where " +
" isDelete = 0 and auditStatus = 3 and postId in (1,2,3,4) ");//创建一个字符缓冲 队列
//如果userName不为空 或者空字符串 就对StringBuffer进行拼接
if(userName != null && userName.trim().length() != 0){
hql.append(" and userName LIKE ‘%"+userName+"%‘");
}
Query query = session.createQuery(hql.toString());
Long num = (Long)query.uniqueResult();
long numn = num;//把Long对象拆装成long基本类型
int totalPage = (int) numn;//把long类型的数据强制转换成int类型
return totalPage;
}
hibernate 中的query的分页查询,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/mxyhws/p/3817967.html