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 閱讀(629) 評論(0)  編輯  收藏 所屬分類: Scala / Java

          主站蜘蛛池模板: 长寿区| 武鸣县| 通化县| 治县。| 兴国县| 子长县| 虞城县| 东宁县| 石棉县| 乌审旗| 泰宁县| 邵阳县| 乌苏市| 天全县| 民勤县| 任丘市| 乌拉特后旗| 鹤山市| 麻江县| 会同县| 雷波县| 五莲县| 杭锦后旗| 会昌县| 兴隆县| 吴旗县| 华安县| 包头市| 龙川县| 清河县| 普兰县| 巨鹿县| 峡江县| 嵩明县| 三河市| 高密市| 五指山市| 敦煌市| 赤峰市| 兰坪| 阳泉市|