将pojo转换成Map,直接构造mongodb bsonobject

2014-11-24 08:59:09 · 作者: · 浏览: 1
package com.skmbw.yinlei.mongo;  
  
import java.util.Date;  
import java.util.HashMap;  
import java.util.Map;  
import java.util.Map.Entry;  
import java.util.concurrent.ConcurrentHashMap;  
import java.util.concurrent.ConcurrentMap;  
  
import org.apache.commons.lang3.StringUtils;  
import org.springframework.cglib.beans.BeanMap;  
  
import com.alibaba.fastjson.JSON;  
import com.alibaba.fastjson.JSONObject;  
import com.esotericsoftware.reflectasm.MethodAccess;  
  
public class JSONUtils {  
    public static ConcurrentMap beanMapCache = new ConcurrentHashMap();  
      
    //JSONObject就是个Map,它实现了Map接口  
    public static Map toMap(Object object) {  
        JSONObject jsonObject = (JSONObject)JSON.toJSON(object);  
        return jsonObject;  
    }  
      
    //去掉null  
    public static Map toMaps(Object object) {  
        Map map = new HashMap();  
        JSONObject jsonObject = (JSONObject) JSON.toJSON(object);  
        for (Entry entry : jsonObject.entrySet()) {  
            if (entry.getValue() != null) {  
                map.put(entry.getKey(), entry.getValue());  
            }  
        }  
        return map;  
    }  
      
    public static void main(String[] aa) {  
        User user = new User();  
        user.setAccount("asdf");  
        user.setAge(22);  
        user.setDate(new Date());  
        Map result = new HashMap();  
        Map result2 = new HashMap();  
          
        long d2 = System.nanoTime();  
        JSONObject jsonObject = (JSONObject)JSON.toJSON(user);  
        String json = jsonObject.toJSONString();  
        Map jsonMap = JSON.parseObject(json);  
        System.out.println(System.nanoTime() - d2);  
          
          
        long dd = System.nanoTime();  
        Map map = (Map)jsonObject;  
        result.putAll(map);  
        System.out.println(System.nanoTime() - dd);  
        long d = System.nanoTime();  
        for (Entry
entry : jsonObject.entrySet()) { if (entry.getValue() != null) { result2.put(entry.getKey(), entry.getValue()); } } System.out.println(System.nanoTime() - d); Map toMap = new HashMap(); beanToMap(user, toMap); Map toMap2 = beanToMap(user); System.out.println(toMap2); } public static void beanToMap(Object fromBean, Map toMap) { //MethodAccess要缓存 MethodAccess methodAccess = MethodAccess.get(fromBean.getClass()); String[] methodNames = methodAccess.getMethodNames(); for (String methodName : methodNames){ if (methodName.startsWith("get")){ Object value = methodAccess.invoke(fromBean, methodName, (Object[])null); toMap.put(StringUtils.uncapitalize(methodName.substring(3)), value); } } } public static BeanMap getBeanMap(Object object) { BeanMap beanMap = beanMapCache.get(object.getClass().getName()); if (beanMap == null) { beanMap = BeanMap.create(object); beanMapCache.put(object.getClass().getName(), beanMap); } return beanMap; } //如果使用BeanMap缓存,这个性能最好。 public static Map beanToMap(Object object) { BeanMap beanMap = getBeanMap(object); beanMap.setBean(object); @SuppressWarnings("unchecked") Map toMap = beanMap; for (Entry entry : toMap.entrySet()) { if (entry.getValue() != null) { toMap.put(entry.getKey(), entry.getValue()); } } return toMap; } }

因为mongodb的bosnobject,有一个putAll(Map)的方法,可以快速构建BsonObject。