Mongo

Mongo Java Driver结合Morphia的使用

2016-02-15  本文已影响1266人  JohnShen

导入依赖

在pom文件中添加以下依赖,其中mongo-java-driver提供了java操作mongoDB的基本API,而morphia则是在其之上封装了Java对象和BSON对象的映射关系。

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.2.1</version>
</dependency>

<dependency>
    <groupId>org.mongodb.morphia</groupId>
    <artifactId>morphia</artifactId>
    <version>1.0.1</version>
</dependency>

构建Datastore对象

// mongo客户端用于连接服务端的地址串
String mongoClientURIString = 
    "mongodb://192.168.0.100,192.168.14.101/?replicaSet=test&readPreference=nearest";
MongoClientURI mongoClientURI = new MongoClientURI(mongoClientURIString);
// 构建出mongo客户端对象
MongoClient mongoClient = new MongoClient(mongoClientURI);
Morphia morphia = new Morphia();
// 告诉morphia到哪个包下面去找实体类
morphia.mapPackage("com.elong.web.gcCollection.model");
// 构建Datastore对象,其中`gcLog`是数据库名称
Datastore datastore = morphia.createDatastore(mongoClient, "gcLog");
// 确保索引生效
datastore.ensureIndexes();

为实体加上注解

通常一个实体映射为mongo中的一个collection,我们通过注解的方式告诉morphia这个映射关系。@Entity("gcLogItem")表示这个类对应mongo中的名为gcLogItem的collection;@Id表示这个属性对应主键字段。

注解的其它用法参见这里

@Entity("gcLogItem")
public class BaseGcItem {
    @Id
    private String id;

    private String host;

    /**
     * other fields go here ...
     */

    /**
     * getters and setters methods go here ...
     */
}

通过Datastore对象操作mongoDB

构建完Datastore对象以后就可以利用这个对象提供的API来操作mongoDB了。详细用法,请看这里

下面这些例子展示了一些用法:

@Repository
public class GcLogItemDaoImpl implements GcLogItemDao {

    @Autowired
    private DbClient dbClient;

    @Override
    public String insert(BaseGcItem gcItem) {
        Datastore datastore = dbClient.getDatastore();
        return (String)datastore.save(gcItem).getId();
    }

    @Override
    public Iterator<Key<BaseGcItem>> insert(List<BaseGcItem> gcItemList) {
        Datastore datastore = dbClient.getDatastore();
        return datastore.save(gcItemList).iterator();
    }

    @Override
    public List<BaseGcItem> getAll() {
        Query<BaseGcItem> query = dbClient.getDatastore()
            .createQuery(BaseGcItem.class);
        return query.asList();
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<String> getDistinctHost() {
        DBCollection dbCollection = dbClient.getDatastore()
            .getCollection(BaseGcItem.class);
        return (List<String>)dbCollection.distinct("host");
    }

    @Override
    public List<BaseGcItem> getByHost(String host) {
        Query<BaseGcItem> query = dbClient.getDatastore()
            .createQuery(BaseGcItem.class)
            .field("host").equal(host)
            .order("-timestampOfGc");
        return query.asList();
    }


    @Override
    public List<BaseGcItem> getByGcItemType(GcItemType gcItemType) {
        Query<BaseGcItem> query = dbClient.getDatastore()
            .createQuery(BaseGcItem.class)
            .field("gcItemType").equal(gcItemType)
            .order("-timestampOfGc");
        return query.asList();
    }

    @Override
    public int removeByDateRange(Date startDate, Date endDate) {
        Datastore datastore = dbClient.getDatastore();
        Query<BaseGcItem> query = datastore
            .createQuery(BaseGcItem.class)
            .field("timestampOfGc").greaterThanOrEq(startDate)
            .field("timestampOfGc").lessThan(endDate);
        return datastore.delete(query).getN();
    }

}
上一篇下一篇

猜你喜欢

热点阅读