1. 程式人生 > 程式設計 >JDBC查詢Map轉物件實現過程詳解

JDBC查詢Map轉物件實現過程詳解

雖然專案中都夾雜了Hibernate的支援,但是團隊開發中,很多人為了編寫特殊查詢的程式碼時都使用了JDBC進行查詢。JDBC查詢後返回的是一個List集合,List中組裝的是Map,一個Map就是一個對應的物件。但是介面不能直接返回Map,都是返回的物件,以方便自己和其他人使用,為了轉換這個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; 
} 

很麻煩,很多,很枯燥。

為了解決這個問題,我列出一個解決方法,寫一個方法,傳入要賦值的物件和Map,然後根據列的屬性名稱從Map中獲得響應的值,然後賦值給這個物件的屬性。

例如,這裡寫了一個簡單的查詢:

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

然後我們呼叫了他的analyzeMap方法,這個方法把當前物件當作要賦值的物件,然後呼叫公用方法進行組裝:

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(); 
    // 獲得物件屬性   
    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) { 
  } 
} 

下面就有人說了,那根據物件獲得這個物件的Map怎麼搞,這個之前已經寫過了,不這裡仍然把程式碼放一下:

/**  
 * 返回一個物件的屬性和屬性值
 */   
public synchronized static LinkedHashMap<String,String> getProAndValMap(Object entityName) {  
	LinkedHashMap<String,String> map = new LinkedHashMap<String,String>();  
  try {  
    Class c = entityName.getClass();  
    // 獲得物件屬性  
    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;  
}
/**
 * 獲得物件屬性的值
 */
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);
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。