0Pricing
Spring Boot 4 Microservices & REST APIs · Pelajaran

Proyeksi dan DTO

Pilih hanya data yang Anda perlukan.

Proyeksi dan DTO adalah pelajaran Spring Boot 4 Microservices & REST APIs gratis di CoddyKit. Ini adalah pelajaran 2 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.

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

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Proyeksi dan DTO” gratis?

Ya — teks lengkap “Proyeksi dan DTO” 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 “Proyeksi dan DTO”?

Pilih hanya data yang Anda perlukan. 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 2 dari 4.

Berapa lama pelajaran “Proyeksi dan DTO” 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