Spring Framework 2024. 8. 7. 17:34

[MapStruct] 필드 매핑할 때 다른 Mapper 클래스의 메소드 사용하는 법 - uses, @Named

목차
  1. 상황
  2. 해결법
  3. 출처

상황

UserDto.unlockingConstellation을 매핑할 때 UserConstellationMapper.toDto(UserConstellation)를 사용해서 매핑하려고 한다. 자동으로 생성된 코드에서는 UserDto.unlockingConstellation.constellation까지 .get()하여 의도하지 않은 SELECT가 발생하기 때문이다.

public class UserDto {
...
/**
     * 현재 해금중인 유저 별자리 상태
     */
    private UserConstellationDto unlockingConstellation;
}
@Mapper(componentModel = "spring", uses={UserConstellationMapper.class})
public interface UserMapper {
...
    // ! 이렇게 해도 UserMapperImpl에서 UserConstellationMapper를 참조하지 않는다
    @Mapping(target = "unlockingConstellation", source = "unlockingConstellation")
    UserDto toDto(User user, UserConstellation unlockingConstellation);
}

해결법

  1. UserMapper에 @Mapper(uses={UserConstellationMapper.class})를 추가해서 다른 Mapper 클래스를 불러오게 한다
  2. 불러올 클래스와 메소드에 @Named로 별명을 붙여 준다. 클래스에도 추가해야 한다.
  3. 매핑할 필드에서 qualifiedByName = {"클래스 별명", "메소드 별명"} 을 추가해 준다.

이렇게 하면 자동으로 생성된 UserMapperImpl에서 UserConstellationMapper 멤버 변수를 추가해서 의존성을 주입하고 userConstellationMapper.toDto()를 호출한다.

@Mapper(componentModel = "spring")
@Named("UserConstellationMapper")
public interface UserConstellationMapper {
...
    @Mapping(target = "constellation", ignore = true)
    @Named("toDto(UserConstellation)")
    UserConstellationDto toDto(UserConstellation userConstellation);
 ...
 }

@Mapper(componentModel = "spring", uses={UserConstellationMapper.class})
public interface UserMapper {
...
    @Mapping(target = "unlockingConstellation", source = "unlockingConstellation", qualifiedByName = {"UserConstellationMapper", "toDto(UserConstellation)"})
    UserDto toDto(User user, UserConstellation unlockingConstellation);
}

출처

MapStruct 공식 문서 - 5.9. Mapping method selection based on qualifiers

저작자표시 (새창열림)

'Spring Framework' 카테고리의 다른 글

[Spring][스크랩] Request Param의 타입으로 enum을 지정하는 법  (0) 2024.08.18
[JPA] deleteBy는 N개의 DELETE 쿼리로 실행된다  (0) 2024.08.16
[Spring][스크랩] nested transaction의 동작 방식  (0) 2024.08.02
[Spring] FK(id)만 이용해서 관계 있는 엔티티를 생성하는 법 - 프록시 객체  (0) 2024.07.12
[Spring] DTO의 사용 범위, 목적, Mapper  (0) 2024.07.12
  1. 상황
  2. 해결법
  3. 출처
'Spring Framework' 카테고리의 다른 글
  • [Spring][스크랩] Request Param의 타입으로 enum을 지정하는 법
  • [JPA] deleteBy는 N개의 DELETE 쿼리로 실행된다
  • [Spring][스크랩] nested transaction의 동작 방식
  • [Spring] FK(id)만 이용해서 관계 있는 엔티티를 생성하는 법 - 프록시 객체
개발자 이우진
이우진 기술 블로그
  • All (86)
    • Spring Framework (20)
    • MSA (7)
      • Event Driven Architecture (3)
    • Java (3)
    • Flink (2)
    • Computer Science (9)
      • Object Oriented Programming (3)
    • Problem Solving (15)
    • Design Pattern (0)
    • React (4)
    • Javascript (2)
    • Web (3)
    • Tools & Environment (3)
    • C++ (2)
    • misc (5)
    • Essay (3)
      • 기술 회고 (5)
  • 홈
  • 태그
  • 관리자
  • 글쓰기
hELLO · Designed By 정상우.v4.2.2
[MapStruct] 필드 매핑할 때 다른 Mapper 클래스의 메소드 사용하는 법 - uses, @Named
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.