MapStruct : interface to Dto

By | 2022년 7월 20일
Table of Content

MapStruct : interface to Dto

interface

public interface ICardSaleInfo {

    String getItemDiscountCardName();
    BigDecimal getItemDiscountCardPrice();
    BigDecimal getItemDiscountCardBuyMinimumPrice();
}

repository

public interface ModifiedItemRepository extends JpaRepository<ModifiedItem, Integer> {

    @Query(value =
            " SELECT TOP 1 " +
            "     CSC.aaa AS 'itemDiscountCardName', " +
            "     CS.bbb AS 'itemDiscountCardPrice', " +
            "     CS.ccc AS 'itemDiscountCardBuyMinimumPrice' " +
            " FROM tbl_aaa AS CS " +
            " LEFT JOIN tbl_bbb AS CSC ON CS.cardCode=CSC.cardCode " +
            " WHERE " +
            "     1 = 1 "
            , nativeQuery = true)
    List<ICardSaleInfo> GetCardSaleInfo();
}

mapper

public interface IGenericMapper<D, I> {

    D toDto(I i);
}
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE, componentModel = "spring")
public interface ICardSaleInfoMapper extends IGenericMapper<CardSaleInfoDto, ICardSaleInfo> {
}

service

@RequiredArgsConstructor
@Service
public class ModifiedItemService {

    private final ModifiedItemRepository repository;
    private final ICardSaleInfoMapper iCardSaleInfoMapper = Mappers.getMapper(ICardSaleInfoMapper.class);

    private List<CardSaleInfoDto> toCardSaleInfoDto(List<ICardSaleInfo> lst) {
        return lst.stream().map(this::toDto).collect(Collectors.toList());
    }

    protected CardSaleInfoDto toDto(ICardSaleInfo entity) {
        return iCardSaleInfoMapper.toDto(entity);
    }
}

답글 남기기