日期:2014-05-16 浏览次数:20508 次
建议用一个数据库连接对象操作数据库,如果需要多个,可以初始化多个实例。
使用单例模式保持一个数据库连接:
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; } }驱动可以在我上传的资源中找到。