0Pricing
Spring Boot 4 Microservices & REST APIs · Pelajaran

Kueri Dinamis dengan Spesifikasi

Bangun kueri fleksibel secara terprogram.

Kueri Dinamis dengan Spesifikasi adalah pelajaran Spring Boot 4 Microservices & REST APIs gratis di CoddyKit. Ini adalah pelajaran 1 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Spring Boot 4 Microservices & REST APIs, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Spring Boot 4 Microservices & REST APIs mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Kueri Dinamis dengan Spesifikasi” gratis?

Ya — teks lengkap “Kueri Dinamis dengan Spesifikasi” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Spring Boot 4 Microservices & REST APIs, upgrade ke CoddyKit PRO. Kursus Spring Boot 4 Microservices & REST APIs mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Kueri Dinamis dengan Spesifikasi”?

Bangun kueri fleksibel secara terprogram. Kamu berlatih Spring Boot 4 Microservices & REST APIs dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Spring Boot 4 Microservices & REST APIs?

Tidak diperlukan pengalaman sebelumnya. Spring Boot 4 Microservices & REST APIs di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 1 dari 4.

Berapa lama pelajaran “Kueri Dinamis dengan Spesifikasi” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Spring Boot 4 Microservices & REST APIs ini?

Ya. Setiap pelajaran Spring Boot 4 Microservices & REST APIs menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Kueri Dinamis dengan Spesifikasi
  2. Proyeksi dan DTO
  3. Audit dengan @CreatedDate
  4. Paginasi dan Pengurutan
← Kembali ke Spring Boot 4 Microservices & REST APIs