我正在使用mapstrcut将实体映射到dto,现在面临一个问题,在DTO中有一个额外的字段“折扣”,并且我需要mapstruct来管理填充此字段,并且该字段是根据2计算的值(salePrice和retailPrice)。
我的问题是如何像使用mapstruct一样进行这种映射。
@Mapping(target="discount", source="retailPrice-salePrice")
Run Code Online (Sandbox Code Playgroud)
当我尝试添加此行时,构建时的代码中断表示:
error: No property named "salePrice*retailPrice" exists in source parameter(s).
@Mapping(source="salePrice*retailPrice", target="discount")
Run Code Online (Sandbox Code Playgroud)
那么我该如何进行映射呢?
您可以在以下情况下使用Java表达式的功能:
@Mapping( target="discount", expression="java( source.getRetailPrice() - source.getSalePrice())" )
Run Code Online (Sandbox Code Playgroud)
给定的“表达式”将按原样添加到生成的代码中。您可以@Mapper#imports()根据需要将其他导入添加到生成的类中。
我们已经(非常模糊)计划为这样的表达式提供一种实际的脚本语言支持,但目前还没有:)
| 归档时间: |
|
| 查看次数: |
1935 次 |
| 最近记录: |