public class MyApp {
private MyApp() {
}
public static MyApp getInstance() {
return MyAppHolder.INSTANCE;
}
private static class MyAppHolder {
private static final MyApp INSTANCE = new MyApp();
}
@Getter @Setter
String dbName;
@Setter
MongoClient client;
public DB getDB() {
return client.getDB(dbName);
}
}
后以在任何地方要需应用连接的时候,这样应用:
public static void bind(final String address, final String userId) {
DB db = MyApp.getInstance().getDB();
DBCollection collection = db.getCollection(DISPLAY_COLLECTION);
DBObject condition = new BasicDBObject();
condition.put("address", address);
DBObject field = new BasicDBObject();
field.put("user_id", new ObjectId(userId));
DBObject set = new BasicDBObject();
set.put("$set", field);
collection.update(condition, set, false, false);
}
很简单吧。
文章结束给大家分享下程序员的一些笑话语录: 《诺基亚投资手机浏览器UCWEB,资金不详或控股》杯具了,好不容易养大的闺女嫁外国。(心疼是你养的吗?中国创业型公司创业初期哪个从国有银行贷到过钱?)