ページネーションとソート
大量のデータセットを効率よく返します
「ページネーションとソート」はCoddyKit上の無料Spring Boot 4 Microservices & REST APIsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSpring Boot 4 Microservices & REST APIs学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Why Paginate
Returning thousands of rows at once is slow and memory-heavy. Pagination fetches one page at a time.
Spring Data provides Pageable, Page, and Sort for this.
PagingAndSortingRepository
JpaRepository already includes paging and sorting support, so you get these methods for free.
public interface UserRepository
extends JpaRepository<User, Long> {
}Creating a Pageable
PageRequest.of(page, size) builds a request. Page numbers are zero-based.
Pageable pageable = PageRequest.of(0, 20);
Page<User> page = userRepository.findAll(pageable);The Page Object
A Page carries the content plus metadata.
List<User> content = page.getContent();
long total = page.getTotalElements();
int pages = page.getTotalPages();
boolean hasNext = page.hasNext();Sorting
Build a Sort and pass it alone or inside a PageRequest.
Sort sort = Sort.by("name").ascending();
List<User> sorted = userRepository.findAll(sort);Combining Sort and Paging
Pass a Sort into PageRequest.of.
Pageable pageable = PageRequest.of(
0, 20, Sort.by("name").descending());
Page<User> page = userRepository.findAll(pageable);Multiple Sort Orders
Chain sort orders for tie-breaking.
Sort sort = Sort.by(
Sort.Order.asc("lastName"),
Sort.Order.desc("createdAt"));Paging Derived Queries
Add a Pageable parameter to any derived query method.
Page<User> findByActiveTrue(Pageable pageable);Slice vs Page
A Slice knows only whether a next page exists, skipping the expensive COUNT query that Page runs. Use Slice for infinite scroll.
Slice<User> findByActiveTrue(Pageable pageable);Pageable from the Web Layer
Spring MVC can bind page, size, and sort request params into a Pageable automatically.
@GetMapping("/users")
public Page<User> list(Pageable pageable) {
return userRepository.findAll(pageable);
}
// GET /users?page=0&size=20&sort=name,descMapping Page Content to DTOs
Transform a Page's entities to DTOs while preserving pagination metadata with map.
Page<UserDto> dtos = page.map(
user -> new UserDto(user.getName(), user.getEmail()));Quick Check
Test your understanding of pagination.
Recap
You learned pagination and sorting:
PageRequest.of(page, size)— page is zero-basedPagecarries content plus totals;Sliceskips the countSort.by(...)for ordering- Web layer binds
page/size/sortinto aPageable
よくある質問
「ページネーションとソート」レッスンは無料ですか?
はい。「ページネーションとソート」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Boot 4 Microservices & REST APIsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。
「ページネーションとソート」で何を学びますか?
大量のデータセットを効率よく返します ブラウザで直接実行するハンズオンコードでSpring Boot 4 Microservices & REST APIsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Spring Boot 4 Microservices & REST APIsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSpring Boot 4 Microservices & REST APIsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「ページネーションとソート」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSpring Boot 4 Microservices & REST APIsレッスンでコードを書いて実行できますか?
はい。すべてのSpring Boot 4 Microservices & REST APIsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。