利用反射更新類中map快取資料

2021-09-25 17:57:03 字數 1411 閱讀 4147

@resource

private static mapsystemproperty;

private static mapconfigcahche = new hashmap();

/** * 清除system配置快取

* @return

* @throws illegalacces***ception

* @throws illegalargumentexception

* @throws invocationtargetexception

* @throws nosuchmethodexception

* @throws securityexception

*/@responsebody

public string deletesystemapcache() throws illegalacces***ception, illegalargumentexception,

invocationtargetexception, nosuchmethodexception, securityexception ).newinstance(new object {});

} catch (instantiationexception e)

// 獲得物件的所有屬性

field fields = classtype.getdeclaredfields();

for (int i = 0; i < fields.length; i++)

string firstletter = fieldname.substring(0, 1).touppercase();

// 獲得和屬性對應的set***()方法的名字

string setmethodname = "set" + firstletter + fieldname.substring(1);

// 獲得和屬性對應的set***()方法

method setmethod = classtype.getmethod(setmethodname, new class );

// 呼叫原物件的get***()方法

object value = null;

// 呼叫拷貝物件的set***()方法

setmethod.invoke(objectcopy, new object );

} mapresultmap = new hashmap();

resultmap.put("data", 0);

resultmap.put("result", constants.result_success);

string returnresult = json.tojsonstring(resultmap);

return returnresult;

}}

利用反射將實體類物件轉成Map

在我們程式設計的過程中往往會用到反射,利用反射有時候可以更容易的做到一些事情,下面就說乙個反射的實際應用場景吧,就是將object轉化成乙個map,object裡的屬性名對應map的key,object裡的屬性值對應map的value public static mapobjecttomap obj...

在專案中利用Map實現資料快取功能

在專案開發中,對於資料庫中不經常更改,但需要經常查詢的資料,可以使用map將資料快取,減少資料庫io次數,提公升效能。一下 是實現過去12個月,按月份查詢每個月的進出廠數量,每個月的數量不會改變。首先定義乙個全域性私有變數map private map map new hashmap 業務方法 pu...

利用反射將資料讀入實體類

利用反射將資料讀入實體類 在實際開發中,我們經常需要從資料庫中讀取資料並賦值給實體類的相應屬性。比如 public role getroles int blogid if reader name dbnull.value if reader description dbnull.value read...