ModelMapper,将实体列表映射到DTO对象列表 [英] ModelMapper, mapping list of Entites to List of DTO objects

查看:1119
本文介绍了ModelMapper,将实体列表映射到DTO对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing simple blog web application using Spring MVC framework. I am willing to add DTO layer to my app.

我决定使用 ModelMapper 框架将视图中的Entity对象转换为DTO对象.

I decided to use ModelMapper framework for conversion from Entity objects to DTO objects used in my views.

我只有一个问题. 在我的主页上,我正在显示博客中的帖子列表.在我看来,这只是Post(实体)对象的列表.我想更改它以将PostDTO对象的列表传递到我的视图.有什么方法可以通过单个方法调用将Post对象的List映射到PostDTO对象的List? 我当时正在考虑编写转换器来转换此格式,但我不确定这是一个好方法做吧.

I have just one problem. On my main page, I am showing a list of posts on my blog. In my view, it's just list of Post (Entity) objects. I want to change it to pass a list of PostDTO objects to my view. Is there any way to map List of Post objects to List of PostDTO object with single method call? I was thinking about writing converter that will convert this but I am not sure it's a good way to do it.

此外,我还在其他位置(例如管理面板或页面上每个帖子下方的评论)中使用Entities中的Lists.

Also, I am using Lists of Entities in few more places like administrative panel or comment below every Post on my page.

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

Link to code of my app on GitHub repository: repository

推荐答案

您可以创建util类:

You can create util class:

public class ObjectMapperUtils {

    private static ModelMapper modelMapper = new 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;
    }
}

并根据您的需要使用它:

And use it for your needs:

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

这篇关于ModelMapper,将实体列表映射到DTO对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆