자동으로 mapping하는 경우
수동으로 mapping 하는 방법
자동으로 mapping되는 경우
@Mapper(componentModel = "spring")
public interface MemberMapper {
// displayName, image, location, memberTitle, aboutMe가 자동으로 mapping 된다.
Member memberPatchDtoToMember(MemberDto.Patch patch);
...
}
@Getter
public class MemberDto {
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public static class Patch{
private String displayName;
private String image;
private String location;
private String memberTitle;
private String aboutMe;
}
}
@NoArgsConstructor
@Getter
@Setter
@Entity
public class Member extends Auditable {
private long memberId;
private String email;
private String password;
private String displayName;
private String image;
private String location;
private String memberTitle;
private String aboutMe;
...
}
@Mapper(componentModel = "spring")
public interface QuestionMapper {
// Member의 field 중 하나인 memberId를 mapping함
@Mapping(target = "member.memberId", source = "memberId")
Question questionPostDtoToQuestion(QuestionDto.Post post);
}
@NoArgsConstructor
@Getter
@Setter
@Entity
public class Question extends Auditable {
private long questionId;
private String title;
private String content;
private long view = 0;
private long vote = 0;
private Member member;
}
@Getter
public class QuestionDto {
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public static class Post{
private String title;
private String content;
private long memberId;
}
}
@Mapping에서 custom 값을 이용하는 경우
@Mapper(componentModel = "spring")
public interface QuestionMapper {
// QuestionDto.Response의 answerCount값을 question.getAnswers().size()의 결과값이 들어간다.
// QuestionDto.Response의 bestAnswerId 값을
// question.getBestAnswer() != null ? question.getBestAnswer().getAnswerId() : null의 결과값이 들어간다.
@Mapping(target = "answerCount", expression = "java(question.getAnswers().size())")
@Mapping(target = "bestAnswerId",
expression = "java(question.getBestAnswer() != null ? question.getBestAnswer().getAnswerId() : null)")
QuestionDto.Response questionToQuestionResponseDto(Question question);
}
@Mapper(uses={}) 사용 예시
@Mapper(componentModel = "spring", uses = {MemberMapper.class, AnswerMapper.class})
public interface FollowAnswerMapper {
// MemberMapper에 Member를 MemberDto.SubResponse으로 mapping하는 method가 구현되어 있음
// AnswerMApper에 Answer를 AnswerDto.Response으로 mapping하는 method가 구현되어 있음
FollowAnswerDto.PostResponse followAnswerToFollowAnswerPostResponseDto(FollowAnswer followAnswer);
}
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class FollowAnswer {
private long faId;
private Answer answer;
private Member member;
private LocalDateTime createdAt;
}
public class FollowAnswerDto {
@AllArgsConstructor
@Getter
@Setter
public static class PostResponse {
private MemberDto.SubResponse member;
private AnswerDto.Response answer;
}
}
@Mapper(uses={})를 사용한 Class에 test code 작성
@SpringBootTest(classes = {QuestionMapperImpl.class, AnswerMapperImpl.class,
MemberMapperImpl.class, FollowAnswerMapperImpl.class})
public class FollowAnswerMapperTest {
@Autowired
private FollowAnswerMapper mapper;
private static final FollowAnswer MOCK_ENTITY = FollowAnswerStub.getEntity;
@Test
void followAnswerToFollowAnswerPostResponseDtoTest() {
FollowAnswerDto.PostResponse expected
= new FollowAnswerDto.PostResponse(MemberStub.getSubResponse(), AnswerStub.getResponse());
FollowAnswerDto.PostResponse result
= mapper.followAnswerToFollowAnswerPostResponseDto(MOCK_ENTITY);
assertThat(result).usingRecursiveComparison().isEqualTo(expected);
}