一:ORM框架Morphia的學習

Morphia 是一個針對Mongo和Java 對象轉換的映射的輕量級ORM類型安全類庫。

      1.簡單易用,輕量級,一旦每一種類型通過反射獲取將被緩存,性能比較好。

       2.Datastore和DAO<T,V>的抽象封裝。

      3.快速的查詢的支持,在類運行時進行校驗。

      4.Mapping是基於註解而不是基於xml。

      5.針對Validation和Log的擴展。

      6.生命週期的控制。

      7.可以和Spring,Guice等DI框架整合。

      8.支持各種功能的擴展。


在nosql的數據庫中,如果你用mongo api的驅動直接去開發,那個效率啊~~那個性能啊~~那個XX。。。

下面貼代碼,自己細細品味下,這樣寫好不好。當然,下面貼的代碼,是我網上找的。


Java代碼  收藏代碼
  1. package com.easyway.mongodb.morphia;  
  2.   
  3.   
  4. import java.net.UnknownHostException;  
  5. import java.util.Iterator;  
  6. import java.util.List;  
  7. import java.util.Set;  
  8.   
  9. import com.mongodb.BasicDBObject;  
  10. import com.mongodb.DB;  
  11. import com.mongodb.DBCollection;  
  12. import com.mongodb.DBCursor;  
  13. import com.mongodb.DBObject;  
  14. import com.mongodb.Mongo;  
  15. import com.mongodb.MongoException;  

  16. public class MongoApp {  
  17.   
  18.   
  19.         private Mongo m;  
  20.         private DB db;  
  21.   
  22.         public void init() {  
  23.   
  24.             try {  
  25.                 //創建一個Mongo對象  
  26.                 this.m = new Mongo("localhost");  
  27.                 System.out.println(m.debugString());  
  28.                   
  29.                 // // select a DB  
  30.                 // // The database doesn't have to exist - if it doesn't, MongoDB  
  31.                 // will create it for you.  
  32.                 //創建一個Mongo的db對象  
  33.                 this.db = m.getDB("test");  
  34.                 System.out.println("DB [" + db.getName() + "] Connected");  
  35.             } catch (UnknownHostException e) {  
  36.                 e.printStackTrace();  
  37.             } catch (MongoException e) {  
  38.                 e.printStackTrace();  
  39.             }  
  40.         }  
  41.         /** 
  42.          * 查詢數據庫和表的信息 
  43.          */  
  44.         public void queryDatabaseInfo(){  
  45.             System.out.println("mongodb version  "+m.getVersion());  
  46.             List<String> dbNameList=this.m.getDatabaseNames();  
  47.             for (String dbName : dbNameList) {  
  48.                 System.out.println(" "+dbName);  
  49.                 DB db=m.getDB(dbName);  
  50.                 Set<String> collectionList=db.getCollectionNames();  
  51.                 for (String collName : collectionList) {  
  52.                     System.out.print(collName +"  ");  
  53.                 }  
  54.             }  
  55.         }  
  56.           
  57.   
  58.         private void createIndex() {  
  59.             insertRecords();  
  60.             // MongoDB supports indexes, and they are very easy to add on a  
  61.             // collection. To create an index, you just specify the field that  
  62.             // should be indexed, and specify if you want the index to be ascending  
  63.             // (1) or descending (-1). The following creates an ascending index on  
  64.             // the "i" field :  
  65.             DBCollection dbconn = db.getCollection("things");  
  66.             dbconn.createIndex(new BasicDBObject("case", -1)); // create index on "i",  
  67.                                                             // ascending  
  68.               
  69.             Iterator<DBObject> iter=dbconn.getIndexInfo().iterator();  
  70.             while (iter.hasNext()) {  
  71.                 DBObject dbObject = (DBObject) iter.next();  
  72.                 System.out.println("Index Info:"+dbObject);;  
  73.             }  
  74.             removeAll();  
  75.         }  
  76.   
  77.         private void insertAndQuery() {  
  78.             insertRecords();  
  79.             query();  
  80.             removeAll();  
  81.         }  
  82.   
  83.         private void query() {  
  84.             System.out.println("start  query");  
  85.             DBCollection dbconn = db.getCollection("things");  
  86.             // looks like find by example  
  87.             BasicDBObject queryDb = new BasicDBObject();  
  88.             queryDb.append("case"5);  
  89.             DBCursor dbc = dbconn.find(queryDb);  
  90.             while (dbc.hasNext()) {  
  91.                 DBObject dbObject = (DBObject) dbc.next();  
  92.                 System.out.println("result case=5:" + dbObject);  
  93.             }  
  94.             System.out.println("end  query");  
  95.             // find in range  
  96.             BasicDBObject query = new BasicDBObject();  
  97.             query.put("case"new BasicDBObject("$gt"5)); // e.g. find all where  
  98.                                                             // case > 5  
  99.             DBCursor cur = dbconn.find(query);  
  100.             while (cur.hasNext()) {  
  101.                 System.out.println("case >5:" + cur.next());  
  102.             }  
  103.         }  
  104.   
  105.         private void insertFindOneRemove() {  
  106.             insertRecords();  
  107.             testFindOneInCollections();  
  108.             removeAll();  
  109.   
  110.         }  
  111.   
  112.         private void testFindOneInCollections() {  
  113.             System.out.println("Collections Find one:");  
  114.             DBCollection dbconn = db.getCollection("things");  
  115.             // declare that just one object is needed,null if none  
  116.             System.out.println(dbconn.findOne());  
  117.             System.out.println("Collections Find one end");  
  118.         }  
  119.   
  120.         private void insertFindAllRemove() {  
  121.             insertRecords();  
  122.             testFindAllInCollections();  
  123.             removeAll();  
  124.   
  125.         }  
  126.        //刪除所有的對象  
  127.         private void removeAll() {  
  128.             System.out.println("start remove all");  
  129.             DBCollection dbconn = db.getCollection("things");  
  130.             DBCursor c = dbconn.find();  
  131.             while (c.hasNext()) {  
  132.                 dbconn.remove(c.next());  
  133.             }  
  134.             System.out.println("done");  
  135.         }  
  136.           
  137.   
  138.         /** 
  139.          * 添加數據 
  140.          */  
  141.         public void insertRecords() {  
  142.             System.out.println("start insert :");  
  143.             DBCollection dbconn = db.getCollection("things");  
  144.             for (int i = 0; i < 10L; i++) {  
  145.                 // BasicDBObject basic=new BasicDBObject();  
  146.                 //  
  147.                 // basic.append("time",System.currentTimeMillis());  
  148.                 //  
  149.                 // basic.append("obj",i);  
  150.                 // dbconn.save(basic);  
  151.                 BasicDBObject doc = new BasicDBObject();  
  152.                 doc.put("case", i);  
  153.                 doc.put("name""MongoDB");  
  154.                 doc.put("type""database");  
  155.                 doc.put("count"1);  
  156.                 BasicDBObject info = new BasicDBObject();  
  157.                 info.put("x"203);  
  158.                 info.put("y"102);  
  159.                 doc.put("info", info);  
  160.                 dbconn.insert(doc);  
  161.             }  
  162.             System.out.println("Insert End");  
  163.         }  
  164.   
  165.         /** 
  166.          * 查詢所有 
  167.          */  
  168.         public void testFindAllInCollections() {  
  169.             System.out.println("Data in 'things':");  
  170.             DBCollection coll = db.getCollection("things");  
  171.   
  172.             DBCursor c = coll.find();  
  173.             while (c.hasNext()) {  
  174.                 System.out.println(c.next());  
  175.   
  176.             }  
  177.         }  
  178.           
  179.           
  180.         public static void main(String[] args) {  
  181.             MongoApp test=new MongoApp();  
  182.             test.init();  
  183.             test.queryDatabaseInfo();  
  184.               
  185.   
  186.             test.insertFindAllRemove();  
  187.             test.insertFindOneRemove();  
  188.             test.insertAndQuery();  
  189.             test.createIndex();  
  190.         }  
  191. }  

發佈了38 篇原創文章 · 獲贊 24 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章