JDBC查詢Map轉(zhuǎn)對(duì)象實(shí)現(xiàn)過程詳解
雖然項(xiàng)目中都夾雜了Hibernate的支持,但是團(tuán)隊(duì)開發(fā)中,很多人為了編寫特殊查詢的代碼時(shí)都使用了JDBC進(jìn)行查詢。JDBC查詢后返回的是一個(gè)List集合,List中組裝的是Map,一個(gè)Map就是一個(gè)對(duì)應(yīng)的對(duì)象。但是接口不能直接返回Map,都是返回的對(duì)象,以方便自己和其他人使用,為了轉(zhuǎn)換這個(gè)Map,往往寫這樣的代碼:
@SuppressWarnings("unchecked")
public static MS_Mont analyzeMapToMS_Mont(Map map){
MS_Mont obj = new MS_Mont();
if(null != map.get("montNo")) obj.setMontNo(Integer.parseInt(map.get("montNo").toString()));
if(null != map.get("montName")) obj.setMontName(map.get("montName").toString());
if(null != map.get("montType")) obj.setMontType(Integer.parseInt(map.get("montType").toString()));
if(null != map.get("montLength")) obj.setMontLength(Integer.parseInt(map.get("montLength").toString()));
if(null != map.get("montDesc")) obj.setMontDesc(map.get("montDesc").toString());
if(null != map.get("bigType")) obj.setBigType(Integer.parseInt(map.get("bigType").toString()));
if(null != map.get("bigTypeName")) obj.setBigTypeName(map.get("bigTypeName").toString());
if(null != map.get("littleType")) obj.setLittleType(Integer.parseInt(map.get("littleType").toString()));
if(null != map.get("littleTypeName")) obj.setLittleTypeName(map.get("littleTypeName").toString());
if(null != map.get("insertTime")) obj.setInsertTime(map.get("insertTime").toString());
if(null != map.get("updateTime")) obj.setUpdateTime(map.get("updateTime").toString());
if(null != map.get("userNoRe")) obj.setUserNoRe(Integer.parseInt(map.get("userNoRe").toString()));
if(null != map.get("userNoLast")) obj.setUserNoLast(Integer.parseInt(map.get("userNoLast").toString()));
return obj;
}
很麻煩,很多,很枯燥。
為了解決這個(gè)問題,我列出一個(gè)解決方法,寫一個(gè)方法,傳入要賦值的對(duì)象和Map,然后根據(jù)列的屬性名稱從Map中獲得響應(yīng)的值,然后賦值給這個(gè)對(duì)象的屬性。
例如,這里寫了一個(gè)簡(jiǎn)單的查詢:
public CM_Line getObjectBean(int lineNo) {
try {
String sql = "select * from cm_line where lineNo=?";
Object[] obj = new Object[]{ lineNo };
List rows = jdbcTemplate.queryForList( sql, obj );
if(null != rows && rows.size() > 0) {
CM_Line line = new CM_Line();
return (CM_Line) line.analyzeMap((Map)rows.get(0));
} else {
return null;
}
} catch (Exception e) {
logger.error(e);
}
return null;
}
然后我們調(diào)用了他的analyzeMap方法,這個(gè)方法把當(dāng)前對(duì)象當(dāng)作要賦值的對(duì)象,然后調(diào)用公用方法進(jìn)行組裝:
public Object analyzeMap(Map<String, Object> para){
Object obj = this;
ObjectUtil.setValToObj(obj, para);
return obj;
}
公用方法:
public synchronized static void setValToObj(Object entityName, Map<String, Object> para){
try {
Class c = entityName.getClass();
// 獲得對(duì)象屬性
Field field[] = c.getDeclaredFields();
for (Field f : field) {
try {
PropertyDescriptor pd = new PropertyDescriptor(f.getName(), c);
Method writeMethod = pd.getWriteMethod();
if(!CommonCheck.isNullOrEmpty(para.get(f.getName())))
writeMethod.invoke(entityName, para.get(f.getName()));
} catch (Exception e) {
}
}
} catch (Exception e) {
}
}
下面就有人說了,那根據(jù)對(duì)象獲得這個(gè)對(duì)象的Map怎么搞,這個(gè)之前已經(jīng)寫過了,不這里仍然把代碼放一下:
/**
* 返回一個(gè)對(duì)象的屬性和屬性值
*/
public synchronized static LinkedHashMap<String,String> getProAndValMap(Object entityName) {
LinkedHashMap<String,String> map = new LinkedHashMap<String, String>();
try {
Class c = entityName.getClass();
// 獲得對(duì)象屬性
Field field[] = c.getDeclaredFields();
for (Field f : field) {
Object v = invokeMethod(entityName, f.getName(), null);
if(null != v) map.put(f.getName(), v.toString());
else map.put(f.getName(), "");
}
} catch (Exception e) {
map = null;
}
return map;
}
/**
* 獲得對(duì)象屬性的值
*/
private synchronized static Object invokeMethod(Object owner, String methodName,
Object[] args) throws Exception {
Class ownerClass = owner.getClass();
methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
Method method = null;
try {
method = ownerClass.getMethod("get" + methodName);
} catch (Exception e) {
}
return method.invoke(owner);
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Java的JDBC中Statement與PreparedStatement對(duì)象
- JDBC之PreparedStatement類中預(yù)編譯的綜合應(yīng)用解析
- JDBCTM 指南:入門6-PreparedStatement
- Oracle JDBC連接BUG解決方案
- jdbc實(shí)現(xiàn)寵物商店管理系統(tǒng)
- Jmeter基于JDBC請(qǐng)求實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)測(cè)試
- Java如果通過jdbc操作連接oracle數(shù)據(jù)庫(kù)
- JDBC PreparedStatement Like參數(shù)報(bào)錯(cuò)解決方案
相關(guān)文章
Java根據(jù)表達(dá)式獲取對(duì)象中的值及設(shè)置值的例子
這篇文章主要介紹了Java根據(jù)表達(dá)式獲取對(duì)象中的值及設(shè)置值的例子,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2025-03-03
Java網(wǎng)絡(luò)編程之IO模型阻塞與非阻塞簡(jiǎn)要分析
這篇文章主要介紹Java網(wǎng)絡(luò)編程中的IO模型阻塞與非阻塞簡(jiǎn)要分析,文中附有示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09
基于SpringBoot生成二維碼的幾種實(shí)現(xiàn)方式
本文將基于Spring Boot介紹兩種生成二維碼的實(shí)現(xiàn)方式,一種是基于Google開發(fā)工具包,另一種是基于Hutool來實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2022-03-03
Java實(shí)現(xiàn)隨機(jī)驗(yàn)證碼具體代碼
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)隨機(jī)驗(yàn)證碼具體代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
基于Java的MathML轉(zhuǎn)圖片的方法(示例代碼)
最近接到一個(gè)新需求mathML轉(zhuǎn)圖片怎么實(shí)現(xiàn)呢?剛開始還真是蒙圈了,不知道怎么實(shí)現(xiàn),今天小編記錄一種基于Java的MathML轉(zhuǎn)圖片的方法,感興趣的朋友一起看看吧2021-06-06

