modelmapper skip属性

arknldoa  于 2021-07-11  发布在  Java
关注(0)|答案(0)|浏览(281)

我想把我的实体Map到dto中。但我需要两种不同的方法。一个用于将整个实体Map到dto,另一个用于跳过某些属性(在这个特定示例中是child的集合)。
我的mapper类如下所示:

import javax.annotation.PostConstruct;

import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;

@Service
public class ItemMapper
{
    private final ModelMapper modelMapper = new ModelMapper();
    private final ModelMapper modelMapperSkipChilds = new ModelMapper();

    @PostConstruct
    public void init()
    {
        modelMapperSkipChilds.typeMap( Item.class, ItemDto.class )
            .addMappings( mapper -> mapper.skip( ItemDto :: setChilds ) );
    }

    public ItemDto mapToDto( Item item )
    {
        return modelMapper.map ( item, ItemDto.class );
    }

    public ItemDto mapToDtoSkipEntries( Item item )
    {
        return modelMapperSkipEntries.map( item, ItemDto.class );
    }
}

是否可以删除modelmapperskipchilds对象并仅处理modelmapper对象中的所有功能?
在方法“maptodoskipentries()”中,我需要如下内容:

return modelMapper
    .localTypeMap( Item.class, ItemDto.class )
    .addLocalMappings( mapper -> mapper.skip( ItemDto :: setChilds ) )
    .map( item, ItemDto.class );

其中.localtypemap和.addlocalmappings是已经存在的特性的具体实现,它只跳过特定方法的特定属性。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题