Projeksiyonlar ve DTO'lar
Yalnızca ihtiyacınız olan verileri seçin.
Projeksiyonlar ve DTO'lar, CoddyKit'te ücretsiz bir Spring Boot 4 Microservices & REST APIs dersidir. Bu, 4 dersinin 2. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Spring Boot 4 Microservices & REST APIs öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Spring Boot 4 Microservices & REST APIs kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
Why Projections
Fetching whole entities when you need only a few columns wastes memory and bandwidth.
Projections let you select a subset of fields, returning lightweight objects instead of full entities.
Interface Projections
Declare an interface with getters for the fields you want. Spring Data implements it as a proxy.
public interface UserSummary {
String getName();
String getEmail();
}Returning a Projection
Use the interface as the query method return type.
public interface UserRepository
extends JpaRepository<User, Long> {
List<UserSummary> findByActiveTrue();
}Closed vs Open Projections
A closed projection only exposes existing properties (efficient — selects only those columns). An open projection computes values with @Value and SpEL (loads the whole entity).
Open Projection Example
Combine fields with a SpEL expression.
public interface UserView {
@Value("#{target.firstName + ' ' + target.lastName}")
String getFullName();
}Class-Based Projections (DTOs)
A DTO class with a matching constructor is another projection style. Spring Data maps query columns to constructor parameters by name.
public class UserDto {
private final String name;
private final String email;
public UserDto(String name, String email) {
this.name = name;
this.email = email;
}
// getters
}Returning a DTO
Use the DTO class as the return type, just like interface projections.
public interface UserRepository
extends JpaRepository<User, Long> {
List<UserDto> findByActiveTrue();
}DTOs in JPQL Constructor Expressions
With custom JPQL, use a constructor expression to build DTOs directly.
@Query("SELECT new com.app.UserDto(u.name, u.email) "
+ "FROM User u WHERE u.active = true")
List<UserDto> findActiveSummaries();Dynamic Projections
Make the return type generic so one method serves multiple projections.
<T> List<T> findByActiveTrue(Class<T> type);
// usage
repo.findByActiveTrue(UserSummary.class);
repo.findByActiveTrue(UserDto.class);Projections with Native Queries
Interface projections also work with native SQL queries when column names match the getters.
@Query(value = "SELECT name, email FROM users",
nativeQuery = true)
List<UserSummary> findAllSummaries();Choosing an Approach
Prefer closed interface projections for read-only lists (most efficient). Use DTO classes when you need behavior or want them outside the persistence layer.
Quick Check
Test your understanding of projections.
Recap
You learned to return lightweight results:
- Interface projections — closed (efficient) or open (SpEL)
- Class-based DTO projections via constructor mapping
- JPQL constructor expressions for custom queries
- Dynamic projections with a generic type parameter
Yapay zeka eğitmeniyle Java öğren — ücretsiz
Tarayıcında gerçek kod yaz ve çalıştır, 7/24 yapay zeka eğitmeninden anında yardım al; web'de ya da uygulamada kaldığın yerden devam et.
- Kurslar
- 24
- Dersler
- 93
Sıkça Sorulan Sorular
“Projeksiyonlar ve DTO'lar” dersi ücretsiz mi?
Evet — “Projeksiyonlar ve DTO'lar” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Spring Boot 4 Microservices & REST APIs kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Spring Boot 4 Microservices & REST APIs kursu toplamda 4 dersten oluşur.
“Projeksiyonlar ve DTO'lar” dersinde ne öğreneceğim?
Yalnızca ihtiyacınız olan verileri seçin. Spring Boot 4 Microservices & REST APIs ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Spring Boot 4 Microservices & REST APIs öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Spring Boot 4 Microservices & REST APIs, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 2. dersidir.
“Projeksiyonlar ve DTO'lar” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Spring Boot 4 Microservices & REST APIs dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Spring Boot 4 Microservices & REST APIs dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Specifications ile Dinamik Sorgular
- Projeksiyonlar ve DTO'lar
- @CreatedDate ile Denetim
- Sayfalama ve Sıralama