`
bo_hai
  • 浏览: 563828 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

ProbeFactory.getProbe() 的使用

阅读更多
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ibatis.common.beans.Probe;
import com.ibatis.common.beans.ProbeFactory;
import com.newheight.model.TempProduct;

/**
 * <pre>
 * ProbeFactory.getProbe()测试
 * </pre>
 *
 * @author liuhongbo
 * @version $Id: TestProbe.java, v 0.1 2012-3-22 上午09:47:45 liuhongbo Exp $
 */
public class TestProbe {
    private static final Probe PROBE = ProbeFactory.getProbe();
    
    /**
     * 
     * <pre>
     * 从List的E中返回valueProp属性的List
     * </pre>
     *
     * @param <E>
     * @param list
     * @param propertyName
     * @return
     */
	public static <E> List<Object> GetPropertyList(List<E> list,String propertyName) {
    	List<Object> valueList = new ArrayList<Object>();
    	for (Object o : list) {
    		Object value = PROBE.getObject(o, propertyName);
    		valueList.add(value);
		}
    	return valueList;
    }
    
	/**
	 * 
	 * <pre>
	 * 从List的E中返回valueProp属性的List
	 * </pre>
	 *
	 * @param <E>
	 * @param list
	 * @param keyProp map中的的key值
	 * @param valueProp map中的value值,为null时,取对象本身
	 * @return
	 */
    public static <E> Map<Object,Object> ListforMap(List<E> list, String keyProp, String valueProp) {
        Map<Object,Object> map = new HashMap<Object,Object>();
        
        for (int i = 0, n = list.size(); i < n; i++) {
            Object object = list.get(i);
            if(null == object){
                continue;
            }
            Object key = PROBE.getObject(object, keyProp);
            Object value = null;
            if (valueProp == null || "".equals(valueProp)){
                value = object;
            } else {
                value = PROBE.getObject(object, valueProp);
            }
            map.put(key, value);
        }
        return map;
    }
	
    public static void main(String[] args) {
		TempProduct tp1 = new TempProduct();
		tp1.setId(789456L);
		TempProduct tp2 = new TempProduct();
		tp2.setId(789451L);
		
		TempProduct tp3 = new TempProduct();
		tp3.setId(789453L);
		List<TempProduct> array = new ArrayList<TempProduct>();
		array.add(tp1);
		array.add(tp2);
		array.add(tp3);
		List<Object> list1 = GetPropertyList(array,"id");
		for (Object o : list1) {
			System.out.println(o.toString());
		}
		
		System.out.println(ListforMap(array,"id",null));
    }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics