16๋ฒ์งธ ๊ณผ์
[Spring] Entity, DTO, VO ์ ๊ฐ๋ ๊ณผ ํน์ง
12๋ฒ์งธ ๊ณผ์ Entity - ์ค์ DB์ ํ ์ด๋ธ๊ณผ ๋งคํ๋๋ ๊ฐ์ฒด - id๋ฅผ ํตํด ๊ฐ๊ฐ์ Entity๋ฅผ ๊ตฌ๋ถํ๋ค - ๋ก์ง์ ๊ฐ์ง ์ ์๋ค import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.GeneratedValue; impo
sooz.tistory.com
์ด์ ์ Entity์ DTO, VO์ ๊ฐ๋ ๊ณผ ํน์ง์ ์์๋ณด์๋ค.
๋ฐฐ๋ฌ API ๊ตฌํ ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉด์ ์ฐ์ ์ ์ผ๋ก Food Entity ๋ฅผ ๊ตฌํํ์๊ณ ์ด๋ฅผ DTO๋ก ๋ณํํ๋ ค๊ณ ํ๋ค.
๊ทธ๋ฆฌ๊ณ Entity ์ DTO ๋ ๋ถ๋ฆฌํ์ฌ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค๊ณ ํ๋ค. ์ ์ธ์ง๋ ์๋์์ ํ์ธํด๋ณด์!
0. Entity๋ฅผ DTO๋ก ๋ณํํ๋ ์ด์
์ฐ์ Entity๋ ์ ๊ธ์์ ํ์ธํ ์ ์๋ฏ์ด ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ ์ด๋ธ๊ณผ ๋งคํ๋๋ ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ ์ฌ์ฉ์์๊ฒ ๋ ธ์ถํ๋ฉด ์ํ์ง ์์ ์ํฉ์์ ์์์ ์์ฑ์ด ๋ณ๊ฒฝ๋ ๊ฐ๋ฅ์ฑ์ด ์๊ณ ๋ณด์์์ ๋ฌธ์ ๋ํ ๋ฐ์ํ ์ ์๋ค. ๋ํ ์ํฐํฐ์์ ์์ ์ ํ๋ฉด ์ ๋ฌ๋ฐ๋ ๋ทฐ์์๋ ์์ ์ ํด์ผํ๋ฏ๋ก ๊ฒฐํฉ๋๊ฐ ๋์ ์ ์ง๋ณด์์ ์ฉ์ดํ์ง ์๋ค.
DTO๋ ๊ณ์ธต๊ฐ์ ๋ฐ์ดํฐ ๊ตํ์ ์ํด ์ฌ์ฉํ๋ ๊ฐ์ฒด์ด๋ค. DTO๋ฅผ ์ฌ์ฉํ๋ฉด ํ์ํ ๋ฐ์ดํฐ๋ฅผ ์ ํ์ ์ผ๋ก ๋ณด๋ผ ์ ์๋ค. ๋ฐ๋ผ์ DTO์์ Entity์ ํ์ํ ๋ฐ์ดํฐ๋ง ์ ํ์ ์ผ๋ก ๋ด์ ์ฌ์ฉํ๋ฉด Entity ํด๋์ค๋ฅผ ๋ณดํธํ ์ ์๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์์ Entity์์ ๋ฐ์ํ๋ ๋ฌธ์ ์ ์ ํด๊ฒฐํ ์ ์๋ค.
๊ฒฐ๋ก ์ ์ผ๋ก DTO๋ก ๋ณํํ๋ ์ด์ ๋ Entity ๋ณดํธ์ ์ฝ๋ ์ฌ์ฌ์ฉ์ฑ, ์ ์ง๋ณด์ ์ฉ์ด ๋ฑ์ ์ฅ์ ๋๋ฌธ์ด๋ผ๊ณ ํ ์ ์๋ค.
1. Controller์์ ๋ฐ์ DTO๋ฅผ ์ ๋ฌํ์ฌ Service์์ Entity๋ก ๋ณํํ๊ธฐ
๊ตฌ๊ธ๋ง์ ํตํด ์์๋ณธ ๊ฒฐ๊ณผ, ์ํฉ์ ๋ฐ๋ผ ๊ฐ์ธ์ ๋ฐ๋ผ ๋ค์ํ ๋ฐฉ๋ฒ์ผ๋ก ๊ฐ ๊ณ์ธต์์ ๊ตฌํํ๋ ๊ฒ์ ์๊ฒ ๋์๋ค. ๋์ฑ ํท๊ฐ๋ฆฌ๊ฒ (?) ๋์๋ค. ใ ใ ใ ใ ๋ฐ๋ผ์ ๋๋ ์ง๊ธ ์งํํ๋ ํ๋ก์ ํธ์ ๋ง๋ ๋ฐฉ๋ฒ์ ์ ํํด์ ์ฌ์ฉํด๋ณด๊ณ ์ถํ์ ๊ท๋ชจ๊ฐ ์ปค์ง๊ฑฐ๋ ๋ฌธ์ ์ ์ด๋ ๋ถํธํจ์ด ์๊ธฐ๋ฉด ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ์ฉํด๋ณด๋ ๊ฒ๋ ์ข์ ๊ฒ ๊ฐ์์ ์ผ๋จ Controller์์ Service๋ก DTO๋ฅผ ์ ๋ฌํ๊ณ Service์์ Entity๋ก ๋ณํํ๋ ๋ฐฉ๋ฒ์ ์ ํํ์๋ค.
์ด ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ฉด Controller์์๋ Entity๋ฅผ ์์ง ๋ชปํ๊ณ DTO๋ง ์ ์ ์๋ค. Controller์์ Service๋ก DTO๋ฅผ ์ ๋ฌํ๋ฏ๋ก ๋ ๊ณ์ธต์ ๊ฒฐํฉ๋๊ฐ ๋์์ง๋ ๋จ์ ์ด ์๊ธด๋ค.
2. DTO ↔ Entity ๋ณํ ๋ฐฉ๋ฒ
1) Model Mapper
- Mapper ํด๋์ค๋ฅผ ๋ฐ๋ก ์์ฑํ์ฌ ๋ถ๋ฆฌํด์ฃผ๋ ๋ฐฉ๋ฒ์ด๋ค. Mapper์์๋ DTO์ Entity๋ฅผ ๋ณํํด์ฃผ๋ ์์ ์ ํด์ค๋ค. ๋ณํ ์์ ์ Mapper์ ์์ํ๋ฉด์ Controller์์ ๋ฒ์ด๋ ์ญํ ๋ถ๋ฆฌ๊ฐ ๋๋ค.
2) Map Struct
- ๋๋ฉ์ธ ๊ธฐ๋ฅ์ด ๋์ด๋๋ฉด Mapper ๋ ๋ง์์ง๋ฏ๋ก ์ฑ๋ฅ์ด ์ ํ๋๋ค๋ ๋จ์ ์ด ์๋ค. Map Struct๋ ์ด๋ฌํ ๋จ์ ์ ๋ณด์ํ์ฌ Mapper ๊ตฌํ ํด๋์ค๋ฅผ ์๋ ์์ฑํด์ฃผ์ด ํจ์จ์ฑ์ ๋์ฌ์ค๋ค.
3) ์๋์ผ๋ก ์ง์ ๋ณํ
- 3๊ณ์ธต ์ค ํ ๊ณ์ธต์์ ์ง์ ์ฝ๋๋ฅผ ์์ฑํ์ฌ ์๋์ผ๋ก ๋ณํํ๋ ๋ฐฉ๋ฒ์ด๋ค. ์ด๋ฒ ํ๋ก์ ํธ์์๋ Controller์ Service๋ฅผ ํตํด ์ง์ ๋ณํํ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ ๊ฒ์ด๋ค.
3. @Builder ์ฌ์ฉ
@Builder๋ฅผ ์ฌ์ฉํ๋ ์ด์ ๋
- ํ์ํ ๋ฐ์ดํฐ๋ง ์ค์ ๊ฐ๋ฅํ๋ค
- ์ ์ฐ์ฑ์ ํ๋ณดํ ์ ์๋ค
- ๊ฐ๋ ์ฑ์ ๋์ธ๋ค
- ๋ถ๋ณ์ฑ์ ํ๋ณดํ ์ ์๋ค
์ด๋ฌํ ์ด์ ๋ค์ด ์๋ค. ๋ํ @Builder๋ฅผ ์ฌ์ฉํ๊ณ @Setter๋ฅผ ์ฌ์ฉํ์ง ์๋ ์ด์ ๊ฐ ์กด์ฌํ๋ค.
@Setter๋ฅผ ์ฌ์ฉํ๋ฉด ์์ , ์์ฑ์ ๊ตฌ๋ณ์ด ํ๋ค์ด ์ฝ๋์ ๊ฐ๋ ์ฑ ๋ฌธ์ ๊ฐ ์๊ธด๋ค. ๊ทธ๋ฆฌ๊ณ ์์ ์ ๊ฐ๋ฐฉ์ ์ด๋ฏ๋ก ๊ฐ์ฒด์งํฅ์๋ฆฌ์ธ ํ์ฅ์ ์ด๋ ค์๊ณ ์์ ์ ๋ซํ์์ด์ผํ๋ค๋ OCP(Open Closed Principle) ์๋ฆฌ๋ฅผ ์๋ฐํ๋ค. ์ด ๋๋ฌธ์ @Builder๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๋ฐํด๋ณด์๋ค.
1) Food Entity ์ FoodDTO ์ @Builder๋ฅผ ์ ์ธํ๊ณ FoodDTO์ builder๋ฅผ ์ฌ์ฉํ์ฌ toDto, toEntity ๋ฉ์๋๋ฅผ ์์ฑํ์๋ค.
package com.example.project.food;
import lombok.*;
@Getter
public class FoodDto {
private int id;
private String name;
private int price;
private String content;
private String type;
public FoodDto(){
}
@Builder
public FoodDto(int id, String name, int price, String content, String type) {
this.id = id;
this.name = name;
this.price = price;
this.content = content;
this.type = type;
}
public Food toEntity() {
return Food.builder()
.id(id)
.name(name)
.price(price)
.content(content)
.type(type)
.build();
}
public FoodDto toDto(Food food) {
return FoodDto.builder()
.id(food.getId())
.name(food.getName())
.price(food.getPrice())
.content(food.getContent())
.type(food.getType())
.build();
}
}
2) Controller์์๋ ์๋ Food๋ก ์ ๋ฌํ๋ ๊ฒ์ FoodDto๋ฅผ ๋ฐ์์ Service๋ก ์ ๋ฌํ๋ค. DTO๋ง ์ฌ์ฉํ๋ฏ๋ก Controller์์๋ Entity๋ฅผ ํ์ธํ ์ ์๋ค.
//์์ ๋ฑ๋ก
@PostMapping("/foods")
public void insertFood(@Validated @RequestBody FoodDto foodDto) {
foodService.insertFood(foodDto);
}
3) Service์์๋ Controller์์ ์ ๋ฌ๋ฐ์ DTO๋ฅผ toEntity ๋ฉ์๋๋ฅผ ํตํด Entity๋ก ๋ณํํ์๋ค.
public void insertFood(FoodDto foodDto) {
foodMapRepository.insertFood(foodDto.toEntity());
}
lombok ์ถ๊ฐํ๊ธฐ
lombok ์ถ๊ฐํจ : build.gradle์ dependencies์ ์ฝ๋ ์ถ๊ฐ
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
์ฐธ๊ณ
[Java] DAO, DTO, VO, Entity ๋?
์น ๊ฐ๋ฐ์ ํ๋ค ๋ณด๋ฉด DAO, DTO, VO, Entity๋ผ๋ ๋จ์ด๋ค์ ๋ง์ด ์ ํ๊ฒ ๋๋ค. ํด๋น ๊ฐ๋ ์ ์ ์ก์๋์ผ ํ์ ์์ ๋ฌธ์ ๊ฐ ์์ ๊ฒ ๊ฐ๋ค. DAO - DAO๋ Data Access Object ์ ์ฝ์์ด๋ฉฐ ์ด๋ฆ ๊ทธ๋๋ก DB์ data์ ์ ๊ทผ
lucas-owner.tistory.com
[JPA] entity < -- > DTO ๋ณํ ์ ์ค๋ฌด์๋ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ๊น?
โถ DTO(Data Transfer Object)๋? DTO(Data Transfer Object, ๋ฐ์ดํฐ ์ ์ก ๊ฐ์ฒด)๋ ํ๋ก์ธ์ค ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ๊ฐ์ฒด์ด๋ค. ์๊ฒฉ ์ธํฐํ์ด์ค๋ก ์์ ์ ํ ๋, ํธ์ถ์ ๋ฐ๋ฅธ ๋น์ฉ์ด ๋น์ธ๊ธฐ ๋๋ฌธ์ ์์ฒญ์ ํ์
jjunn93.com
DTO๋ ๋์ฒด ์ด๋์ ๋ณํํ๋ ๊ฒ์ด ์ข์๊น?
1. DTO๋ฅผ ์ ์ฌ์ฉํ๋์ง? DTO(Data Transfer Object)๋ ๋ง ๊ทธ๋๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๋ ๊ฐ์ฒด์ด๋ค. ๋ฒจ๋ฉ์์๋ "They are flat data structures that contain no business logic." ์ด๋ผ๊ณ ์ ์ํด๋๋ค. 1. @RequestParam์ผ๋ก ๋ฐ์ดํฐ๋ฅผ
sedangdang.tistory.com
์์ฒญ๊ณผ ์๋ต์ผ๋ก ์ํฐํฐ(Entity) ๋์ DTO๋ฅผ ์ฌ์ฉํ์
…
tecoble.techcourse.co.kr
[Spirng/JPA] Dto์ Entity๋ฅผ ๋ถ๋ฆฌํด์ ์ฌ์ฉํ๋ ์ด์
ํ๋ก์ ํธ๋ฅผ ์งํํ๊ฑฐ๋ ๊ฐ์, ์ฑ ์ ๋ณด๋ฉด ํญ์ ์ํฐํฐ๋ฅผ ์ง์ ๋ฐํํ์ง๋ง๊ณ DTO๋ก ๋ณํํ์ฌ ๋ฐํํ๋ผ๋ ๋ง์ ์ ํ๊ฑฐ๋ ๋ณด์์ ๊ฒ์ด๋ค. ํ์ง๋ง ๋จ์ํ "์ ~ ๊ทธ๋ ๊ฒ ํ๋ผ๋๊น ๊ทธ๋ ๊ฒ ํด์ผ์ง" ๋ณด๋ค
hstory0208.tistory.com