คำค้นแบบไดนามิกด้วยข้อมูลจำเพาะ
สร้างคำค้นที่ยืดหยุ่นด้วยโปรแกรม
คำค้นแบบไดนามิกด้วยข้อมูลจำเพาะ เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Microservices & REST APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Problem with Static Queries
When users can filter by many optional fields, writing one query method per combination explodes quickly.
Spring Data Specifications let you build queries dynamically at runtime based on which filters are present.
JpaSpecificationExecutor
Extend JpaSpecificationExecutor on your repository to enable Specification-based queries.
public interface UserRepository
extends JpaRepository<User, Long>,
JpaSpecificationExecutor<User> {
}A Specification
A Specification<T> describes a single WHERE predicate using the JPA Criteria API.
Specification<User> hasName(String name) {
return (root, query, cb) ->
cb.equal(root.get("name"), name);
}Executing a Specification
Pass the spec to findAll.
List<User> users =
userRepository.findAll(hasName("Alice"));Combining with and
Specifications compose with and, or, and not.
Specification<User> spec =
hasName("Alice").and(isActive(true));
List<User> users = userRepository.findAll(spec);Building Conditionally
The real power: add predicates only when the filter is supplied.
Specification<User> spec = Specification.where(null);
if (name != null) {
spec = spec.and(hasName(name));
}
if (active != null) {
spec = spec.and(isActive(active));
}
return userRepository.findAll(spec);Like and Comparison Predicates
The Criteria builder offers many predicate types.
Specification<User> nameLike(String part) {
return (root, query, cb) ->
cb.like(root.get("name"), "%" + part + "%");
}
Specification<User> olderThan(int age) {
return (root, query, cb) ->
cb.greaterThan(root.get("age"), age);
}Joining Related Entities
Specifications can navigate joins to filter on associated entities.
Specification<User> inCity(String city) {
return (root, query, cb) ->
cb.equal(root.join("address").get("city"), city);
}Specifications with Paging
Combine a Specification with a Pageable for filtered, paginated results.
Page<User> page = userRepository.findAll(
spec, PageRequest.of(0, 20));Counting Matches
count accepts a Specification too.
long matches = userRepository.count(hasName("Alice"));Reusing Predicate Factories
Group spec factories in a helper class so they can be reused and unit tested.
public class UserSpecs {
public static Specification<User> active() {
return (root, q, cb) ->
cb.isTrue(root.get("active"));
}
}Quick Check
Test your understanding of Specifications.
Recap
You learned dynamic querying:
- Extend
JpaSpecificationExecutor - A
Specificationis one Criteria predicate - Compose with
and/or, add conditionally - Combine with
Pageablefor filtered pages
เรียนรู้ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “คำค้นแบบไดนามิกด้วยข้อมูลจำเพาะ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- คำค้นแบบไดนามิกด้วยข้อมูลจำเพาะ
- โพรเจกชันและ DTO
- การตรวจสอบประวัติด้วย @CreatedDate
- การแบ่งหน้าและการเรียงลำดับ