Memo/짧은 메모
[Spring] Mapstruct, Mapper
생각없이 해도 생각보다 좋다.
2022. 11. 29. 18:42
MapStruct
>dependencies
implementation 'org.mapstruct:mapstruct:1.4.2.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'
>Mapper
: 필요할 때마다 Entity를 Dto로, Dto를 Entity로 변경할 수 있게 구현해둔 클래스
>@Mapper(componenetModel = "spring")
: MapStruct를 사용하기위한 애너테이션
: Spring Bean으로 등록해주는 속성값.
: Mapper 역할을 하는 인터페이스에 붙인다.
>Mapper 인터페이스를 구현하는 클래스(~~Impl)
: MapStruct가 애플리케이션 실행(혹은 build task)시 자동으로 생성한다.
: build 디렉토리에서 mapper 디렉토리를 찾으면 확인할 수 있다.
>MapperImpl
: MapStruct가 자동으로 생성해준 Mapper클래스 명
: Mapper로 변경되는 클래스는 필수적으로 getter가 있어야 한다.
: Mapper로 변경된 클래스는 필수적으로 setter가 있어야 한다.
: MapperImpl 내부 코드를 보면 getter 를 이용해 값을 불러오고, setter를 이용해서 값을 할당시킨다.