0Pricing
Spring Boot 4 Microservices & REST APIs · درس

الاستعلامات الديناميكية باستخدام Specifications

أنشئ استعلامات مرنة برمجيًا

الاستعلامات الديناميكية باستخدام Specifications درس مجاني في Spring Boot 4 Microservices & REST APIs على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 Specification is one Criteria predicate
  • Compose with and/or, add conditionally
  • Combine with Pageable for filtered pages

الأسئلة الشائعة

هل درس «الاستعلامات الديناميكية باستخدام Specifications» مجاني؟

نعم — نص درس «الاستعلامات الديناميكية باستخدام Specifications» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Boot 4 Microservices & REST APIs، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.

ماذا ستتعلم في «الاستعلامات الديناميكية باستخدام Specifications»؟

أنشئ استعلامات مرنة برمجيًا تتمرن على Spring Boot 4 Microservices & REST APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Spring Boot 4 Microservices & REST APIs؟

لا تُشترط خبرة سابقة. Spring Boot 4 Microservices & REST APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «الاستعلامات الديناميكية باستخدام Specifications»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Spring Boot 4 Microservices & REST APIs هذا؟

نعم. كل درس في Spring Boot 4 Microservices & REST APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. الاستعلامات الديناميكية باستخدام Specifications
  2. الإسقاطات وDTOs
  3. التدقيق باستخدام @CreatedDate
  4. التقسيم إلى صفحات والفرز
← العودة إلى Spring Boot 4 Microservices & REST APIs