Project/Coffee Board

[Coffee Board] 게시판 구현 1 - @Mapper

한비Skyla 2024. 7. 17. 08:11

📚 문제상황

@Mapper 를 달고,

Mapper 클래스에서 @Mapping을 해 주었으나, 매핑이 되지 않음...

 

 

💡@Mapper

@Mapper 라는 애너테이션을 달고, 추상 메서드를 구현 해 두면 서버를 실행할 때. 

라이브러리(MapStruact)를 통해 구현체를 생성하는데. 

 

responseDto. 타켓 클래스에는 빌더나 생성자가 정의 되어야 한다. 

@NoArgsconstructor, @Builer 생성해 주어야 한다

 

Mapstruct 주의사항

Mapstruct vo, dto, entity 간 변환을 쉽게 할 수 있도록 해준다. 사실 전에는 클래스마다 static of 메소드를 만들어서 변환시 마다 빌더를 직접 코딩했는데 payload 방대할 경우 이게 참 못할 짓이다. 일반

findmypiece.tistory.com

💡Source, Targer

source: 매핑이 될 객체. getter 필요.

target: 매핑 할 객체. 값을 넣는 객체. builder, 생성자 + setter 필요.

 

<우선순위>

1. @Builder 

2. @Default

3. 생성자

4. NoArgsConstructor

 

 

[Java/Spring] (2) MapStruct - Mapping 필드 정의하기

Mapping 필드 정의하기 지난 게시글에서는 MapStruct가 무엇인지, 그리고 Mapper는 어떻게 생성하는지에 대해 알아보았습니다. 이번 게시글에서는 Mapper 내부의 Mapping은 어떻게 해야 하는 지에 대해 작

mein-figur.tistory.com

 

🔎 문제 해결

 

BoardMapper

@Mapping(source = "member.memberId", target = "memberId")
@Mapping(source = "comment.content", target = "boardComment.content")
@Mapping(source = "comment.member.memberId", target = "boardComment.memberId")
BoardResponseDto boardToBoardResponseDto (Board board);

 

 

BoardResposeDto

@NoArgsConstructor
@Setter
@Getter
public class BoardResponseDto {
    private long boardId;
    private long memberId;
    private String title;
    private String content;
    private int likeCount ;
    private int viewCount ;
    private Board.QuestionStatus questionStatus;
    private Board.VisibilityStatus visibilityStatus;
    private LocalDateTime createdAt;
    private LocalDateTime modifiedAt;
    private CommentResponseDto boardComment;

}