0Pricing
Spring Boot 4 Microservices & REST APIs · 강의

Specifications로 동적 쿼리 만들기

프로그램으로 유연한 쿼리를 만들어 보세요.

Specifications로 동적 쿼리 만들기은(는) CoddyKit의 무료 Spring Boot 4 Microservices & REST APIs 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 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 AI 튜터), CoddyKit PRO로 업그레이드하면 Spring Boot 4 Microservices & REST APIs 강의 전체를 잠금 해제할 수 있습니다. Spring Boot 4 Microservices & REST APIs 강의에는 총 4개의 강의가 포함되어 있습니다.

“Specifications로 동적 쿼리 만들기”에서 뭘 배우나요?

프로그램으로 유연한 쿼리를 만들어 보세요. 브라우저에서 직접 실행하는 실습 코드로 Spring Boot 4 Microservices & REST APIs을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Spring Boot 4 Microservices & REST APIs을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Spring Boot 4 Microservices & REST APIs은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.

“Specifications로 동적 쿼리 만들기” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Spring Boot 4 Microservices & REST APIs 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Spring Boot 4 Microservices & REST APIs 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. Specifications로 동적 쿼리 만들기
  2. 프로젝션과 DTO
  3. @CreatedDate로 감사 추적하기
  4. 페이지 매김과 정렬
← Spring Boot 4 Microservices & REST APIs(으)로 돌아가기