分页查询count方法-删除hql中的order by字句
/** * 获取查询数据总数 * * @param hql * @param params * @return 结果集总数 */ public int count(String hql, Map<String, Object> params) { // Query q = this.createQuery(hql, params); // ScrollableResults scroll = q.scroll(); // scroll.last(); // return scroll.getRowNumber() + 1; // replaceAll忽略大小写: (?i) hql = hql.replaceAll("(?i)order\s+(?i)by\s+[^()]*", ""); StringBuilder sb = new StringBuilder(); sb.append("SELECT COUNT(*) "); sb.append(hql.substring(hql.toLowerCase().indexOf("from"))); Object o = this.createQuery(sb.toString(), params).uniqueResult(); return o == null ? 0 : Integer.parseInt("" + o); }
---