Model Mapping

๋ชจ๋ธ ๋งคํ•‘์€ ์–ด๋Š Layer ๋‹จ์—์„œ ๊ตฌํ˜„ํ•ด์•ผํ• ๊นŒ?

  • Servie : ์—ฌ๋Ÿฌ ์„œ๋น„์Šค์—์„œ ์‚ฌ์šฉ์‹œ ์ค‘๋ณต์ฝ”๋“œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉฐ, ๋ชจ๋ธ๊ฐ„ ๋งคํ•‘ ๋กœ์ง์€ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ์•„๋‹ˆ๋ฏ€๋กœ, ๋ชฉ์ ์— ๋ถ€ํ•ฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

  • Model Object

    • ์„œ๋น„์Šค๊ฐ€ ์ปค์ง€๊ณ , ๋ชจ๋ธ๊ฐ„ ๋‹ค์–‘ํ•œ ๋งคํ•‘์ด ์žˆ์„ ๊ฒฝ์šฐ ์ ์  ๊ฐ์ฒด์•ˆ์— ๋ฉ”์„œ๋“œ๊ฐ€ ๋งŽ์•„์ง€๊ฒŒ ๋œ๋‹ค.

    • ๋ชจ๋ธ ๊ฐ์ฒด์— ๋ชจ๋ธ๊ฐ„ ๋งคํ•‘ ๋กœ์ง์€ ๋ชจ๋ธ ๊ฐ์ฒด์˜ ๋ชฉ์ ์— ๋ถ€ํ•ฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

      Mapper

      ๋ชจ๋ธ๊ฐ„ ๋งคํ•‘์— ๋Œ€ํ•œ ์ฑ…์ž„์„ ์ง„๋‹ค.

      ์ด๋•Œ, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ ๋ชจ๋ธ๊ฐ„ ๋งคํ•‘์„ ์ž๋™ํ™”ํ• ๊ฒƒ์ธ์ง€ ์ง์ ‘ Mapper๋ฅผ ์ด์šฉํ•ด์„œ ์ฒ˜๋ฆฌํ• ์ง€ ์ •ํ•ด์•ผํ•œ๋‹ค.

  • ModelMapper

    • ๋‚ด๋ถ€์ ์œผ๋กœ Refelction์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

    • Reflection์œผ๋กœ ์ธํ•œ ์„ฑ๋Šฅ ์ด์Šˆ์™€ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

  • MapStruct

    • Reflection์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

    • ์ปดํŒŒ์ผ์‹œ ์ƒ์„ฑํ•œ๋‹ค.

    • ๊ตญ๋‚ด๋Š” ์ ์ง€๋งŒ ๋ฏธ๊ตญ์—์„œ ์ ์  ์‚ฌ์šฉ๋„๊ฐ€ ๋†’์•„์ง€๊ณ  ์žˆ๋‹ค.

Model Mapper

Entity์™€ DTO๊ฐ„ ๊ฐ์ฒด ๋ณ€ํ™˜์„ ํ•  ๋•Œ ModelMapper๋ฅผ ํ™œ์šฉํ•˜๋ฉด, ์‰ฝ๊ฒŒ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • pom.xml

    <!-- model mapper - entity-DTO conversion -->
<dependency>
  <groupId>org.modelmapper</groupId>
  <artifactId>modelmapper</artifactId>
  <version>2.3.0</version>
</dependency>

์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•˜๋‹ค.

private PaymentDto convertToDto(Payment payment) {

        // Entity๋ฅผ DTO๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ์œ„ํ•ด modelmapper์‚ฌ์šฉ 
        ModelMapper modelMapper = new ModelMapper();

        PaymentDto paymentDto = modelMapper.map(payment, PaymentDto.class);

        return paymentDto;
}

์ฒ˜์Œ์—๋Š” ์ด๋ ‡๊ฒŒ convertToDto ๋ฉ”์†Œ๋“œ๋กœ Entity์—์„œ DTO๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ž‘์—…์„ ํ–ˆ์œผ๋‚˜, ์ด๊ฒƒ์€ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ Mapperํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

MapStruct

  • pom.xml

<!-- mapStruct - Entity to Dto-->
<dependency>
  <groupId>org.mapstruct</groupId>
  <artifactId>mapstruct</artifactId>
  <version>1.3.1.Final</version> 
</dependency>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <annotationProcessorPaths>
      <path>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>1.3.1.Final</version>
      </path>
      <!-- other annotation processors -->
    </annotationProcessorPaths>
  </configuration>
</plugin>
  • EntityMapper

import java.util.List;

public interface EntityMapper <D, E>{

    E toEntity(D dto);
    D toDto(E entity);
    List<D> toDtoList(List<E> entity);
}
  • PaymentMapper

@Mapper(componentModel = "spring")
public interface PaymentMapper extends EntityMapper<PaymentDto, Payment> {


}
  • Service

@Autowired
private PaymentMapper paymentMapper;
    @Override
    @Transactional
    public PaymentDto cancelRequest(String mbrId, String pmtId, long pmtAmt, Boolean isParticleCancle) {

    // ...
        Payment result = paymentRepository.save(newPayment); 
        PaymentDto paymentDto = paymentMapper.toDto(result);

        return paymentDto;
  }

์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๋ฉด๋œ๋‹ค. ๋งŒ์•ฝ์— ๋ณ€์ˆ˜๋ช…์ด ๋‹ค๋ฅด๋‹ค๋ฉด, @Mapping annotation์œผ๋กœ ์‰ฝ๊ฒŒ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

์ฐธ์กฐ

Last updated