π― Goals
- Entity λ μ¬μ€μ Getter Setter λ° ν΅μ¬ λΉμ¦λμ€ λ©μλλ§ μμ΄μΌνλ€.
νλ©΄ λμμΈκ³Ό κ΄λ ¨λ λ‘μ§μ μΆκ°ν΄μ μλλ€. - API μ€κ³μ entity κ°μ²΄λ₯Ό λ°ννμ§ μμμΌνλ€.
λΌλ μ λ λͺ μ λ₯Ό μ΄ν΄νλ€.
μλͺ»λ μμ
Member.java
@Entity
@Getter @Setter
public class Member {
@Id @GeneratedValue
@Column(name = "member_id")
private Long id;
private String userName;
@Embedded
private Address address;
// μ°κ΄ κ΄κ³μ μ£ΌμΈμ΄ μλ κ²½μ° μ½κΈ° μ μ©μ΄ λ¨.
// `member` field in order μ μν΄ λ§€νλμλ€λ κ²μ λͺ
μ.
@OneToMany(mappedBy = "member")
private List<Order> orders = new ArrayList();
}
μ΄ μ½λμ²λΌ λ³λμ νλ©΄μ 건λ리λ λ‘μ§μ Entity κ° λ΄μμ μλλ€.
Entity λ΄μ View λ¨μ μμ‘΄μ μΈ μ½λκ° μ λ μ겨μ μλλ€.
MemberController.java
@Controller
@RequiredArgsConstructor
public class MemberController {
private final MemberService memberService;
@GetMapping("/members")
public String list(Model model) {
// λͺ¨λ μ¬μ©μλ₯Ό μ°Ύμ
List<Member> members = memberService.findMembers();
model.addAttribute("members", members);
// memberList.html μ members κ°μ²΄ μ λ¬.
return "members/memberList";
}
}
memberList.html
thymeleaf μ μ°λλ SSR (Server Side Rendering) νμ΄μ§ μ€ μΌλΆ μ½λλ€.
<tbody>
<tr th:each="member : ${members}">
<td th:text="${member.id}"></td>
<td th:text="${member.userName}"></td>
<td th:text="${member.address?.city}"></td>
<td th:text="${member.address?.street}"></td>
<td th:text="${member.address?.zipcode}"></td>
</tr>
`member` entity κ°μ²΄λ₯Ό μλ²μ Controller λ‘ λΆν° λ°μμ€κ³ μλ€.
μλͺ»λ μ€κ³λ€.
entity κ°μ²΄λ₯Ό μ λ API μμ λ°νν΄μ μλλ€.
APIλ μ€ν©, λ¬Έμκ° μ‘΄μ¬νλ€. API μμμ entity λ₯Ό κ·Έλλ‘ λ°ννκ² λλ©΄
entity μ attribute κ° μΆκ°λκ±°λ μμ λ λλ§λ€ API μ€ν©κ³Ό λ¬Έμ λͺ¨λ μ λ°μ΄νΈκ° μΌμ΄λμΌνλ€.
λ°λΌμ Controller -> View λ¨μΌλ‘ λκ²¨μ€ λ°μ΄ν°λ λ³λμ DTO(Data Trasnfer Object) λ₯Ό μ μνμ¬ νμν λ°μ΄ν°λ§ λ΄μ 보λ΄λ κ²μ΄ μ’λ€.
'JVM > JPA' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[JPA] μμκ΄κ³ 맀ν (0) | 2023.01.13 |
---|---|
[JPA] 1:1 μ°κ΄κ΄κ³ μ€μ (0) | 2023.01.12 |
[JPA] auto_increment μ λ΅ IDENTITY vs SEQUENCE (0) | 2023.01.09 |
[JPA] JPA λ μ λ±μ₯νμκΉ? (0) | 2023.01.07 |