溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JDBC如何實(shí)現(xiàn)查詢Map轉(zhuǎn)對(duì)象

發(fā)布時(shí)間:2020-11-02 15:11:55 來源:億速云 閱讀:174 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章為大家展示了JDBC如何實(shí)現(xiàn)查詢Map轉(zhuǎn)對(duì)象,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

@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);
}

上述內(nèi)容就是JDBC如何實(shí)現(xiàn)查詢Map轉(zhuǎn)對(duì)象,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI