对象拷贝工具类BeanutilsCopy
2016-04-28 23:43
225 查看
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; } }
相关文章推荐
- Eclipse下启动Tomcat报错: The archive: F:/JSPDev/apache-tomcat-8.0.32/bin/bootstrap.jar which is reference
- centos 7上安装python3.5.1和bs4
- Hadoop 好文章收录
- centos 6 下编译打包 ceph 10.2.0
- linux3.4.2移植
- Docker Hub 的伟大在那里?
- 理解Linux启动过程
- 构建高效安全的Nginx Web服务器
- 微服务架构只是个 “技术的浪潮 、流行” ?
- centos7编译安装mysql
- linux错误解决:CAN’T EXEC “LOCALE”: NO SUCH FILE OR DIRECTORY AT /USR/SHARE/PERL5/DEBCONF/ENCODING.PM
- nginx1.8.0和php7.0.4编译安装脚本
- Linux系统的理解及学习Linux内核的心得
- linux上破解myeclipse2014
- hbase的shell操作和java操作
- Linux课程学习之我思
- 关于OpenGL的绘制上下文
- 目标跟踪学习笔记_1(opencv中meanshift和camshift例子的应用)
- Linux内核学习总结
- linux基本命令(8)——cp命令