Spring Boot 4 Microservices & REST APIs · บทเรียน

การแบ่งหน้าและการเรียงลำดับ

ส่งคืนชุดข้อมูลขนาดใหญ่อย่างมีประสิทธิภาพ

บทเรียน 4 จาก 413 ขั้นตอน

การแบ่งหน้าและการเรียงลำดับ เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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,desc

Mapping 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-based
  • Page carries content plus totals; Slice skips the count
  • Sort.by(...) for ordering
  • Web layer binds page/size/sort into a Pageable
เริ่มต้นได้ฟรี

เรียนรู้ Java ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
24
บทเรียน
93

คำถามที่พบบ่อย

บทเรียน “การแบ่งหน้าและการเรียงลำดับ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแบ่งหน้าและการเรียงลำดับ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแบ่งหน้าและการเรียงลำดับ”

ส่งคืนชุดข้อมูลขนาดใหญ่อย่างมีประสิทธิภาพ คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การแบ่งหน้าและการเรียงลำดับ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม

ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. คำค้นแบบไดนามิกด้วยข้อมูลจำเพาะ
  2. โพรเจกชันและ DTO
  3. การตรวจสอบประวัติด้วย @CreatedDate
  4. การแบ่งหน้าและการเรียงลำดับ
← กลับไปที่ Spring Boot 4 Microservices & REST APIs