@SuppressWarnings("rawtypes") private void setPagers(Query query, Pager pages) { Integer pageSize = SystemContext.getPageSize(); Integer pageOffset = SystemContext.getPageOffset(); if (pageOffset == null || pageOffset < 0) pageOffset = 0; if (pageSize == null || pageSize < 0) pageSize = 15; pages.setOffset(pageOffset); pages.setSize(pageSize); query.setFirstResult(pageOffset).setMaxResults(pageSize); }
public Pager<T> find(String hql, Object[] args, Map<String, Object> alias) { hql = initSort(hql); String cq = getCountHql(hql, true); Query cquery = getSession().createQuery(cq); Query query = getSession().createQuery(hql); // 设置别名参数 setAliasParameter(query, alias); setAliasParameter(cquery, alias); // 设置参数 setParameter(query, args); setParameter(cquery, args); Pager<T> pages = new Pager<T>(); setPagers(query, pages); List<T> datas = query.list(); pages.setRows(datas); long total = (Long) cquery.uniqueResult(); pages.setTotal(total); return pages; }
public <N extends Object> Pager<N> findBySql( String sql, Object[] args, Map<String, Object> alias, Class<?> clz, boolean hasEntity) { sql = initSort(sql); String cq = getCountHql(sql, false); SQLQuery sq = getSession().createSQLQuery(sql); SQLQuery cquery = getSession().createSQLQuery(cq); setAliasParameter(sq, alias); setAliasParameter(cquery, alias); setParameter(sq, args); setParameter(cquery, args); Pager<N> pages = new Pager<N>(); setPagers(sq, pages); if (hasEntity) { sq.addEntity(clz); } else { sq.setResultTransformer(Transformers.aliasToBean(clz)); } List<N> datas = sq.list(); pages.setRows(datas); long total = ((BigInteger) cquery.uniqueResult()).longValue(); pages.setTotal(total); return pages; }