![](https://csdnimg.cn/release/download_crawler_static/22663893/bg5.jpg)
}
}
工具类,将带有 getter、setter 方法的 Java 类序列化成 DBObject 对象
package com.hoo.util;
import java.lang.reflect.Method;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
/**
*<b>function:</b>将Entity/Model转换成DBObject
*@authorhoojo
*@createDate2011-5-30下午01:53:08
*@fileBasicDBObjectUtil.java
*@packagecom.hoo.util
*@projectMongoDB
*@bloghttp://blog.csdn.net/IBM_hoojo
*@emailhoojo_@126.com
*@version1.0
*/
publicclass BasicDBObjectUtils {
publicstatic<T> DBObject castModel2DBObject(T entity) throws
Method[] method = entity.getClass().getMethods();
DBObject dbObject = new BasicDBObject();
for (Method m : method) {
//System.out.println(m.getName());
if (m.getName().startsWith("get")) {
String name = m.getName().replace("get", "");
for (Method m2 : method) {
}
if (m2.getName().equals("set" + name)) {
}
name = name.substring(0, 1).toLowerCase() +
Object returnVal = m.invoke(entity, new Object[]
if (returnVal != null) {
}
//System.out.println(name + " : " +
dbObject.put(name, returnVal);
Exception {
name.substring(1);
{});
m.invoke(shipping, new Object[] {}));