๋ชฉ์ฐจ
๋ณดํต ์
ํ์กฐ์ธ์ ์นดํ
๊ณ ๋ฆฌ์ฒ๋ผ 1์ฐจ, 2์ฐจ, 3์ฐจ.... ๋ฌดํ์ ์ผ๋ก ๋์ด๋ ์ ์์ ๋ ์ฌ์ฉ๋๋๋ฐ,
ํ๋์ ํ
์ด๋ธ๋ก ๋ชจ๋ ๊ด๊ณ๋ฅผ ์ ์ํ ์ ์์ด์ ์ ์ฉํ๊ฒ ์ฐ์ธ๋ค.
์
ํ์กฐ์ธ ํํ๋ก ๊ตฌํํ ์นดํ
๊ณ ๋ฆฌ ์ํฐํฐ์ ๋ฉ๋ด ์ํฐํฐ๋ฅผ ๋งตํํ๊ณ Json์ ๋ด๋ ค์ฃผ๋ ๊ณผ์ ์์
์ด๋ป๊ฒ ์ํ์ฐธ์กฐ๋ฅผ ํผํ ์ ์์๊น??
์์๋ฐ์ดํฐ
์นดํ ๊ณ ๋ฆฌ
๋ฉ๋ด
๋ด๊ฐ ์ํ๋ ์๋ต๊ฐ
์นดํ ๊ณ ๋ฆฌ
{
"message": "์นดํ
๊ณ ๋ฆฌ ์ ๋ณด๋ฅผ ์กฐํํ์์ต๋๋ค.",
"data": {
"id": 2,
"categoryCode": "soup_stews",
"categoryName": "์ฐ_ํ_์ฐ๊ฐ",
"parent": {
"id": 1,
"categoryCode": "koreanfood",
"categoryName": "ํ์",
"parent": null,
"childList": null
},
"childList": [
{
"id": 3,
"categoryCode": "stew",
"categoryName": "์ฐ๊ฐ",
"parent": null,
"childList": null
},
{
"id": 4,
"categoryCode": "steamed",
"categoryName": "์ฐ",
"parent": null,
"childList": null
}
]
}
}
- ์ฐ๊ฐ์์๋ parent๋ฅผ ํ๊ณ ๋ค์ด๊ฐ ์ ์์ง๋ง depth๋ฅผ ์ง์ ํ์ฌ ์กฐํํ๊ณ ์ถ๋ค
- parent์ child๊ฐ ํ์ํ ๋๋ง ๊ฐ์ ์กฐํํ๊ณ ์ถ๋ค
๋ฉ๋ด
์นดํ
๊ณ ๋ฆฌ๋ณ๋ก ์กฐํํ ๊ฒฝ์ฐ
{
"message": "๋ฉ๋ด ์ ๋ณด ๋ฆฌ์คํธ๋ฅผ ์กฐํํ์์ต๋๋ค.",
"data": {
"content": [
{
"createdDateTime": "2022-11-09 00:32:19",
"modifiedDateTime": "2022-11-09 00:32:19",
"id": 1,
"menuCode": 111,
"menuName": "๊น์น์ฐ๊ฐ",
"description": "๊น์น์ฐ๊ฐ์
๋๋ค.",
"categoryCode": "stew",
"categoryId": 3,
"categoryName": "์ฐ๊ฐ"
},
{
"createdDateTime": "2022-11-09 00:32:41",
"modifiedDateTime": "2022-11-09 00:32:41",
"id": 2,
"menuCode": 112,
"menuName": "๋์ฅ์ฐ๊ฐ",
"description": "๋์ฅ์ฐ๊ฐ์
๋๋ค.",
"categoryCode": "stew",
"categoryId": 3,
"categoryName": "์ฐ๊ฐ"
}
],
"currentPage": 1,
"totalPages": 1,
"pageSize": 10,
"firstPage": true,
"lastPage": true
}
}
๊ทธ๋ฅ ๋ชจ๋ ๋ฉ๋ด ์กฐํํ ๊ฒฝ์ฐ
{
"message": "๋ฉ๋ด ์ ๋ณด ๋ฆฌ์คํธ๋ฅผ ์กฐํํ์์ต๋๋ค.",
"data": {
"content": [
{
"createdDateTime": "2022-11-09 00:32:19",
"modifiedDateTime": "2022-11-09 00:32:19",
"id": 1,
"menuCode": 111,
"menuName": "๊น์น์ฐ๊ฐ",
"description": "๊น์น์ฐ๊ฐ์
๋๋ค.",
"categoryCode": "stew",
"categoryId": 3,
"categoryName": "์ฐ๊ฐ"
},
{
"createdDateTime": "2022-11-09 00:32:41",
"modifiedDateTime": "2022-11-09 00:32:41",
"id": 2,
"menuCode": 112,
"menuName": "๋์ฅ์ฐ๊ฐ",
"description": "๋์ฅ์ฐ๊ฐ์
๋๋ค.",
"categoryCode": "stew",
"categoryId": 3,
"categoryName": "์ฐ๊ฐ"
},
{
"createdDateTime": "2022-11-09 00:33:01",
"modifiedDateTime": "2022-11-09 00:33:01",
"id": 3,
"menuCode": 113,
"menuName": "์์น๊ตญ์",
"description": "์์น๊ตญ์์
๋๋ค.",
"categoryCode": "noodle",
"categoryId": 5,
"categoryName": "๊ตญ์"
},
{
"createdDateTime": "2022-11-09 00:33:10",
"modifiedDateTime": "2022-11-09 00:33:10",
"id": 4,
"menuCode": 114,
"menuName": "์นผ๊ตญ์",
"description": "์นผ๊ตญ์์
๋๋ค.",
"categoryCode": "noodle",
"categoryId": 5,
"categoryName": "๊ตญ์"
},
{
"createdDateTime": "2022-11-09 00:34:02",
"modifiedDateTime": "2022-11-09 00:34:02",
"id": 5,
"menuCode": 115,
"menuName": "์๊ตฌ์ฐ",
"description": "์๊ตฌ์ฐ์
๋๋ค.",
"categoryCode": "steamed",
"categoryId": 4,
"categoryName": "์ฐ"
},
{
"createdDateTime": "2022-11-09 00:34:12",
"modifiedDateTime": "2022-11-09 00:34:12",
"id": 6,
"menuCode": 116,
"menuName": "์ฐ๋ญ",
"description": "์ฐ๋ญ์
๋๋ค.",
"categoryCode": "steamed",
"categoryId": 4,
"categoryName": "์ฐ"
}
],
"currentPage": 1,
"totalPages": 1,
"pageSize": 10,
"firstPage": true,
"lastPage": true
}
}
- ์นดํ
๊ณ ๋ฆฌ๋ณ๋ก ์กฐํํ ์๋ ์์ด์ผํ์ง๋ง ๊ทธ๋ฅ ์ ์ฒด๋ฅผ ์กฐํํ ์๋ ์์ด์ผํ๋ค.
- ๊ทธ๋ฌ๊ธฐ ์ํด์ ๊ฐ์ฒด๋ง๋ค ์นดํ
๊ณ ๋ฆฌ ์ ๋ณด๊ฐ ์์ด์ผํ๋ค.
๋ด๊ฐ ์๊ฐํ ์์ด๋์ด ์ฒซ๋ฒ์งธ : ์นดํ ๊ณ ๋ฆฌ ์ํ์ฐธ์กฐ ๋ฐฉ์ง๋ DTO๋ก
public class CategoryDto {
.....์ค๋ต
/**
* ์ํ์ฐธ์กฐ๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด ๋ฐ๋ก ์ ์ํ๋ค
*/
@Data
private static class JsonRes {
private Long id;
private String categoryCode;
private String categoryName;
private JsonRes parent; //JsonRes์ด๋ค. ์ฃผ์!
private List<JsonRes> childList; //JsonRes์ด๋ค. ์ฃผ์!
public JsonRes(Category category, boolean getParent, boolean getChild, Integer depth) {
this.id = category.getId();
this.categoryCode = category.getCategoryCode();
this.categoryName = category.getCategoryName();
if(depth > 0){ //depth๊ฐ 0์ด ๋ ๋๊น์ง๋ง ์กฐํํ๋ค.
if(getParent){ //getParent๊ฐ true์ผ๋๋ง ๋ถ๋ชจ๋ฅผ ์กฐํํ๋ค
Category parent = category.getParentCategory();
if(parent != null){ //๋ถ๋ชจ๋ฅผ ์กฐํํ ๋, ๋ถ๋ชจ๊ฐ ์์๋๋ง JSON์ ์์ฑํ๋ค.
this.parent = new JsonRes(parent, getParent, false, depth-1);
//child๋ ์๋์์ ๋ฐ๋ก ๊ตฌํ ๊ฑฐ๋๊น false๋ก ๊ณ ์ ํ๋ค.
}
}
if(getChild){ //getChild๊ฐ true์ผ๋๋ง ๋ถ๋ชจ๋ฅผ ์กฐํํ๋ค
List<Category> childList = category.getChildCategoryList();
if(childList != null){ //์์์ ์กฐํํ ๋, ์์์ด ์์๋๋ง JSON์ ์์ฑํ๋ค.
this.childList = childList.stream()
.map(child -> new JsonRes(child, getParent, true, depth-1))
.collect(Collectors.toList());
//์์ ์ ๋ถ ๊ตฌํ ๊ฑฐ๋๊น true๋ก ๊ณ ์ ํ๋ค.
}
}
}
}
}
.....์ค๋ต
}
๋ด๊ฐ ์๊ฐํ ์์ด๋์ด ๋๋ฒ์งธ : ๋ฉ๋ด-์นดํ ๊ณ ๋ฆฌ ๋งตํ ์ ์นดํ ๊ณ ๋ฆฌ ๋ด์ฉ์ getter๋ก
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "menu")
@DynamicUpdate
public class Menu extends BaseTimeEntity {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", columnDefinition = "bigint comment '์์ด๋'")
private Long id;
@Column(name = "menu_code", columnDefinition = "bigint comment '๋ฉ๋ด์ฝ๋'")
private Long menuCode;
@Column(name = "menu_name", columnDefinition = "varchar(255) comment '๋ฉ๋ด๋ช
'")
private String menuName;
@Column(name = "description", columnDefinition = "text comment '๋ฉ๋ด์ค๋ช
'")
private String description;
@JsonIgnore
@Getter(AccessLevel.PRIVATE) //category ์์ฒด getter ์ ๊ทผ ๋ง๋๋ค
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", columnDefinition = "bigint comment '์นดํ
๊ณ ๋ฆฌ ์์ด๋'")
private Category category;
@Column(name = "category_code", columnDefinition = "varchar(255) comment '์นดํ
๊ณ ๋ฆฌ ์ฝ๋'")
private String categoryCode;
@Transient //DB์ ์ ์ฅํ์ง ์๊ณ ์ฌ์ฉํ๋ ์ปฌ๋ผ
private Long categoryId;
@Transient //DB์ ์ ์ฅํ์ง ์๊ณ ์ฌ์ฉํ๋ ์ปฌ๋ผ
private String categoryName;
@Builder
public Menu(Long id, Long menuCode, String menuName, String description, Category category) {
this.id = id;
this.menuCode = menuCode;
this.menuName = menuName;
this.description = description;
this.category = category;
this.categoryId = category.getId();
this.categoryCode = category.getCategoryCode();
this.categoryName = category.getCategoryName();
}
public String getCategoryCode() {
return this.category.getCategoryCode(); //getter ์ ์
}
public Long getCategoryId() {
return this.category.getId(); //getter ์ ์
}
public String getCategoryName() {
return this.category.getCategoryName(); //getter ์ ์
}
}
์ ์ฒด ์ฝ๋
ํ ์คํธ
์นดํ ๊ณ ๋ฆฌ ์กฐํ depth ํ์ธ
์นดํ
๊ณ ๋ฆฌ getChild, getParent ๋์ ํ์ธ
๋ฉ๋ด ์นดํ
๊ณ ๋ฆฌ๋ณ ์กฐํ / ์ผ๋ฐ์กฐํ
์ด๊ฒ์ ๊ฒ ํด๋ณด๋ค๊ฐ ์ด๋ฐ ๋ฐฉ๋ฒ๋ ์๊ฒ ๊ตฌ๋~~ ํด์ ์ ๋ฆฌํด๋ดค๋ค!