Dynamische Abfragen mit Specifications
Erstellen Sie flexible Abfragen programmatisch.
Dynamische Abfragen mit Specifications ist eine kostenlose Spring Boot 4 Microservices & REST APIs-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Spring Boot 4 Microservices & REST APIs-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Spring Boot 4 Microservices & REST APIs-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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
Häufig gestellte Fragen
Ist die Lektion „Dynamische Abfragen mit Specifications“ kostenlos?
Ja — der vollständige Text von „Dynamische Abfragen mit Specifications“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Spring Boot 4 Microservices & REST APIs-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Spring Boot 4 Microservices & REST APIs-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Dynamische Abfragen mit Specifications“?
Erstellen Sie flexible Abfragen programmatisch. Du übst Spring Boot 4 Microservices & REST APIs mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Spring Boot 4 Microservices & REST APIs zu starten?
Keine Vorkenntnisse erforderlich. Spring Boot 4 Microservices & REST APIs auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Dynamische Abfragen mit Specifications“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Spring Boot 4 Microservices & REST APIs-Lektion Code schreiben und ausführen?
Ja. Jede Spring Boot 4 Microservices & REST APIs-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Dynamische Abfragen mit Specifications
- Projections und DTOs
- Auditing mit @CreatedDate
- Seitennummerierung und Sortierung