はfindAll(Predicate, Pageable)
の戻り値としてSlice
を返すサポートしていませんので、カウントクエリは避けられないようです。しかし、新しいベースリポジトリインターフェイスを定義し、findAll
メソッドを実装して、ページネーションのカウントクエリを発行しないようにすることができます。基本的には、この実装はフェッチう
public class SliceableRepositoryImpl<T, ID extends Serializable>
extends QueryDslJpaRepository<T, ID>
implements SliceableRepository<T, ID> {
private static final EntityPathResolver DEFAULT_ENTITY_PATH_RESOLVER = SimpleEntityPathResolver.INSTANCE;
private final EntityPath<T> path;
private final PathBuilder<T> builder;
private final Querydsl querydsl;
public SliceableRepositoryImpl(JpaEntityInformation<T, ID> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
path = DEFAULT_ENTITY_PATH_RESOLVER.createPath(entityInformation.getJavaType());
this.builder = new PathBuilder<>(path.getType(), path.getMetadata());
this.querydsl = new Querydsl(entityManager, builder);
}
@Override
public Page<T> findAll(Predicate predicate, Pageable pageable) {
int oneMore = pageable.getPageSize() + 1;
JPQLQuery query = createQuery(predicate)
.offset(pageable.getOffset())
.limit(oneMore);
Sort sort = pageable.getSort();
query = querydsl.applySorting(sort, query);
List<T> entities = query.list(path);
int size = entities.size();
if (size > pageable.getPageSize())
entities.remove(size - 1);
return new PageImpl<>(entities, pageable, pageable.getOffset() + size);
}
}
:よう、このインタフェースを実装し、その後
/**
* Interface for adding one method to all repositories.
*
* <p>The main motivation of this interface is to provide a way
* to paginate list of items without issuing a count query
* beforehand. Basically we're going to get one element more
* than requested and form a {@link Page} object out of it.</p>
*/
@NoRepositoryBean
public interface SliceableRepository<T, ID extends Serializable>
extends JpaRepository<T, ID>,
QueryDslPredicateExecutor<T> {
Page<T> findAll(Predicate predicate, Pageable pageable);
}
:まず第一に、あなたは他のすべてのリポジトリの基本インタフェースとして使用されるインターフェイスを定義する必要があります要求されたサイズよりも1つ多い要素とし、その結果をPage
の構築に使用します。
@EnableJpaRepositories(repositoryBaseClass = SliceableRepositoryImpl.class)
そして最後に、あなたの基本インタフェースとしてSliceableRepository
を拡張する:次に、あなたは、リポジトリ基本クラスとしてこの実装を使用するように春のデータを教えてください
public SomeRepository extends SliceableRepository<Some, SomeID> {}
私たちといくつかのコードを共有してくださいことはできますか? 'count'クエリは、通常、ページングの一部であり、UIの総ページ数/結果の数を表示することができます。なぜあなたのケースで 'カウント '部分が遅いのですか? – miensol