如果它们从 Modelmapper 中为空,如何排除整个 属性

how to exclude whole property if they are null from Modelmapper

ModelMapper(http://modelmapper.org/)支持排除属性的吗?如果值为空。

我刚找到 PropertyMap。但这对我来说是一种约束。 因为我必须描述我想要的特定 属性。

像这样。

ModelMapper modelMapper = new ModelMapper();
modelMapper.addMappings(new PropertyMap<TestObject, TestObject>() {
    @Override
    protected void configure() {
        when(Conditions.isNull()).skip().setName(source.getName());
        when(Conditions.isNull()).skip().set...(source.get...());
        when(Conditions.isNull()).skip().set...(source.get...());
        when(Conditions.isNull()).skip().set...(source.get...());
        when(Conditions.isNull()).skip().set...(source.get...());
        when(Conditions.isNull()).skip().set...(source.get...());
    }
});

就我而言,我有很多 属性 并且冗长。 如果映射 属性 全部为空,如何排除它们。 有没有更舒服的方案?

谢谢。

您可以使用以下配置将 ModelMapper 配置为忽略所有为 null 的属性:

modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());

它很有用,例如,对于目标对象的部分更新,您只想从源对象复制非空的那些属性。