public Page<T> queryByConditionNQ(String sql, Map<String, Object> param, Pageable pageRequest) {
Query q = em.createNativeQuery(sql);
String countSql = "select count(*) from (" + sql + ") count_sql";
Query countQ = em.createNativeQuery(countSql);
for (Map.Entry<String, Object> entry : param.entrySet()) {
q.setParameter(entry.getKey(), entry.getValue());
countQ.setParameter(entry.getKey(), entry.getValue());
}
Assert.notNull(q);
Assert.notNull(countQ);
List<Object> totals = countQ.getResultList();
Long total = 0L;
for (Object elementObj : totals) {
BigInteger elementBi = (BigInteger) elementObj;//转换成大类型
Long element = elementBi.longValue(); //转换成long类型
total += element == null ? 0 : element;
}
q.setFirstResult(pageRequest.getOffset());
q.setMaxResults(pageRequest.getPageSize());
List<T> content = total > pageRequest.getOffset() ? q.getResultList() : Collections
.<T> emptyList();
return new PageImpl<T>(content, pageRequest, total);
}
jpa 分页
最新推荐文章于 2024-08-07 18:13:39 发布