使用MongoDB Java驱动程序将DBObject转换为POJO [英] Convert DBObject to a POJO using MongoDB Java Driver
问题描述
MongoDB似乎返回BSON / JSON对象。
MongoDB seems to return BSON/JSON objects.
我认为你肯定可以检索字符串,整数等值,然后可以保存作为POJO。
I thought that surely you'd be able to retrieve values as Strings, ints etc. which can then be saved as POJO.
我有一个DBObject(实例化为BasicDBObject)作为迭代列表的结果......(cur.next())。
I have a DBObject (instantiated as a BasicDBObject) as a result of iterating over a list ... (cur.next()).
除了使用某种持久性框架之外,唯一的方法是将数据放入POJO以使用JSON serlialiser / deserialiser吗?
Is the only way (other than using some sort of persistence framework) to get the data into a POJO to use a JSON serlialiser/deserialiser?
我的方法如下所示:
public List<User> findByEmail(String email){
DBCollection userColl;
try {
userColl = Dao.getDB().getCollection("users"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace();}
DBCursor cur = userColl.find();
List<User> usersWithMatchEmail = new ArrayList<User>();
while(cur.hasNext()) {
// this is where I want to convert cur.next() into a <User> POJO
usersWithMatchEmail.add(cur.next());
}
return null;
}
编辑:很明显,只需做这样的事情。
It's pretty obvious, just do something like this.
推荐答案
有一些java库可以帮助你:
There is a few java libs that can help you with it:
- Morhpia - http://code.google.com/p/morphia/
- MongoDB的Spring数据 - http:// www .springsource.org / spring-data / mongodb
- Morhpia - http://code.google.com/p/morphia/
- Spring Data for MongoDB - http://www.springsource.org/spring-data/mongodb
这篇关于使用MongoDB Java驱动程序将DBObject转换为POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!