https://dojjong.tistory.com/10
부모-자식관계 트리구조 만들기
부서를 구현하고 싶을때, 3단계 까지 들어가는 부서가 있다고 치면 예를 들어 ○○실-□□부-△△팀 의 형식이라 해당하는 부서를 컬럼으로 따로 구분했었습니다. 기존에 이미 서비스중인 시스
dojjong.tistory.com
데이터는 플랫하게 가져와서
converter내에서 처리
Map<Long, CategoryTreeDto> dtoMap = new HashMap<>();
List<CategoryTreeDto> roots = new ArrayList<>();
for (Category c : categories) {
dtoMap.put(c.getId(), new CategoryTreeDto(c.getId(), c.getName()));
}
for (Category c : categories) {
CategoryTreeDto current = dtoMap.get(c.getId());
if (c.getParent() != null) {
CategoryTreeDto parent = dtoMap.get(c.getParent().getId());
parent.getChildren().add(current);
} else {
roots.add(current);
}
}
'Spring > 서비스 계층' 카테고리의 다른 글
[Spring] DTO (0) | 2024.06.15 |
---|