鍍金池/ 問答/Java  C  網(wǎng)絡(luò)安全/ dozer支持list到list的映射嗎?

dozer支持list到list的映射嗎?

Goods.java

@Getter
@Setter
public class Goods {
    private Integer id;

    private String url;

    private Integer groupId;

    private Integer sourceId;

    private Integer userId;

    private Date createTime;

    private Date updateTime;

}

GoodsVo.java

@Getter
@Setter
public class GoodsVo {

    private Integer id;

    private String url;

    private Integer groupId;

    private Integer sourceId;

    private Integer userId;
}
List<Goods> goods = goodsService.listGoodsByGroup(groupId);

我要將List<Goods>轉(zhuǎn)為L(zhǎng)ist<GoodsVo>,請(qǐng)問該怎么轉(zhuǎn)???謝謝!

回答
編輯回答
空痕

你得自己做一下簡(jiǎn)單的封裝就實(shí)現(xiàn)了

public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass){
    List destinationList = Lists.newArrayList();
    for (Iterator i$ = sourceList.iterator(); i$.hasNext();){
      Object sourceObject = i$.next();
      Object destinationObject = dozer.map(sourceObject, destinationClass);
      destinationList.add(destinationObject);
    }  
    return destinationList;  
  }
2017年5月2日 16:27
編輯回答
詆毀你

我自己封裝的一個(gè)。

public class DozerUtils {

    /**
     * 封裝dozer處理集合的方法:List<S> --> List<T>
     */
    public static <T, S> List<T> mapList(final Mapper mapper, List<S> sourceList, Class<T> targetObjectClass) {
        List<T> targetList = new ArrayList<T>();
        for (S s : sourceList) {
            targetList.add(mapper.map(s, targetObjectClass));
        }
        return targetList;
    }
}
2017年12月31日 20:40