본문 바로가기
IT

MapStruct 설정

by 허브큐 2021. 6. 22.

Gradle 설정

  • lombok 과 함께 사용할 경우 annotationProcessor에 lombok 설정을 추가해 주어야 한다
ext {
    mapstructVersion = "1.4.2.Final"
    lombokVersion = "1.18.20"
    lombokMapstructBindingVersion = "0.2.0"
}

dependencies {
    implementation "org.mapstruct:mapstruct:${mapstructVersion}", "org.projectlombok:lombok:${lombokVersion}"
    annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}", "org.projectlombok:lombok:${lombokVersion}", "org.projectlombok:lombok-mapstruct-binding:${lombokMapstructBindingVersion}"
}

Mapper interface

  • Interface를 통해 소스코드가 생성된다
  • Source
// source
@Data
public class Source {

    private String test;

}

// target source
@Getter
@Setter
public class Target {

    private Long testing;

}

// mapper source
@Mapper
public interface SourceTargetMapper {

    SourceTargetMapper MAPPER = Mappers.getMapper( SourceTargetMapper.class );

    @Mapping( source = "test", target = "testing" )
    Target toTarget( Source s );
}

Mapper 사용하기

public class Main {

    public static void main( String[] args ) {
        Source s = new Source();
        s.setTest( "5" );

        Target t = SourceTargetMapper.MAPPER.toTarget( s );
        System.out.println( t.getTesting() );
    }
}

Reference

 

MapStruct 1.4.2.Final Reference Guide

If set to true, MapStruct in which MapStruct logs its major decisions. Note, at the moment of writing in Maven, also showWarnings needs to be added due to a problem in the maven-compiler-plugin configuration.

mapstruct.org

 

'IT' 카테고리의 다른 글

mongodb collection  (0) 2021.09.18
mongo crud 연산 명령어  (0) 2021.09.17
Installing mongodb with Homebrew  (0) 2021.09.10

댓글