/** * Reads the given {@link TypedQuery} into a {@link Page} applying the given {@link Pageable} and * {@link Specification}. * * @param query must not be {@literal null}. * @param spec can be {@literal null}. * @param pageable must not be {@literal null}. * @return * @deprecated use {@link #readPage(TypedQuery, Class, Pageable, Specification)} instead */ @Deprecated protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, @Nullable Specification<T> spec) { return readPage(query, getDomainClass(), pageable, spec); }
@Override public Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable) { TypedQuery<T> query = getQuery(spec, pageable); return isUnpaged(pageable) ? new PageImpl<T>(query.getResultList()) : readPage(query, getDomainClass(), pageable, spec); }
@Override public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) { ExampleSpecification<S> spec = new ExampleSpecification<>(example); Class<S> probeType = example.getProbeType(); TypedQuery<S> query = getQuery(new ExampleSpecification<>(example), probeType, pageable); return isUnpaged(pageable) ? new PageImpl<>(query.getResultList()) : readPage(query, probeType, pageable, spec); }
/** * Reads the given {@link TypedQuery} into a {@link Page} applying the given {@link Pageable} and * {@link Specification}. * * @param query must not be {@literal null}. * @param spec can be {@literal null}. * @param pageable must not be {@literal null}. * @return * @deprecated use {@link #readPage(TypedQuery, Class, Pageable, Specification)} instead */ @Deprecated protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, @Nullable Specification<T> spec) { return readPage(query, getDomainClass(), pageable, spec); }
public Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable) { TypedQuery<T> query = getQuery(spec, pageable); return isUnpaged(pageable) ? new PageImpl<T>(query.getResultList()) : readPage(query, getDomainClass(), pageable, spec); }
@Override @Deprecated protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, Specification<T> spec) { Page<T> page = super.readPage(query, pageable, spec); return pageable == null ? new WrappedPageImpl<T>(page.getContent()) : new WrappedPageImpl<T>(page.getContent(), pageable, page.getTotalElements()); }
@Override public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) { ExampleSpecification<S> spec = new ExampleSpecification<>(example); Class<S> probeType = example.getProbeType(); TypedQuery<S> query = getQuery(new ExampleSpecification<>(example), probeType, pageable); return isUnpaged(pageable) ? new PageImpl<>(query.getResultList()) : readPage(query, probeType, pageable, spec); }
@Override protected <S extends E> Page<S> readPage(TypedQuery<S> query, Class<S> domainClass, Pageable pageable, @Nullable Specification<S> spec) { super.readPage(query, domainClass, pageable, spec); //首先查询满足条件的记录数 Long total = executeCountQuery(getCountQuery(spec, domainClass)); //检查查询的数据是否超出总记录数,如果超出总记录数,默认查询最后一页,并修改pageable参数 if (pageable.getOffset() >= total) { int pagenum = 0; //由于页号是从0开始,所以需要考虑整除的情况 if (total % pageable.getPageSize() == 0) { pagenum = Ints.checkedCast(Math.max(total / pageable.getPageSize() - 1, 0)); } else { pagenum = Ints.checkedCast(total / pageable.getPageSize()); } log.info("总记录数为 {} ,当前页号为 {} 。超出分页查询范围,只返回最后一页(第{}页)的数据。", total, pageable.getPageNumber(), pagenum); pageable = PageRequest.of(pagenum, pageable.getPageSize()); } //设置分页查询参数 query.setFirstResult(Long.valueOf(pageable.getOffset()).intValue()); query.setMaxResults(pageable.getPageSize()); List<S> content = total > pageable.getOffset() ? query.getResultList() : Collections.<S>emptyList(); return new PageImpl<S>(content, pageable, total); }
@Override protected <S extends T> Page<S> readPage(TypedQuery<S> query, Class<S> domainClass, Pageable pageable, Specification<S> spec) { Page<S> page = super.readPage(query, domainClass, pageable, spec); return pageable == null ? new WrappedPageImpl<S>(page.getContent()) : new WrappedPageImpl<S>(page.getContent(), pageable, page.getTotalElements()); }