使用 MapStructs 的表达式映射 属性

Mapping a property using an expression of MapStructs

class Product {
    Object obj;
}

class Object {
    Float amount;
}

class ObjectDto {
    Integer price;
}

class ProductMapper{

    @Mapping(expression = "java(this.convert(dto.getObject().getPrice(), decimals))", target = "object.amount")
    public abstract Product(ProductDto dto, decimals);

    protected Float convert(Integer price, decimals){
        price.floatValue();
    }
}

我正在尝试通过 Mapstruct 表达式将 Integer 映射为 Float 并将参数和小数传递给函数但是当生成实现时未正确到达参数 "decimals" 我无法映射它.

有可能吗?

实现代码如下所示:

class ProductMapperImpl {
    method(ObjectDto objectDto, Integer decimals){
        product.setObject(objectDtoToObject(dto.getObject()));
    }
    Object objectDtoToObject(ObjectDto objectDto){
        Object obj = new Object();
        obj.setAmount (this.convert(objectDto.getPrice());
    }
}

那么,这个呢(我原来是这个意思):


@Mapper
public abstract class ProductMapper{

    @Mapping( target = "amount", source = "object.price" ); 
    public abstract Product toProduct(ProductDto dto, @Context Integer decimals);

    public Float createPrice(Integer price, @Context Integer decimals) {
       // do some Float stuff here
    }

}

顺便说一句:我不会使用 Float 作为金额,但总是使用 BigDecimal .. 只需 google 浮点数/金额..