Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MapStruct #4

Open
hjjae2 opened this issue Aug 7, 2021 · 0 comments
Open

MapStruct #4

hjjae2 opened this issue Aug 7, 2021 · 0 comments

Comments

@hjjae2
Copy link
Owner

hjjae2 commented Aug 7, 2021

MapStruct

https://mapstruct.org/documentation/stable/reference/html/

아래와 같이 사용한다면, lombok 의 annotation 을 사용할 수 있다.

annotationProcessor 'org.projectlombok:lombok'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'

아래와 같이 사용한다면, lombok 의 annotation 을 사용할 수 없다. 따라서 getter, setter 등을 직접 구현해주어야 한다.

annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'
annotationProcessor 'org.projectlombok:lombok'

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2021-08-07T18:03:07+0900",
    comments = "version: 1.4.2.Final, compiler: IncrementalProcessingEnvironment from gradle-language-java-7.1.1.jar, environment: Java 1.8.0_281 (Oracle Corporation)"
)
public class PersonMapperImpl implements PersonMapper {

    @Override
    public PersonRequestDto person2PersonRequestDto(Person person) {
        if ( person == null ) {
            return null;
        }

        PersonRequestDto personRequestDto = new PersonRequestDto();

        personRequestDto.setPhone( personPhoneValue( person ) );

        return personRequestDto;
    }

    @Override
    public PersonResponseDto person2PersonResponseDto(Person person) {
        if ( person == null ) {
            return null;
        }

        PersonResponseDto personResponseDto = new PersonResponseDto();

        personResponseDto.setPhone( personPhoneValue( person ) );

        return personResponseDto;
    }
...

https://mapstruct.org/documentation/stable/reference/html/#mapping-with-constructors

위에 글을 참조하면 알 수 있듯이, '생성자 방식'을 사용한 Mapper 에서는 우선순위가 있다.

  1. @Default
  2. NoArgsConstructor
  3. 그 외 (유효한) public constructor (ex. AllArgsConstructor)
  4. 만약 유효한 public constructor 가 여러 개라면 컴파일오류가 발생할 것이며 @Default 어노테이션을 명시해줘야한다.

[TIP] @NoArgsConstructor 가 사용된다면, setter 메서드가 필요하다. 불필요한 setter 를 만들고 싶지 않아 @AllArgsConstructor 방식을 이용하게 유도하였다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant