도메인 객체와 DTO 간의 변환을 담당하는 객체
DTO끼리의 변환 또한 담당할 수 있음
메서드명 명명규칙을 잘 지켜서 인터페이스만 작성하면 별도의 로직 작성 없이도 MapStruct가 자동으로 구현체 로직을 작성해줌
build.gradle에 의존성 추가
dependencies {
implementation 'org.mapstruct:mapstruct:1.5.5.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'
}
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface PostMapper {
PostResponseDto postToPostResponseDto(Post post);
// 나머지 로직 구현...
}
unmappedTargetpolicy = IGNORE는 객체 변환 시 반환될 객체에 있는 필드 중에서
소스 객체에 해당하는 필드가 없더라도 경고를 띄우지 말고 넘어갈 것을 명시하는 속성
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE,
uses = {CommentMapper.class, ExcuseMapper.class, MemberMapper.class})
public interface PostMapper {
@Mapping(target = "postId", source = "id")
PostSummaryResponseDto postTomultiPostSummaryResponseDto(Post post);
// 나머지 로직 구현...
}