hibernate分页
在搭建好的hibernate环境中:
一个简单的小列子
/**
*查询姓名包换b的员工 并且薪水降序排序
*/
@Test
public void test(){
Criteria criteria = session.createCriteria(Emp.class);
criteria.add(Restrictions.ilike("name", "b", MatchMode.ANYWHERE)).
setProjection(Projections.count("name"));
int count=((Long) criteria.uniqueResult()).intValue();
//设置当前页和页大小
int pageIndex=3;
int pageSize=2;
//计算总页数
int pageTotalCount=(count%pageSize==0)?count/pageSize:count/pageSize+1;
//根据薪水降序排列
Criteria order = session.createCriteria(Emp.class).add(Restrictions.ilike("name", "b",MatchMode.ANYWHERE)).
addOrder(Order.desc("sal"));
//设置起始页和页大小
List<Emp> list = order.setFirstResult((pageIndex-1)*pageSize).
setMaxResults(pageSize).list();
for (Emp emp : list) {
System.out.println(emp);
}
}
}