JAVA對(duì)象的屬性自動(dòng)復(fù)制

在JAVA編程的WEB開(kāi)發(fā)和UPDATE過(guò)程當(dāng)中,通常的做法是先load出數(shù)據(jù)庫(kù)的原值,然后再把頁(yè)面的值更新數(shù)據(jù)庫(kù)中數(shù)據(jù),如果頁(yè)面對(duì)象的存儲(chǔ)值不夠(相對(duì)數(shù)據(jù)庫(kù)LOAD出來(lái)的對(duì)象),那么就有可能在更新時(shí)用NULL把原值覆蓋,針對(duì)上面的情況自己寫(xiě)了個(gè)類(lèi),來(lái)進(jìn)行自動(dòng)賦值
public class CopyObject {

public static Object copy(Object rtuObject, Object object){

Class classType = object.getClass();
Class rtuClassType = rtuObject.getClass();


Field fields[] =classType.getDeclaredFields();

for(int i=0;i Field field = fields[i];
String fieldName = field.getName();
String firstLetter = fieldName.substring(0,1).toUpperCase();

String getMethodName = "get"+firstLetter+fieldName.substring(1);
String setMethodName = "set"+firstLetter+fieldName.substring(1);


try {

Method getMethod = classType.getMethod(getMethodName,new Class[]{});
Method setMethod = rtuClassType.getMethod(setMethodName,new Class[]{field.getType()});
Object value = getMethod.invoke(object,new Object[]{});
if (null!=value){
setMethod.invoke(rtuObject,new Object[]{value});
}

} catch (Exception e) {
e.printStackTrace();
}[@more@]

文章標(biāo)題:JAVA對(duì)象的屬性自動(dòng)復(fù)制
標(biāo)題URL:http://bm7419.com/article16/jdcedg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、外貿(mào)建站、響應(yīng)式網(wǎng)站域名注冊(cè)、網(wǎng)站改版網(wǎng)站營(yíng)銷(xiāo)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷(xiāo)型網(wǎng)站建設(shè)