package com.roadway.keyin.bo.user.util;import java.lang.reflect.Field;import com.roadway.keyin.dao.hibernate.PreviewTemplate;import com.roadway.keyin.dao.hibernate.PreviewUser;public class SeterUtil { public static Object set(Object sourceObj, Object targetObj) { if (sourceObj == null || targetObj == null) { return targetObj; } Class<?> sourceObjCls = sourceObj.getClass(); Field[] targetObjFields = targetObj.getClass().getFields(); Field sourceObjField = null; for (Field targetObjField : targetObjFields) { try { sourceObjField = sourceObjCls .getField(targetObjField.getName()); if (sourceObjField == null) { continue; } /**//** 把属性名和属性类型加在一起 */ String sourceStr = sourceObjField.getType().toString(); sourceStr += sourceObjField.getName(); /**//** 把属性名和属性类型加在一起 */ String targetStr = targetObjField.getType().toString(); targetStr += targetObjField.getName(); /**//** 比较两个属性是否相等 *** */ if (sourceStr.equals(targetStr)) { targetObjField .set(targetObj, sourceObjField.get(sourceObj)); } else { System.out.println(sourceStr); System.out.println(targetStr); System.out.println(); } } catch (Exception ex) {} } return targetObj; } public static void main(String[] args) throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException { PreviewUser privewUser = new PreviewUser(); PreviewTemplate previewTemplate = new PreviewTemplate(); previewTemplate.setP100(15624L); SeterUtil.set(previewTemplate, privewUser); System.out.println(privewUser.getP100()); }}