JPA实现分页
Jpa自己已经有了实现分页的基本查询方法,只要自己在网上找一个分页的前端插件,然后再用Jpa查询到数据给它。
页面传当前页和每一页的大小给后台,后台就像下面这样处理:
public List<PsrAnalysisTask> findAnalysisTask(String loginName,
Long analysisTempId,Integer pageIndex, Integer pageSize) {
Query query = null;
query = em.createQuery("select a from PsrAnalysisTask a where a.createdBy=:createdBy and a.analysistempId =:analysistempId ");
query.setParameter("createdBy", loginName);
query.setParameter("analysistempId", analysisTempId);
query.setFirstResult((pageIndex-1)*pageSize);
query.setMaxResults(pageSize);
List<PsrAnalysisTask> as = query.getResultList();
return as;
}
当然,页面要返回的值,除了查询到的列表还有查询到的总数:
@Override
public Integer getTotalCount(String loginName) {
return Integer.valueOf(em.createQuery(
"SELECT COUNT(*) FROM PsrAnalysisTask e WHERE e.createdBy = :createdBy")
.setParameter("createdBy",loginName).getSingleResult().toString());
}
至此就完全能够满足分页需求了,加油吧