티스토리 뷰
-
Dependency
-
기능
- Entity와 DTO, VO의 객체를 Convert 해주는 Mapper를 생성한다.
-
설정
- build.gradle plugins { ... id 'net.ltgt.apt' version '0.20' } allprojects { ... apply plugin: 'net.ltgt.apt-idea' apply plugin: 'net.ltgt.apt-eclipse' } subprojects { ... dependencies { ... implementation ( ... "org.mapstruct:mapstruct:${mapstructVersion}" ) compileOnly ( ... "org.projectlombok:lombok:${lombokVersion}" ) annotationProcessor( ... "org.projectlombok:lombok:${lombokVersion}", "org.mapstruct:mapstruct-processor:${mapstructVersion}" ) } ... compileJava { options.compilerArgs = [ '-Amapstruct.suppressGeneratorTimestamp=true', '-Amapstruct.suppressGeneratorVersionInfoComment=true' ] } } - lombok.config: mapStruct에서 lombok의 builder를 사용하기 위해 project root에 추가. lombok.addLombokGeneratedAnnotation = true lombok.anyConstructor.addConstructorProperties = true
-
Entity & DTO
- MapStruct는 기본적으로 setter를 사용하여 값을 추가한다. 하지만 immutable이 아니기 때문에 lombok의 Builder를 사용했다. @Getter @Builder @ToString public class Address { private String address1; private String address2; private String address3; } @Getter @Builder @ToString public class Member { private Long id; private String name; private Integer age; private Address address; private String phoneNumber; private String email; } @Getter @Builder @ToString public class MemberDto { private String memberName; private Integer age; private Address address; private String phoneNumber; private String email; }
-
Mapper Interface & generated impl
- Mapper Interface @Mapper public interface MemberMapper { MemberMapper INSTANCE = Mappers.getMapper(MemberMapper.class); @Mapping(source = "member.name", target = "memberName") MemberDto memberToMemberDto(Member member); @Mapping(source = "memberDto.memberName", target = "name") Member memberToMemberDto(MemberDto memberDto, Long id); } - Generated Impl @Generated( value = "org.mapstruct.ap.MappingProcessor" ) public class MemberMapperImpl implements MemberMapper { @Override public MemberDto memberToMemberDto(Member member) { if ( member == null ) { return null; } MemberDtoBuilder memberDto = MemberDto.builder(); memberDto.name( member.getName() ); memberDto.age( member.getAge() ); memberDto.address( member.getAddress() ); memberDto.phoneNumber( member.getPhoneNumber() ); memberDto.email( member.getEmail() ); return memberDto.build(); } @Override public Member memberToMemberDto(MemberDto memberDto, Long id) { if ( memberDto == null && id == null ) { return null; } MemberBuilder member = Member.builder(); if ( memberDto != null ) { member.name( memberDto.getName() ); member.age( memberDto.getAge() ); member.address( memberDto.getAddress() ); member.phoneNumber( memberDto.getPhoneNumber() ); member.email( memberDto.getEmail() ); } if ( id != null ) { member.id( id ); } return member.build(); } }
-
Test
public class MemberMapperLauncher { public static void main(String[] args) { Address address = Address.builder() .address1("kor") .address2("seoul") .address3("gangnam") .build(); Member member = Member.builder() .id(1L) .name("jack") .age(20) .address(address) .phoneNumber("010-1234-1234") .email("abcd@google.com") .build(); MemberDto memberDto = MemberMapper.INSTANCE.memberToMemberDto(member); System.out.println(memberDto); //MemberDto(name=jack, age=20, address=base.domain.example.mapstruct.Address@4edde6e5, phoneNumber=010-1234-1234, email=abcd@google.com) Member member1 = MemberMapper.INSTANCE.memberToMemberDto(memberDto, 1234L); System.out.println(member1); //Member(id=1234, name=jack, age=20, address=Address(address1=kor, address2=seoul, address3=gangnam), phoneNumber=010-1234-1234, email=abcd@google.com) } }
-
심화
- @Mapper Annotaion에서 지원하는 detail settings
- @Mapping Annotaion에서 지원하는 detail settings
- @MapperConfig(componentModel = "spring" ...)을 사용하면 Spring 사용시 Mapper를 Bean으로 등록해준다. 나머지 상세 설정은 레퍼런스 페이지를 참고한다.
-
Reference:
'Programming > 유용한 라이브러리' 카테고리의 다른 글
JsonPath + Template Engine (0) | 2020.04.11 |
---|---|
JsonPath (0) | 2020.04.03 |
Guava AsyncEventBus (0) | 2019.02.07 |
Guava (0) | 2019.02.07 |
Flyway 설정 (0) | 2019.01.18 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- @Primary
- java generic
- Spring JDBC Template
- Embeddable Mapping
- spring spel
- scikit-learn
- SmartLifecycle
- Query DSL
- JPA
- Embedded Mapping
- 복합키 Mapping
- Mapping
- Typesafe Config
- RetryTemplate
- java EqualsAndHashCode
- Registrar
- Spring Registrar
- Spring
- docker
- DI
- Join Table
- Charles proxy
- Akka
- JPA Criteria
- java Equals
- Sprint RetryTemplate
- Discriminate Mapping
- Criteria
- guava
- Property
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함