--引用涉及的司內工具類 ,恕不提供。
兩個簡單的Bean:屬性基本都是基礎類型(如int,float)或一些基礎類(如 Date,String),除了名字不一樣,屬性幾乎一模一樣,值複製你可以用Apache的BeanUtils.copyProperties,也可以用(司內)SuperBeanTools.simpleCopy。
如果是複雜Bean(含有以List爲形式的下級簡單Bean),貌似沒有太簡單明瞭的工具,因此,有了這個AR工具BizVoUtils。
【例】
PolicyDto |
保單大對象 |
|
EndorseDto |
批單大對象 |
|
有子Dto型屬性 |
PrpCmainDto mainDto |
String policyNo; String applyCode; ... |
|
PrpPmain mainDto |
String endorDto; String applyCode; ... |
有List型屬性(注,List命名一般應完全一致) |
List insuredList; -- List中是PrpCinsuredDto |
|
|
List insuredList; -- List中是PrpPinsuredDto
|
|
|
PrpCinsuredDto中有這些屬性 |
String policyNo; String insuredCode; String insuredName;
|
|
PrpPinsuredDto中有這些屬性 |
String policyNo; String insuredCode; String insuredName; |
... |
... |
... |
|
... |
... |
【例子說明】
- 兩個業務大對象的屬性幾乎一模一樣——除了子Dto名中或的這一點差異:保單是PrpC,批單是PrpP;
- List屬性命名一致(見過不規範者,比如保單大對象中被保險人List命名成prpCinsuredList,批單大對象中命名成prpPinsuredList,也可以改工具中代碼以解決,但很麻煩,暫不提供支持,所以沒啥事,還是都照規範來吧);
【使用說明】
- 由於Arch4中底層Dto無基礎類型(double,int),只有基礎類(Double,Integer),因此本版不支持基礎類型;
- 字段名除了差異部分,其它字母應完全一致,大小寫也要完全一致。如果有某個字段賦值未成功,請檢查是否符合這個條件;
- 實現公共特殊字段的賦值——比如批單大對象中EndorNo是公共的,但保單大對象中卻沒有,因此,在從保單大對象向批單大對象賦值時,若沒有這個功能,還是得對每個子對象拿出來去單獨賦值EndorNo;
- 如果目標Dto中已經有部分值,此工具實現了不覆蓋,先不多解釋。
【使用樣例】
兩個不同業務大對象的拷貝
//比如從保單Dto要生成批單Dto
//prpCmainDto是一個已經從數據庫或頁面組裝好的大對象
//先創建目錄對象prpPmainDto
Map<String, Object> specialFieldMap = new HashMap<String, Object>();
specialFieldMap.put("EndorNo", endorNo);//字段名首字母要大寫
PrpPmainDto prpPmainDto = new PrpPmainDto();
BizVoUtils.copyBizObj(prpCmainDto, prpPmainDto ,
BizVoUtils.POLICY, BizVoUtils.ENDOR,
specialFieldMap);
業務大對象自己的深拷貝
PrpCmainDto prpCmainDto2 = new PrpCmainDto();
BizDtoUtil.copyBizObj(prpCmainDto,prpCmainDto2);
【源碼】
package com.sinosoft.utility.bean;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.sinosoft.sysframework.common.util.ObjectUtils;
/**
* 前身BizDtoUtil
*
* @author amosryan 2010-08
*
*/
public class BizVoUtils extends DtoUtils {
/** 是否打印調試信息? */
private static boolean verbose = true;
/** 項目 */
public static final String PROJECT = "PrpSproject";
/** 項目批改 */
public static final String ENDOR_PROJECT = "PrpSprojectEndor";
/** 保單 */
public static final String POLICY = "PrpC";
/** 批單 */
public static final String ENDOR = "PrpP";
/** 可直接賦值的類屬性類型 */
private static Map<Class, String> supportTypeMap = new HashMap<Class, String>();
static {
supportTypeMap.put(Integer.class, "");
supportTypeMap.put(Long.class, "");
supportTypeMap.put(Double.class, "");
supportTypeMap.put(BigDecimal.class, "");
supportTypeMap.put(String.class, "");
supportTypeMap.put(Short.class, "");
supportTypeMap.put(Date.class, "");
supportTypeMap.put(java.sql.Date.class, "");
supportTypeMap.put(Boolean.class, "");
supportTypeMap.put(byte[].class, "");
}
/**
* 用於大對象自己深拷貝
* @param target 目標業務Dto
* @param source 源業務Dto
* @param targetBizType 目標業務Dto業務類型
* @param sourceBizType 源業務Dto業務類型
*/
public static void copyBizObj(Object source, Object target) {
return copyBizObj(source,target,"","",null);
}
/**
* 複製複雜的業務大對象(目標對象某屬性有值則跳過)
*
* @param source
* @param target
* @param sourceBizType
* @param targetBizType
* @param specialFieldsMap
* 需要給target傳值的特殊域,常用於target比source多出的主鍵,傳格式以map.put("fieldName",fieldValue
* )傳值 (注意FieldName首字母大寫)
*/
public static void copyBizObj(Object source, Object target,
String sourceBizType, String targetBizType,
Map<String, Object> specialFieldsMap) {
List targetSetMethodList = ObjectUtils.getSetter(target.getClass());
List sourceGetMethodList = ObjectUtils.getGetter(source.getClass());
Map sourceGetMethodMap = new HashMap();
if (specialFieldsMap == null) {
specialFieldsMap = new HashMap();
}
for (Iterator i = sourceGetMethodList.iterator(); i.hasNext();) {
Method sourceGetMethod = (Method) i.next();
sourceGetMethodMap.put(sourceGetMethod.getName(), sourceGetMethod);
}
try {
for (Iterator i = targetSetMethodList.iterator(); i.hasNext();) {
Method targetSetMethod = (Method) i.next();
String targetFieldName = targetSetMethod.getName().substring(3);
String sourceFieldName = targetFieldName.replaceFirst(
targetBizType, sourceBizType);
// 如果是List型,則根據源創建目標Dto,放入目標List
Method sourceGetMethod = (Method) sourceGetMethodMap.get("get"
+ sourceFieldName);
if (sourceGetMethod != null) {
Object sourceValue = sourceGetMethod.invoke(source,
new Object[0]);
if (sourceValue != null) {
String sourceName = getBeanName(sourceValue);
String targetName = null;
if (sourceValue instanceof List) {// List情況
List targetList = (List) DtoUtils.getFieldValue(
target, targetFieldName);
if (targetList == null) {// 如果原來無值,新建List
targetList = new ArrayList();
targetSetMethod.invoke(target,
new Object[] { targetList });
}
List sourceList = (List) sourceValue;
for (int j = 0; j < sourceList.size(); j++) {
Object o = sourceList.get(j);
Object obj = null;
if (j < targetList.size()) {// 如果List此位置有值
obj = targetList.get(j);
copyBizObj(o, obj, sourceBizType,
targetBizType, specialFieldsMap);
} else {// 此位置無值就新建
String packageName = getPackageName(o);
String sourceBeanName = getBeanName(o);
targetName = packageName
+ sourceBeanName.replaceFirst(
sourceBizType,
targetBizType);
obj = getInstance(targetName, packageName,
sourceBeanName);
copyBizObj(o, obj, sourceBizType,
targetBizType, specialFieldsMap);
targetList.add(obj);
}
}
targetName = ArrayList.class.getName();
} else if (!supportTypeMap.containsKey(sourceValue
.getClass())) {// 業務對象情況
Object obj = DtoUtils.getFieldValue(target,
targetFieldName);
if (obj == null) {
String packageName = getPackageName(sourceValue);
targetName = packageName
+ sourceName.replaceFirst(
sourceBizType, targetBizType);
if (verbose) {
System.out.println();
System.out.println(" In unsupport:---");
System.out.println(" packageName:"
+ packageName);
System.out.println(" targetName:"
+ targetName);
}
obj = getInstance(targetName, packageName,
sourceName);
targetSetMethod.invoke(target,
new Object[] { obj });
}
copyBizObj(sourceValue, obj, sourceBizType,
targetBizType, specialFieldsMap);
} else {//葉子屬性進行值拷貝
targetName = sourceValue.getClass().getName();
Object obj = DtoUtils.getFieldValue(target,
targetFieldName);
if (obj == null) {
targetSetMethod.invoke(target,
new Object[] { sourceValue });
}
}
if (verbose) {
System.out.println();
System.out.println("Source名稱:" + sourceFieldName);
System.out.println("Source類型:"
+ sourceValue.getClass());
System.out.println("target名稱:" + targetFieldName);
System.out.println("target類型:" + targetName);
}
}
} else if (specialFieldsMap.containsKey(sourceFieldName)) {
targetSetMethod.invoke(target,
new Object[] { specialFieldsMap
.get(sourceFieldName) });
if (verbose) {
System.out.println();
System.out.println(sourceFieldName
+ " exists in SpecialFieldMap!");
}
} else {
if (verbose) {
System.out.println();
System.out.println(sourceFieldName
+ " not exists in source!");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static Object getInstance(String targetName, String packageName,
String sourceName) throws Exception {
Object obj = getInstance(targetName);
if (obj == null) {
targetName = packageName + sourceName;
obj = getInstance(targetName);
}
return obj;
}
}