본문 바로가기

개발

ModelMapper 사용

ModelMapper를 사용하니 Entity-DTO 전환이 한층 수월해졌다. 

 

기존엔 생성자 혹은 builder를 사용하여 Entity-DTO 전환을 하였다. 

public class QnaRequestDto {
    private String title;
    private String content;

    public QnaRequestDto(Qna qna) {
        this.title = qna.getTitle();
        this.content = qna.getContent();
    }

    public static Qna toQna(QnaRequestDto qnaRequestDto) {
        return Qna.builder()
                .title(qnaRequestDto.getTitle())
                .content(qnaRequestDto.getContent())
                .build();

    }

}

 

그러나 ModelMapper를 사용하니, 

 

Qna qnaEntity = modelMapper.map(qnaRequestDto, Qna.class);

이렇게만 하면 Entity-DTO 전환이 된다. 

 

출처: http://modelmapper.org/

 

ModelMapper - Simple, Intelligent, Object Mapping.

Why ModelMapper? The goal of ModelMapper is to make object mapping easy, by automatically determining how one object model maps to another, based on conventions, in the same way that a human would - while providing a simple, refactoring-safe API for handli

modelmapper.org