使用MapStructs的表达式映射属性

问题描述 投票:0回答:1
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 to 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());
    }
}
java mapstruct
1个回答
2
投票

那么,这个(这就是我最初的意思):


@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
    }

}

顺便说一句:我不会使用浮动数量,但总是一个BigDecimal ..只是google浮动/金额..

© www.soinside.com 2019 - 2024. All rights reserved.