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

来源:互联网 发布:js使input失去焦点 编辑:程序博客网 时间:2024/04/29 05:07
**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());        }    }
1 0