【发布时间】:2021-11-15 09:30:37
【问题描述】:
所以
我有课
public class Customer {
private String magicid;
private String name;
}
public class CustomerDto {
public String id;
public String customerName;
}
和一个映射器
@Mapper
public interface CustomerMapper {
CustomerMapper INSTANCE = Mappers.getMapper( CustomerMapper.class );
@Mapping(source = "customerName", target = "name")
@Mapping(target= magicId, expression="java(getMagicId(customerDto.getId()))")
Customer toCustomer(CustomerDto customerDto);
public String getMagicId(String id)
...
return magicId
问题是,mapstruct 查看字符串到字符串的映射器并将其用于客户名称的映射
我能做些什么来防止这种情况发生?
我看到有一个 MappingControl 选项可以在 @Mapper 注释中使用,但我不知道如何使用它。有人有例子吗?这是唯一的方法吗?
注意:我对 java 还很陌生,我真的很难理解 注释如何工作/可以参数化
【问题讨论】: