import java.util.Collection;
import java.util.List;

import org.dozer.DozerBeanMapper;

import com.google.common.collect.Lists;

/**
 * 封装并持有Dozer单例,深度转换Bean<->Bean的Mapper,实现: 1. 转换对象的类型 2. 转换Collection中对象的类型 3. 将对象A的值拷贝到对象B中
 */
public class BeanMapper {

    /**
     * 持有Dozer单例
     */
    private static DozerBeanMapper dozer = new DozerBeanMapper();

    /**
     * 转换对象的类型
     */
    public static <T> T map(Object source, Class<T> destinationClass) {
        return dozer.map(source, destinationClass);
    }

    /**
     * 转换Collection中对象的类型
     */
    @SuppressWarnings("rawtypes")
    public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
        List<T> destinationList = Lists.newArrayList();
        for (Object sourceObject : sourceList) {
            T destinationObject = dozer.map(sourceObject, destinationClass);
            destinationList.add(destinationObject);
        }
        return destinationList;
    }

    /**
     * 将对象A的值拷贝到对象B中
     */
    public static <T> T copy(Object source, T destinationObject) {
        dozer.map(source, destinationObject);
        return destinationObject;
    }

}

map使用实例:

Dozer实践--对象值转化

mapList实例:

Dozer实践--对象值转化

copy实例:

Dozer实践--对象值转化

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-11
  • 2021-08-17
  • 2021-10-12
  • 2021-05-21
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-14
  • 2021-07-09
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案