JPA implement paging

JPA implement paging


Jpa themselves have a basic query methods to achieve paging, as long as they find a front-end plug-in page on the Internet, then use Jpa queries to the data to it.


Page biography of the current page and each page size to the background, the background like this deal:

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;

    }

Of course, the value of the page to be returned, in addition to the list of queries as well as the total number of queries to:

@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());
}

At this point it is fully able to meet the needs of a page, filling it

Published 46 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/kingbox000/article/details/50349345