標籤:
代碼僅供練習(反射,泛型):
package utils;import java.lang.reflect.Field;import com.mongodb.BasicDBObject;import com.mongodb.DBObject;import pojo.User;public class BeanFromDBObject { /** * @Description:bean-->DBObject * @param bean * @return DBObject 傳回型別 */ public static <T> DBObject getDBObject(T bean) { if (bean == null) { return null; } DBObject obj = new BasicDBObject(); Field[] field = bean.getClass().getDeclaredFields(); for (Field f : field) { String name = f.getName(); if (!f.isAccessible()) { f.setAccessible(true); } try { Object oj = f.get(bean); if (oj == null) { obj.put(name, ""); } else if (oj instanceof Integer) { int value = ((Integer) oj).intValue(); obj.put(name, value); } else if (oj instanceof Double) { Double value = ((Double) oj).doubleValue(); obj.put(name, value); } else if (oj instanceof Float) { Float value = ((Float) oj).floatValue(); obj.put(name, value); } else if (oj instanceof Boolean) { Boolean value = ((Boolean) oj).booleanValue(); obj.put(name, value); } else if (oj instanceof Long) { Long value = ((Long) oj).longValue(); obj.put(name, value); } else { obj.put(name, oj); } } catch (Exception e) { e.printStackTrace(); } } return obj; }}
MongoDB:實體物件(javabean)轉DBObject