对象拷贝工具类BeanutilsCopy

mport java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.dozer.DozerBeanMapper;

import com.google.common.collect.Lists;


public class BeanutilsCopy<T> {
    private static Logger log = Logger.getLogger("BeanutilsCopy");

    /**
     * 
     * 
     * apacheCopyProperties(单个对象拷贝)
     * 
     * @Title: apacheCopyProperties
     * @param @param dest
     * @param @param orig
     * @return void
     * @throws
     */
    public static void apacheCopyProperties(Object dest, Object orig) {
        try {
            BeanUtils.copyProperties(dest, orig);
        } catch (Exception e) {
            log.error("apacheCopyProperties 异常:" + e);
        }

    }

    /**
     * 
      * mapList(拷贝集合信息)
      * @Title: mapList
      * @param @param sourceList
      * @param @param destinationClass
      * @param @return
      * @return List<T>
      * @throws
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static <T> List<T> CopyList(Collection sourceList, Class<T> destinationClass)  
      {  
        DozerBeanMapper dozer=new DozerBeanMapper();
        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;  
      } 

    @SuppressWarnings("unchecked")
    public static <T, D> T populateTbyDBySpring(D sourceObj, Class<T> clazz)
      {
        if (sourceObj == null) {
          return null;
        }
        Object t = null;
        try {
          t = clazz.newInstance();
        } catch (IllegalAccessException e) {
            log.error("自动转换失败", e);
        } catch (InstantiationException exx) {
            log.error("自动转换失败", exx);
        }
        org.springframework.beans.BeanUtils.copyProperties(sourceObj, t);

        return (T) t;

      }

      @SuppressWarnings({ "rawtypes", "unchecked" })
    public static <T, D> List<T> populateTListbyDListBySpring(List<D> sourceObjs, Class<T> clazz)
      {
        if (sourceObjs == null) {
          return null;
        }
        int len = sourceObjs.size();
        List ts = new ArrayList(len);
        Object t = null;
        for (int i = 0; i < len; i++) {
          Object d = sourceObjs.get(i);
          t = populateTbyDBySpring(d, clazz);
          ts.add(t);
        }
        return ts;
      }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值