mongodb - Java API 첨삭 수정 작업

5826 단어
package com.x.mongodb;  
  
import java.net.UnknownHostException;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
import org.bson.types.ObjectId;  
  
import com.mongodb.BasicDBObject;  
import com.mongodb.DB;  
import com.mongodb.DBCollection;  
import com.mongodb.DBObject;  
import com.mongodb.Mongo;  
import com.mongodb.MongoException;  
  
  
public final class MongoDBUtil {  
  
    private static final String HOST = "127.0.0.1";  
  
    private static final String dbName = "test";  
  
    private static Mongo mongo;  
  
    private static DB db;  
  
    static {  
        try {  
            mongo = new Mongo(HOST);  
            db = mongo.getDB(dbName);  
            // db.authenticate(username, passwd)  
        } catch (UnknownHostException e) {  
            e.printStackTrace();  
        } catch (MongoException e) {  
            e.printStackTrace();  
        }  
    }  
  
    private MongoDBUtil() {  
    }  
  
    /** 
     *      
     * <br>------------------------------<br> 
     * @param map 
     * @param collectionName 
     */  
    public static void add(Map<String, Object> map, String collectionName) {  
        DBObject dbObject = new BasicDBObject(map);  
        getCollection(collectionName).insert(dbObject);  
    }  
      
    /** 
     *      
     * <br>------------------------------<br> 
     * @param list 
     * @param collectionName 
     */  
    public static void add(List<Map<String, Object>> list, String collectionName) {  
        for (Map<String, Object> map : list) {  
            add(map, collectionName);  
        }  
    }  
      
    /** 
     *      
     * <br>------------------------------<br> 
     * @param map 
     * @param collectionName 
     */  
    public static void delete(Map<String, Object> map, String collectionName) {  
        DBObject dbObject = new BasicDBObject(map);  
        getCollection(collectionName).remove(dbObject);  
    }  
      
    /** 
     *     ,     
     * <br>------------------------------<br> 
     * @param id             
     * @param collectionName 
     */  
    public static void delete(String id, String collectionName) {  
        Map<String, Object> map = new HashMap<String, Object>();  
        map.put("_id", new ObjectId(id));  
        delete(map, collectionName);  
    }  
      
    /** 
     *      
     * <br>------------------------------<br> 
     * @param collectionName 
     */  
    public static void deleteAll(String collectionName) {  
        getCollection(collectionName).drop();  
    }  
      
    /** 
     *     </br> 
     *              ,  key       </br> 
     *      {"_id":"123","name":"zhangsan","age":12}   _id  age  
     * value {"age":12}     name    ,        
     * <br>------------------------------<br> 
     * @param whereMap       
     * @param valueMap       
     * @param collectionName 
     */  
    public static void update(Map<String, Object> whereMap, Map<String, Object> valueMap, String collectionName) {  
        executeUpdate(collectionName, whereMap, valueMap, new UpdateCallback(){  
            public DBObject doCallback(DBObject valueDBObject) {  
                return valueDBObject;  
            }  
        });  
    }   
      
    /** 
     *     ,  $set   </br> 
     *         ,      ,     ,       ,       ,          
     * <br>------------------------------<br> 
     * @param whereMap       
     * @param valueMap       
     * @param collectionName 
     */  
    public static void updateSet(Map<String, Object> whereMap, Map<String, Object> valueMap, String collectionName) {  
        executeUpdate(collectionName, whereMap, valueMap, new UpdateCallback(){  
            public DBObject doCallback(DBObject valueDBObject) {  
                return new BasicDBObject("$set", valueDBObject);  
            }  
        });  
    }   
      
    /** 
     *     ,  $inc   </br> 
     *            </br> 
     *              ,          
     * <br>------------------------------<br> 
     * @param whereMap       
     * @param valueMap       
     * @param collectionName 
     */  
    public static void updateInc(Map<String, Object> whereMap, Map<String, Integer> valueMap, String collectionName) {  
        executeUpdate(collectionName, whereMap, valueMap, new UpdateCallback(){  
            public DBObject doCallback(DBObject valueDBObject) {  
                return new BasicDBObject("$inc", valueDBObject);  
            }  
        });  
    }   
      
    /** 
     *    
     * <br>------------------------------<br> 
     * @param collectionName 
     * @param whereMap 
     * @param valueMap 
     * @param updateCallback 
     */  
    private static void executeUpdate(String collectionName, Map whereMap, Map valueMap, UpdateCallback updateCallback) {  
        DBObject whereDBObject = new BasicDBObject(whereMap);  
        DBObject valueDBObject = new BasicDBObject(valueMap);  
        valueDBObject = updateCallback.doCallback(valueDBObject);  
        getCollection(collectionName).update(whereDBObject, valueDBObject);  
    }  
      
    interface UpdateCallback {  
          
        DBObject doCallback(DBObject valueDBObject);  
    }  
      
    /** 
     *     ( ) 
     * <br>------------------------------<br> 
     * @param collectionName 
     * @return 
     */  
    public static DBCollection getCollection(String collectionName) {  
        return db.getCollection(collectionName);  
    }  
}

좋은 웹페이지 즐겨찾기