利用反射封裝Map成Vo

package com.fw.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;


import com.fw.common.sql.SqlHandler;
import com.ve.vo.UserVO;
/**
 * 字段解析、組裝容器
 * @description 
 * @author zhanglm
 * @date   2014-4-28
 */
public class ParseUtils<T extends Object> 
{
	
	private static Logger log = Logger.getLogger(ParseUtils.class);
	
	/**
	 * 將list中的map組裝成list<vo>
	 * @param list
	 * @param t
	 * @return
	 */
	public  List<T> parseListToVo(List<Map<String,Object>> list,T t)
	{
		List<T> lt = new ArrayList<T>();
		
		for(int i=0;i<list.size();i++)
		{
			lt.add(parseMapToVo(list.get(i),t));
		}
		
		return lt;
		
	}
	
	
	/**
	 * 通過反射機制將map中的key對應value注入到vo中
	 * @param map
	 * @param t
	 * @return
	 * @throws Exception
	 */
	public T parseMapToVo(Map<String,Object> map, T t)
	{
		Object obj = null;
		try
		{
			
			Class<?> cl = t.getClass();
			obj = cl.newInstance();
			
			Field[] fields = cl.getDeclaredFields();
			
			Iterator<String> it = map.keySet().iterator();
			
			while (it.hasNext()) 
			{
				String fieldName = it.next();
				for(Field f: fields)
				{
					
					if(f.getName().equalsIgnoreCase(fieldName))
					{
						// 判斷類型是否一致,調用反射注入值,
						String methodName = "set"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
						//System.out.println(methodName);
						Method inMethod = cl.getMethod(methodName, new Class[] { f.getType() });	
						//System.out.println(f.getType().getName());;
						inMethod.invoke(obj, new Object[] { TypeConversionUtils.converType(TypeConversionUtils.getType(map.get(fieldName)), f.getType().getName(), map.get(fieldName)) });
								
					}
				}
				
				
			}
			
		}
		catch(Exception e)
		{
			log.error("ParseUtils parseMapToVo error.");
			log.error(e.getMessage());
			
		}
		
		return (T) obj;
	}
		
	

	
	
	
		

	
	
	public static void main(String[] args)throws Exception {
		ParseUtils<UserVO> p = new ParseUtils<UserVO>();
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("userid", 111);
		map.put("username", "shinyzo");
		map.put("password", "123456");
		UserVO vo = p.parseMapToVo(map, new UserVO());
		System.out.println(vo.getUserid());
		System.out.println(vo.getUsername());
		System.out.println(vo.getPassword());
		
	}
	
	
}
 
 
package com.ve.vo;

import java.util.Date;

public class UserVO extends VO{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6165143238125216769L;
	
	
	private String userid;
	
	private String username;
	
	private String password;
	
	private Date  operTime;
	
	private Float currCount;
	
	private Double price;
	
	private Boolean isrecomment;
	
	private Short isopen;
	
	private Integer page;

	public Date getOperTime() {
		return operTime;
	}

	public void setOperTime(Date operTime) {
		this.operTime = operTime;
	}

	public Float getCurrCount() {
		return currCount;
	}

	public void setCurrCount(Float currCount) {
		this.currCount = currCount;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public Boolean getIsrecomment() {
		return isrecomment;
	}

	public void setIsrecomment(Boolean isrecomment) {
		this.isrecomment = isrecomment;
	}

	public Short getIsopen() {
		return isopen;
	}

	public void setIsopen(Short isopen) {
		this.isopen = isopen;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	

}

 

 

package com.fw.utils;

import java.util.Date;


/**
 * 類型轉換工具類
 * @description 
 * @author zhanglm
 * @date   2014-4-28
 */
public class TypeConversionUtils{
	
	/** 字符串  **/
	private static final String TYPE_STRING_FULL = "java.lang.String";
	private static final String TYPE_STRING = "String";
	/** 整型 **/
	private static final String TYPE_INT_FULL = "java.lang.Integer";
	private static final String TYPE_INT = "Int";
	
	/** 時間類型  **/
	private static final String TYPE_DATE = "java.util.Date";
	
	/** 浮點類型 **/
	private static final String Type_FLOAT_FULL = "java.lang.Float";
	private static final String Type_FLOAT = "float";
	
	/** 雙精度 **/
	private static final String TYPE_DOUBLE_FULL = "java.lang.Double";
	private static final String TYPE_DOUBLE = "double";
	
	/** 短整型 **/
	private static final String TYPE_SHORT_FULL = "java.lang.Short";
	private static final String TYPE_SHORT = "short";
	
	/** 布爾類型  **/
	private static final String TYPE_BOOLEAN_FULL ="java.lang.Boolean";
	private static final String TYPE_BOOLEAN ="boolean";
	

	public static Object converType(String oldType,String newType,Object oldValue)
	{
		// 字符串轉整型
		if( (oldType.equals(TYPE_STRING) || oldType.equals(TYPE_STRING_FULL)) && 
		    (newType.equals(TYPE_INT) || newType.equals(TYPE_INT_FULL)))
		{
			return (Integer) Integer.parseInt((String)oldValue);
		}
		// 整形轉字符串
		if( (oldType.equals(TYPE_INT)||oldType.equals(TYPE_INT_FULL)) && 
			(newType.equals(TYPE_STRING) || newType.equals(TYPE_STRING_FULL)))
		{
			return String.valueOf(oldValue);
		}
		
		// 字符串格式時間轉date類型時間
		
		
		
		return oldValue;
	}
	
	/**
	 * 通過對象匹配類型
	 * @param obj
	 * @return
	 */
	public static String getType(Object obj)
	{
		if(obj instanceof Integer)
		{
			return TYPE_INT_FULL;
		}
		
		if(obj instanceof String)
		{
			return TYPE_STRING_FULL;
		}
		
		if(obj instanceof Short)
		{
			return TYPE_SHORT_FULL;
		}
		
		if(obj instanceof Float)
		{
			return Type_FLOAT_FULL;
		}
		
		if(obj instanceof Double)
		{
			return TYPE_DOUBLE_FULL;
		}
		
		if(obj instanceof Date)
		{
			return TYPE_DATE;
		}
		
		
		return TYPE_STRING_FULL;
	}
	
}


 



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章