Hibernate分页-count总记录数的计算

**EmpDAO**
// select count(id) from Emp where...order by... 只有一条结果
    public Long getCountEmps(String hql, Map emp) {
        return (Long) HibernateSessionFactory.getSession()
                .createQuery("select count(id) from Emp").setProperties(emp)
                .uniqueResult();
**EmpBiz**
EmpDAO dao = new EmpDAO();

    public List findByPage() {
        Transaction tx = null;
        List 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 result = biz.findByPage();
        for (Emp emp : result) {
            System.out.println(emp.getEname());
        }
    }

你可能感兴趣的:(Hibernate分页-count总记录数的计算)