**EmpDAO**
// select count(id) from Emp where...order by... 只有一条结果
public Long getCountEmps(String hql, Map<String, Object> emp) {
return (Long) HibernateSessionFactory.getSession()
.createQuery("select count(id) from Emp").setProperties(emp)
.uniqueResult();
**EmpBiz**
EmpDAO dao = new EmpDAO();
public List<Emp> findByPage() {
Transaction tx = null;
List<Emp> result = null;
try {
tx = HibernateSessionFactory.getSession().beginTransaction();
int count = dao.getCountEmps(null, null).intValue();// 统计结果封装是长整数Long,这个用来取整数值
System.out.println("总共" + count + "条记录");
// (count+size-1)/size
// count % size ==0 ? (count/size):(count/size+1) 分页算法
tx.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
}
return result;
}
**T1**
EmpBiz biz = new EmpBiz();
@Test
public void test() {
List<Emp> result = biz.findByPage();
for (Emp emp : result) {
System.out.println(emp.getEname());
}
}
Hibernate分页-count总记录数的计算
最新推荐文章于 2018-09-20 19:10:23 发布