ModelMapper,将实体列表映射到 DTO 对象列表

新手上路,请多包涵

我正在使用 Spring MVC 框架编写简单的博客 Web 应用程序。我愿意在我的应用程序中添加 DTO 层。

我决定使用 ModelMapper 框架将 Entity DTO 的视图中使用的对象。

我只有一个问题。在我的主页上,我显示了我博客上的帖子列表。在我看来,它只是 Post (实体)对象的列表。我想更改它以将 PostDTO 对象列表传递到我的视图。 Is there any way to map List of Post objects to List of PostDTO object with single method call?我正在考虑编写 转换器 来转换它,但我不确定这是一个好方法。

此外,我在管理面板或我页面上每个帖子下方的评论等其他几个地方使用了 ListsEntities

链接到 GitHub 存储库上我的应用程序的代码: repository

原文由 Gromo 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 574
2 个回答

您可以创建实用程序类:

 public class ObjectMapperUtils {

    private static final ModelMapper modelMapper;

    /**
     * Model mapper property setting are specified in the following block.
     * Default property matching strategy is set to Strict see {@link MatchingStrategies}
     * Custom mappings are added using {@link ModelMapper#addMappings(PropertyMap)}
     */
    static {
        modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
    }

    /**
     * Hide from public usage.
     */
    private ObjectMapperUtils() {
    }

    /**
     * <p>Note: outClass object must have default constructor with no arguments</p>
     *
     * @param <D>      type of result object.
     * @param <T>      type of source object to map from.
     * @param entity   entity that needs to be mapped.
     * @param outClass class of result object.
     * @return new object of <code>outClass</code> type.
     */
    public static <D, T> D map(final T entity, Class<D> outClass) {
        return modelMapper.map(entity, outClass);
    }

    /**
     * <p>Note: outClass object must have default constructor with no arguments</p>
     *
     * @param entityList list of entities that needs to be mapped
     * @param outCLass   class of result list element
     * @param <D>        type of objects in result list
     * @param <T>        type of entity in <code>entityList</code>
     * @return list of mapped object with <code><D></code> type.
     */
    public static <D, T> List<D> mapAll(final Collection<T> entityList, Class<D> outCLass) {
        return entityList.stream()
                .map(entity -> map(entity, outCLass))
                .collect(Collectors.toList());
    }

    /**
     * Maps {@code source} to {@code destination}.
     *
     * @param source      object to map from
     * @param destination object to map to
     */
    public static <S, D> D map(final S source, D destination) {
        modelMapper.map(source, destination);
        return destination;
    }
}

并根据您的需要使用它:

 List<PostDTO> listOfPostDTO = ObjectMapperUtils.mapAll(listOfPosts, PostDTO.class);

原文由 Andrew Nepogoda 发布,翻译遵循 CC BY-SA 4.0 许可协议

考虑到您有一个 Post 实体列表( postEntityList )和一个 PostDTO 类,您可以尝试以下操作:

使用以下导入以获得所需的结果

import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import java.lang.reflect.Type;

使用下面的代码

Type listType = new TypeToken<List<PostDTO>>(){}.getType();
List<PostDTO> postDtoList = modelmapper.map(postEntityList,listType);

原文由 André Carvalho 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题