DozerBeanMapper对象之间相同属性名赋值






package com.river.util;

import org.dozer.DozerBeanMapper;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

/**
 * 两个对象之间,相互属性名之间进行转换
 * <p>
 * Created by hepengfei on 2017/8/10.
 */

public class BeanMapperUtil {


    private static DozerBeanMapper mapper = new DozerBeanMapper();

    /**
     * 将源对象转换为指定类型的对象,相同的属性名进行复制
     *
     * @param source
     * @param destinationClass
     * @param <T>
     * @return
     */
    public static <T> T map(Object source, Class<T> destinationClass) {
        if (source == null) {
            return null;
        }
        return (T) mapper.map(source, destinationClass);
    }

    /**
     * 将源对象的属性值copy到目标对象的同名属性中
     *
     * @param source
     * @param target
     */
    public static void copy(Object source, Object target) {
        mapper.map(source, target);
    }

    /**
     * 将源对象集合转成目标对象类型集合,复制相同属性值
     *
     * @param sourceList
     * @param destinationClass
     * @return
     */
    public static List mapList(Connection sourceList, Class destinationClass) {
        List targetList = new ArrayList();
        if (sourceList != null) {
            for (Object source : targetList) {
                Object destination = mapper.map(source, destinationClass);
                targetList.add(destination);
            }
        }
        return targetList;
    }


}


猜你喜欢

转载自blog.csdn.net/jackieriver/article/details/77062471