springdatajpa 分页优化,如何让findall方法也返回Slice,不再count
简单的查询是可以的,但是我的findAll方法里有动态的参数拼接查询,查询时还是会返回count
JPA分页可以借助 JpaRepository 可以参考下这个 http://t.csdn.cn/CqGlJ
Slice<OutHspinfo> findAll(Specification<OutHspinfo> specification, Pageable pageable);
先看下JPA提供的findAll接口的代码,没有返回Slice的。
public interface CrudRepository<T, ID extends Serializable>
extends Repository<T, ID> {
<S extends T> S save(S entity);
Optional<T> findById(ID primaryKey);
Iterable<T> findAll();
long count();
void delete(T entity);
boolean existsById(ID primaryKey);
// … more functionality omitted.
}
public interface PagingAndSortingRepository<T, ID extends Serializable>
extends CrudRepository<T, ID> {
Iterable<T> findAll(Sort sort);
Page<T> findAll(Pageable pageable);
}
public interface QueryDslPredicateExecutor<T> {
Optional<T> findById(Predicate predicate);
Iterable<T> findAll(Predicate predicate);
long count(Predicate predicate);
boolean exists(Predicate predicate);
// … more functionality omitted.
}
上面是JPA自带的findAl接口,你可以跟踪一下JPA提供的findAll方法有没有你需要的返回类型,如果没有的话,大概率无法设置去掉count查询。
自带的findAll是查询全部数据,这个方法你完成可以重载或者写一个单独的方法实现。
Page<User> findByLastname(String lastname, Pageable pageable);
Slice<User> findByLastname(String lastname, Pageable pageable);