๐โ๏ธ ๊ณ์ํด์ ์ ๋ฐ์ดํธ ํ๊ธฐ ๐โ๏ธ
1. ๊ณต์๋ฌธ์
2. ์ ์ฌ์ฉํ ๊น?
3. ๋์๋ฐฉ์
4. ์์กด์ฑ
5. Mapper Inteface ์์ฑ
6. Mapper ๊ตฌํ์ฒด ์ดํด๋ณด๊ธฐ
๊ณต์๋ฌธ์
https://mapstruct.org/documentation/stable/reference/pdf/mapstruct-reference-guide.pdf
MapStruct ์ ์ฌ์ฉํ ๊น?
๋ณดํต DB์์ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๊ณ ์๋น์ค๋จ์์ ๋ก์ง์ ์ค๊ณ ํ๊ธฐ ์ํด Entity -> Dto , Dto -> Entity ์์ ์ ํ๊ฒ ๋๋๋ฐ ์ด๋ฌํ Mapping ์์ ์ ํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋ง์ด ์กด์ฌํ๋ค ๊ทธ์ค ์๋์ ๋งํฌ๋ฅผ ํ์ธํด๋ณด๋ฉด MapStruct ํผํฌ๋จผ์ค๊ฐ ๋น ๋ฅธ ๊ฒ์ ํ์ธํ ์ ์๋ค.
https://www.baeldung.com/java-performance-mapping-frameworks
๋์๋ฐฉ์
์ฌ์ฉ์๊ฐ ์ธํฐํ์ด์ค ๊ตฌํ์ ์์ฑํ๊ณ ๋งตํ ๊ด๋ จ ์ค์ ์ ํด์ฃผ๋ฉด ์ปดํ์ผํ๋๋์ MapStruct๊ฐ ๋ด๋ถ ๊ตฌํ์ฒด์์ setter๋ฅผ ์ด์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ๋งตํํ๋ค.
์์กด์ฑ
Maven
...
<properties>
<org.mapstruct.version>1.4.2.Final</org.mapstruct.version>
</properties>
...
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
</dependencies>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source> <!-- depending on your project -->
<target>1.8</target> <!-- depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<!-- other annotation processors -->
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
Gradle
dependencies {
...
implementation 'org.mapstruct:mapstruct:1.4.2.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'
}
Mapper Inteface ์์ฑ
public class OrderDto{
private int itemCount;
private ItemStatus itemStatus;
private Brand brand;
}
public class Orders{
private long id;
private ItemList itemList;
private Brand brand;
}
public class Brand{
private Long id;
private String name;
private Stirng tel;
}
@Mapper(uses = {BrandMapper.class})
public interface OrderMapper {
OrderMapper INSTANCE = Mappers.getMapper(OrderMapper.class);
@Mapping(target = "brand", source = "brand")
@Mapping(target = "itemCount", source = "itemList", qualifiedByName = "itemCount")
@Mapping(target = "itemStatus", source = "itemList.status")
OrderDto toDto(Orders orders);
List<OrderDto> toListDto(List<Orders> orders);
@Named("itemCount")
default int itemCount(List<ItemList> itemList){
return (int) itemList.stream()
.map(itemList::getItemId)
.distinct()
.count();
}
}
Orders Entity -> OrderDto ๋ก ๋ณ๊ฒฝํ๋ ์์ ์ด๋ค
@Mapper(uses = {BrandMapper.class})
- OrderDto ์์ Brand ํ๋๊ฐ ์๊ธฐ ๋๋ฌธ์ BrandMapper ๋ ๋ง๋ค์ด์ค ๋ค์์ ํฌํจ์์ผ์ค์ผํ๋ค.
๋ฐ์ ๊ตฌํ์ฒด ์์ ์ด๋ป๊ฒ ์ฌ์ฉ๋๋์ง ์ค๋ช ํ๊ฒ ์
- Orders ์ id ์ OrderDto ์ id ๋ ํ๋๋ช ์ด ๊ฐ๊ธฐ ๋๋ฌธ์ ๋ฐ๋ก @Mapping ํด์ฃผ์ง ์์๋ ์๋์ผ๋ก ๋งตํ๋๋ค.
๊ทธ ์ธ์ ํ์ํ ํ๋๋ @Mapping์ผ๋ก ์ฐ๊ฒฐํด์ค๋๋ค
- Dto์ ์๋ ItemStatus ๋ Orders์ itemList์ status๋ก ๋งตํํ๋ค.
@Mapping(target = "itemCount", source = "itemList", qualifiedByName = "itemCount")
@Named("itemCount")
- Dto๋ก ๋ณํ์ค์ ๊ณ์ฐ์ด ํ์ํ ๊ฒฝ์ฐ ๋ฐ๋ก ๋ฉ์๋๋ฅผ ๋นผ์ค๋๋ค -> ๊ตฌํ์ฒด ์์ฑ์ ์ ์ธํ๊ณ ์์ฑํจ. ํด๋น ์ด๋ฆ์ ๊ฐ์ง ๋ฉ์๋๋ฅผ ํธ์ถํ๋ค.
Mapper ๊ตฌํ์ฒด ์ดํด๋ณด๊ธฐ
์ด๋ค์์ผ๋ก ์์ฑํ๋์ง ํ์ธํ๊ณ ์ถ๋ค๋ฉด ํด๋น ์ธํฐํ์ด์ค์ ๊ตฌํ์ฒด๋ฅผ ์ฐพ์๊ฐ๋ณด๋ฉด ๋๋ค. ์์๋ ์๋์ ๊ฐ๋ค
(Interface ์์ฑ ํ target/generated-sources-annotaions ๋ฅผ rebuild ํ๋ฉด ์๋์ผ๋ก ์์ฑ๋๋ค)
๋๋ ๊ธฐ๋ณธ์์ฑ์, Setter ๊ฐ ์๋ Builder ๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ์๋์ ๊ฐ์ ์ฝ๋๊ฐ ์์ฑ๋์ด์๋ค.
public class OrderMapperImpl implements OrderMapper {
private final BrandMapper brandMapper = Mappers.getMapper( BrandMapper.class );
@Override
public OrderDto toDto(Orders orders) {
if ( orders == null ) {
return null;
}
OrderDtoBuilder orderDto = OrderDto.builder();
orderDto.brand( brandMapper.toDto( brandMapper.toDto(orders) ) );
orderDto.itemCount( itemCount( orders.getItemList() ) );
orderDto.status( itemStatus( orders ) );
private ItemStatus itemStatus(Orders orders) {
if ( orders == null ) {
return null;
}
ItemList itemList = orders.getItemList();
if ( itemList == null ) {
return null;
}
ItemStatus itemStatus = goodsReceipt.getItemStatus();
if ( itemStatus == null ) {
return null;
}
return itemStatus;
}
return orderDto.build();
}
}
'๐ WEB > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Test-Driven-Development ํ ์คํธ ์ฃผ๋ ๊ฐ๋ฐ (0) | 2022.03.24 |
---|---|
Java 8 Interface - default ๋ฉ์๋, static ๋ฉ์๋ (0) | 2022.03.23 |
[Lombok] Difference Between @Value and @Data (0) | 2022.03.11 |
๋ด๊ฐ ๋ณด๋ ค๊ณ ์ ๋ฆฌํ๋ JAVA ๋ฉํฐ ์ค๋ ๋ 5- ์ค๋ ๋ํ (0) | 2021.10.06 |
๋ด๊ฐ ๋ณด๋ ค๊ณ ์ ๋ฆฌํ๋ JAVA ๋ฉํฐ ์ค๋ ๋ 4- ๋ฐ๋ชฌ ์ค๋ ๋, ์ค๋ ๋ ๊ทธ๋ฃน (0) | 2021.10.06 |