POJO to org.bson.Document和Vice Versa [英] POJO to org.bson.Document and Vice Versa

查看:119
本文介绍了POJO to org.bson.Document和Vice Versa的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法可以将Simple POJO转换为org.bson.Document?

Is there a simple way to convert Simple POJO to org.bson.Document?

我知道有很多方法可以像这样做:

I'm aware that there are ways to do this like this one:

Document doc = new Document();
doc.append("name", person.getName()):

但是它有一个更简单和更错误的方式?

But does it have a much simpler and typo less way?

推荐答案

重点是,你不需要把手放在org上.bson.Document。

The point is, that you do not need to put your hands on org.bson.Document.

Morphia会在幕后为你做所有这些。

Morphia will do all that for you behind the curtain.

import com.mongodb.MongoClient;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.DatastoreImpl;
import org.mongodb.morphia.Morphia;
import java.net.UnknownHostException;

.....
    private Datastore createDataStore() throws UnknownHostException {
        MongoClient client = new MongoClient("localhost", 27017);
        // create morphia and map classes
        Morphia morphia = new Morphia();
        morphia.map(FooBar.class);
        return new DatastoreImpl(morphia, client, "testmongo");
    }

......

    //with the Datastore from above you can save any mapped class to mongo
    Datastore datastore;
    final FooBar fb = new FooBar("hello", "world");
    datastore.save(fb);

这里有几个例子: https://mongodb.github.io/morphia/

这篇关于POJO to org.bson.Document和Vice Versa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆