JAva操作mongodbの追加、削除、変更、検索操作.

3990 ワード

1つのデータベースでオブジェクトを接続してデータベースを操作することをお勧めします.複数のインスタンスが必要な場合は、複数のインスタンスを初期化できます.
シングル・インスタンス・モードを使用してデータベース接続を維持するには、次の手順に従います.
public class MongoDb {
	
	private static MongoDb instance;
	
	private DB db;
	
	private MongoDb()
	{
		//MongoClient mongoClient = new MongoClient();
		//MongoClient mongoClient = new MongoClient( "localhost" );
		//MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
		MongoClient mongoClient;
		try {
			mongoClient = new MongoClient("192.168.0.228", 27017);
			//mongoClient = new MongoClient(Arrays.asList(new ServerAddress("192.168.0.228", 27017)));
			db = mongoClient.getDB("simulator");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
	
	public static MongoDb getInstance()
	{
		if(instance == null)
		{
			instance = new MongoDb();
		}
		return instance;
	}
	
	public DB getDb()
	{
		return this.db;
	}
}
DAOレイヤのコードは次のとおりです.
public class PlanDao {
	
	/**
	 *       
	 * @param plan
	 */
	public ObjectId add(Plan plan)
	{
		DB db = MongoDb.getInstance().getDb();
		DBCollection coll = db.getCollection(SimulatorConstant.COLL_PLAN);
		DBObject object = DBObjectUtil.getDBObjectByPlan(plan);
		coll.insert(object);
		return (ObjectId)object.get("_id");
	}
	
	/**
	 *   ID    plan  
	 * @param id
	 */
	public Plan getById(String id)
	{
		DB db = MongoDb.getInstance().getDb();
		DBCollection coll = db.getCollection(SimulatorConstant.COLL_PLAN);
		DBObject object = coll.findOne(new BasicDBObject("_id", new ObjectId(id)));
		return DBObjectUtil.getPlanByDBObject(object);
	}
	
	/**
	 *       
	 * @param id
	 */
	public void delete(String id)
	{
		DB db = MongoDb.getInstance().getDb();
		DBCollection coll = db.getCollection(SimulatorConstant.COLL_PLAN);
		coll.remove(new BasicDBObject("_id", new ObjectId(id)));
	}
	
	/**
	 *      plan  
	 * @return
	 */
	public List<Plan> getAll()
	{
		List<Plan> planList = new ArrayList<Plan>();
		DB db = MongoDb.getInstance().getDb();
		DBCollection coll = db.getCollection(SimulatorConstant.COLL_PLAN);
		DBCursor cursor = coll.find();
		try {
		   while(cursor.hasNext()) {
			   DBObject object = cursor.next();
			   planList.add(DBObjectUtil.getPlanByDBObject(object));
		   }
		} finally {
		   cursor.close();
		}
		return planList;
	}
	
	/**
	 *       
	 * @param plan
	 */
	public void modify(Plan plan)
	{
		DB db = MongoDb.getInstance().getDb();
		DBCollection coll = db.getCollection(SimulatorConstant.COLL_PLAN);
		coll.update(new BasicDBObject("_id", new ObjectId(plan.getId())), DBObjectUtil.getDBObjectByPlan(plan));
	}
}
SimulatorConstant.COLL_PLAN
はcollectionの名前です.
Modelレイヤのコードは次のとおりです.
public class Plan {
	//   id
	private String id;
	//    
	private String name;

	public Plan()
	{
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getId() {
		return id;
	}

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

ツール・クラス:モデル・レイヤとデータベースの対応を実現します.
/**
 * @author hadoop
 *
 */
public class DBObjectUtil {
	
	/**
	 *   Plan  BasicDBObject  
	 * @param mac
	 */
	public static BasicDBObject getDBObjectByPlan(Plan plan)
	{
		ObjectId id;
		String sId = plan.getId();
		if(sId == null || sId.length() != 24)
		{
			id = new ObjectId();
		} else {
			id = new ObjectId(sId);
		}
		BasicDBObject doc = new BasicDBObject("_id", id);
		doc.append("name", plan.getName());
		return doc;
	}
	
	/**
	 *   BasicDBObject  Plan  
	 * @param object
	 */
	public static Plan getPlanByDBObject(DBObject object)
	{
		Plan plan = new Plan();
		plan.setId(((ObjectId)object.get("_id")).toString());
		plan.setName((String)object.get("name"));
		return plan;
	}
}
ドライバは、アップロードしたリソースで見つけることができます.