π 곡λΆνκ² λ κ³κΈ°
λ§€λ² Entity μ DTO μ μ°¨μ΄μ μ¬μ©λ²μ λν΄ ν·κ°λ¦¬κ³ , μ νμνμ§μ λν΄ μκ°νλ μ μ΄ λ§λ€. νλ‘μ νΈμμλ λ€λ₯Έ νμ λΆλ€μ DTO λ₯Ό μμ λ‘κ² μ°μλλ° κ·Έλ μ§ λͺ»ν λλ₯Ό 보면μ 빨리 κΌ κ³΅λΆν΄μΌκ² λ€κ³ μκ°νλ€.

QNA μλΉμ€λ₯Ό 볡μ΅νλ μ€ μ€μ μμ μμλ μ€ν΅νμ§λ§ DTO κ΄λ ¨ λ΄μ©μ΄ λμμ μ€μ€λ‘ ν΄λ³΄κΈ°λ‘ νλ€.

1οΈβ£ DTO μμ±
package com.ay.study.qna.question;
public class QuestionDto {
public static class QuestionInfo {
private String title;
private LocalDateTime createDate;
}
}
μΌλ¨ QuestionDto μμ± ν νλ©΄μ νμν 컬λΌμΈ μ λͺ©κ³Ό, μμ±λ μ§λ₯Ό λ©€λ² λ³μλ‘νλ QuestionInfo λ₯Ό μΆκ°λ‘ μμ±νμλ€.
2οΈβ£ Contorller μμ QuestionDto λ₯Ό λ°μμ Modle λ‘ λ겨주λλ‘
@Controller
@RequiredArgsConstructor
public class QuestionController {
// .
// .
@RequestMapping("/question/dtolist")
public String showDtoList (Model model) {
// questionService λ‘ λΆν° QuestionInfo λ‘ λ List λ°μμ΄
List<QuestionInfo> questionDtoList = questionService.getQuestionDtoList();
model.addAttribute("questionDtoList",questionDtoList);
return "question_dto_list";
}
}
3οΈβ£ Service μμ κ° return
@RequiredArgsConstructor
@Service
public class QuestionService {
// Entity μ체λ₯Ό λ겨주λ λ°©μ
private final QuestionRepository questionRepository;
public List<Question> getQuestionList() {
return questionRepository.findAll();
}
// (μΆμ² λ°©μ) Entity λ₯Ό νμν μ 보λ€μ 골λΌμ λ΄μ DTO λ‘ λ³ννμ¬ λ겨주λ λ°©μ
public List<QuestionInfo> getQuestionDtoList() {
return questionRepository.findAll().stream()
.map(QuestionInfo::fromEntity).collect(Collectors.toList());
}
}
4οΈβ£ Service λ¨μμ λ°μμ¨ Entity μμ μ 보λ₯Ό κ°μ§κ³ μ¬ μ μλλ‘ ν¨μ μμ±
public class QuestionDto {
@Getter
@Setter
@Builder
public static class QuestionInfo {
private String subject;
private LocalDateTime createDate;
// Service λ‘λΆν° λ°μμ¨ question μ λ°νμΌλ‘ μμ±
public static QuestionInfo fromEntity(Question question) {
return QuestionInfo.builder()
.subject(question.getSubject())
.createDate(question.getCreateDate())
.build();
}
}
}
5οΈβ£ DTO κ°μ²΄λ₯Ό μ μμ μΌλ‘ λ°μμλμ§ νμΈ
β λΈλΌμ°μ λ₯Ό ν΅ν νμΈ
<table>
<thead>
<tr>
<th>μ λͺ©</th>
<th>μμ±μΌμ</th>
</tr>
</thead>
<tbody>
<tr th:each="question : ${questionDtoList}">
<td th:text="${question.subject}"></td>
<td th:text="${question.createDate}"></td>
</tr>
</tbody>
</table>

β‘ PostMan μ ν΅ν νμΈ (μ΄λ€ ννλ‘ κ°μ§κ³ μ€λμ§ κΆκΈν΄μ)
- DTO λ₯Ό μ¬μ©ν΄μ νμν 컬λΌλ€λ§ κ°μ§κ³ μ€λ κ²½μ°

- Entity μ 체λ₯Ό κ°μ§κ³ μ€λ κ²½μ° (λΉ¨ : κΈ°μ‘΄ DTO, ν : DTO λ₯Ό μ¬μ©νμ§ μμμΌλ‘μ μΆκ°λ‘ κ°μ Έμ€κ² λλ λ°μ΄ν°)

π μκ° μ 리
κ°λ¨νκ²μ§λ§ μ¬μ©ν΄λ³΄λ©΄μ λλμ μ ν¬κ²
β νμν λΆλΆλ§ 골λΌμ μ¬μ© : νλ‘μ νΈλ₯Ό ν λ μ νμμ±μ λ§μ΄ λλΌμ§ λͺ»νλμ§ μκ°ν΄λ³΄λ©΄ ToDoList μ νΉμ±μ νλκ° λ΄μ©, μλ£μ¬λΆ, λ±λ‘ λ μ§ μ λλ‘ λ§€μ° μμνΈμΈλ° λλΆλΆμ κ³Όμ μμ ν΄λΉ νλλ€μ΄ νμνκΈ° λλ¬Έμ κ·Έλ¬λ κ² κ°λ€.
νμ§λ§ νλκ° λ§€μ° λ§μ Entity μ κ²½μ° λ§€λ² λͺ¨λ μ 보λ€μ΄ νμ μμΌλ―λ‘ Entity μ 체λ₯Ό Controller λ View λ‘ λκΈ°λ κ²μ λ°μ΄ν° μ μ‘μ λ¬΄λ¦¬κ° μμ κ²μ΄λ€.
β‘ λ°μ΄ν° λ³΄νΈ : λ¨μ μ‘°ν보λ€λ μμ , μμ , λ±λ‘ λ±μ μμ΄μ λ°μ΄ν° 보νΈμ μν μ ν μ μμ κ² κ°λ€. Controller μλ View μμ λ°λ‘ λ°μ΄ν°λ₯Ό Entity λ‘ λ겨λ²λ¦¬λ©΄, λ¬Όλ‘ νμΈνλ κ³Όμ μ κ±°μΉ μλ μκ² μ§λ§ κ·Έλλ Entity λ₯Ό μ§μ μ μΌλ‘ 건λ€μ΄λ κ²μ΄λ―λ‘ λ¬Έμ κ° μκΈΈ μ μμ κ² κ°λ€.
μ΄λ κ² λκ°μ§ μ λμ΄λ€. μμΌλ‘ νλ‘μ νΈλ₯Ό νκ²λλ©΄ DTO λ₯Ό νμ©ν΄μ μ’ λ ν¨μ¨μ μΌλ‘ λ°μ΄ν°λ₯Ό λ€λ£° μ μμ κ² κ°λ€.
μΆν ModelMapper λ μ¬μ©ν΄λ³Ό μμ μ΄λ€.