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

标签 java modelmapper

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

我决定使用 ModelMapperEntity 对象到我的 View 中使用的 DTO 对象的转换框架。

我只有一个问题。 在我的主页上,我显示了我博客上的帖子列表。在我看来,它只是 Post (Entity) 对象的列表。我想更改它以将 PostDTO 对象列表传递到我的 View 。有什么方法可以通过单个方法调用将 Post 对象的 List 映射到 PostDTO 对象的 List 吗? 我在考虑写 converter这将转换它,但我不确定这是一个好方法。

此外,我还在其他几个地方使用 ListsEntities,例如管理面板或在我页面上的每个帖子下方发表评论。

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

最佳答案

您可以创建 util 类:

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);

关于java - ModelMapper,将实体列表映射到 DTO 对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47929674/

相关文章:

java - 公开多个接口(interface)的技术(通过静态创建方法)

java - 制作粘性流

java - 如何使用 ModelMapper 将外键从 dto 映射到实体对象?

java - ModelMapper - 如何从源计算值并将其设置为目标

Java 可选的 getter 在模型映射器中不起作用

java - Firebase引用始终获取最后一个用户被创建的android studio

java - NoSuchElementException:未找到行 - Scanner Java

java - 如何在我的 Android 应用程序的导航栏中添加按钮?

java - ModelMapper:如何根据源中的集合是否包含元素来设置目标属性

ModelMapper 无法将 java.lang.String 转换为 java.lang.Long