Chan Chen Coding...

Morphia Java DAO

Morphia 地址:http://code.google.com/p/morphia/

教程:http://code.google.com/p/morphia/wiki/QuickStart

MongoDB 自己带的java API只能是保存 DBObject 对象的子类,类似BasicDBObject,如果对象有很多的字段,那是很繁琐的,需要一个个的put,地球人不爱干这样的事情,于是Morphia就出现了。

创建Meeting对象

 
package com.spell;

import java.util.Date;

import org.bson.types.ObjectId;

import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;

@Entity
//默认是要持久所有对象的
public class Meeting {
    @Id
    private ObjectId id;

    private static final long serialVersionUID = -4161545150796484674L;
    // 标题
    // @Transient //这个表示不持久,莫非
    private String title;
    // 地点
    private String place;
    // 时间
    private Date time;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public ObjectId getId() {
        return id;
    }

    public void setId(ObjectId id) {
        this.id = id;
    }
}      

 

用法示例:MeetingDaoTes.java

----------------------------------

package com.spell;

import java.util.Date;
import java.util.List;

import org.bson.types.ObjectId;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.mongodb.Mongo;

public class MeetingDaoTest {

    public static void main(String[] args) throws Exception {
        MeetingDaoTest test = new MeetingDaoTest();
        // test.save();
        test.queryList();
        // test.getByObjectId();
    }

    public static Datastore getDatastore() throws Exception {
        Mongo mongo = new Mongo("localhost", 27017);
        Morphia morphia = new Morphia();
        Datastore ds = morphia.createDatastore(mongo, "my_mongo", "spell",
                "007".toCharArray());
        return ds;
    }

    public void save() throws Exception {
        Datastore ds = MeetingDaoTest.getDatastore();
        Meeting m = new Meeting();
        m.setTime(new Date());
        m.setPlace("杭州");
        m.setTitle("游玩");
        ds.save(m);
        System.out.println("save success");
    }

    public void queryList() throws Exception {
        Datastore ds = MeetingDaoTest.getDatastore();
        List<Meeting> list = ds.find(Meeting.class).asList();

        /*
         * 也可以有更加高级的查询 List<Meeting> list =
         * ds.find(Meeting.class).field("place").endsWith("杭州").asList();
         */


        for (Meeting m : list) {
            System.out.println(m.getId() + " time:"
                    + m.getTime().toLocaleString());
        }

    }

    public void getByObjectId() throws Exception {
        Datastore ds = MeetingDaoTest.getDatastore();
        ObjectId id = new ObjectId("4d019b0e82ea26c308eea127");
        Meeting m = ds.get(Meeting.class, id);
        System.out.println(m.getTitle());
    }

}   



-----------------------------------------------------
Silence, the way to avoid many problems;
Smile, the way to solve many problems;

posted on 2012-04-14 01:04 Chan Chen 阅读(607) 评论(0)  编辑  收藏 所属分类: Scala / Java


只有注册用户登录后才能发表评论。


网站导航: