日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

mongodb与java结合_MongoDB初探系列之四:MongoDB与Java共舞

發(fā)布時(shí)間:2024/4/18 java 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mongodb与java结合_MongoDB初探系列之四:MongoDB与Java共舞 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

MongoDB初探系列之四:MongoDB與Java共舞

來(lái)源:互聯(lián)網(wǎng)

作者:佚名

時(shí)間:2015-08-05 08:20

對(duì)各位注意到這個(gè)帖子的朋友說(shuō)一聲對(duì)不起,我不是故意的測(cè)試服務(wù)器一直沒(méi)關(guān),一忙就給忘了刪了.....如果有管理員看到,可以把這個(gè)閱讀數(shù)什么的清零,謝謝

MongoDB初探系列之四:MongoDB與Java共舞

分類:

MongoDB初探

由于版本不同,可能API也有所不同,,本次學(xué)習(xí)用的是3.0版本。

1、使用的mongodb的jdbc驅(qū)動(dòng)版本為:mongo-java-driver-3.0.0.jar

2、本節(jié)只是簡(jiǎn)單介紹JDBC操作,暫時(shí)不考慮效率問(wèn)題。

3、封裝的工具類代碼如下:

public class MongoDBProxy {

private static MongoDBProxy proxy=null;//單實(shí)例

private static MongoDatabase db=null;//數(shù)據(jù)庫(kù)連接對(duì)象

private static String [] paramArray=new String[5];//數(shù)據(jù)庫(kù)連接參數(shù)

private MongoDBProxy(){

}

static{

paramArray[0]="username";

paramArray[1]="password";

paramArray[2]="host";

paramArray[3]="port";

paramArray[4]="databaseName";

}

/**

* 得到MongoDBProxy

* 采用系統(tǒng)默認(rèn)配置

*/

public static MongoDBProxy getMongoDBProxy(){

if(proxy==null){

proxy=new MongoDBProxy();

String sURI = String.format("mongodb://%s:%s@%s:%d/%s",paramArray[0],paramArray[1],paramArray[2],Integer.parseInt(paramArray[3]),paramArray[4]);

MongoClientURI uri = new MongoClientURI(sURI);

MongoClient mongoClient = new MongoClient(uri);

db= mongoClient.getDatabase(paramArray[4]);

}

return proxy;

}

/**

* 批量查詢數(shù)據(jù)

* @param table 集合名稱

* @param page 分頁(yè)參數(shù)

* @param filter 過(guò)濾條件

* @param sort 排序條件

*/

public Page findDocList(String table,Page page,Bson filter,Bson sort){

MongoCollection coll=db.getCollection(table);

long count=coll.count(filter);//根據(jù)過(guò)濾條件獲取數(shù)據(jù)總量

Page p=PageUtil.createPage(page,Integer.parseInt(String.valueOf(count)));

p.setFromUrl((page.getFromUrl()==null)?"":page.getFromUrl());

p.setParamString((page.getParamString()==null)?"":page.getParamString());

FindIterable resultIterable=coll.find();

//執(zhí)行條件過(guò)濾

resultIterable=resultIterable.sort(sort).filter(filter).skip(p.getBeginIndex()).batchSize(p.getEveryPage());

MongoCursor cousor=resultIterable.iterator();

List dataList=new ArrayList();

while(cousor.hasNext()){

dataList.add(cousor.next());

}

p.setDataList(dataList);

return PageUtil.buildPageString(p);

}

/**

* 獲取單個(gè)文檔

* @param table 集合名稱

* @param filter 過(guò)濾條件

* @param sort 排序條件

*/

public Document findOneDoc(String table,Bson filter,Bson sort){

MongoCollection coll=db.getCollection(table);

FindIterable resultIterable=coll.find();

if(sort!=null){

resultIterable.sort(sort);

}

if(filter!=null){

resultIterable.filter(filter);

}

return resultIterable.first();

}

/**

* 添加文檔

* @param table 集合名稱

* @prama doc 文檔內(nèi)容

*/

public void addDocument(String table,Document doc){

MongoCollection coll=getCollection(table);

coll.insertOne(doc);

}

/**

* 批量添加文檔

* @param table

集合名稱

* @prama docList 文檔集合

*/

public void addDocumentList(String table,List docList){

MongoCollection coll=getCollection(table);

coll.insertMany(docList);

}

/**

* 更新文檔

* @param table 集合名稱

* @param query 查詢條件

* @param up

更新數(shù)據(jù)

*/

public UpdateResult updateDocument(String table,Bson query,Bson up){

MongoCollection coll=getCollection(table);

return coll.updateOne(query,up);

}

/**

* 替換文檔

* @param table 集合名稱

* @param query 查詢條件

* @param up

替換的文件對(duì)象

*/

public UpdateResult replaceDocument(String table,Bson query,Document up){

MongoCollection coll=getCollection(table);

return coll.replaceOne(query, up);

}

/**

* 刪除文檔

* @param table 集合名稱

* @param delete 刪除條件

*/

public DeleteResult deleteDocument(String table,Bson delete){

MongoCollection coll=getCollection(table);

return coll.deleteOne(delete);

}

/**

* 獲取集合對(duì)象

* @param table 集合名稱

*/

private MongoCollection getCollection(String table){

return db.getCollection(table);

}

}

4、調(diào)用demo

MongoDBProxy proxy=MongoDBProxy.getMongoDBProxy();

System.out.println(proxy.findOneDoc("users",null,null).get("_id"));

Document doc=new Document();

doc.put("user","李四");

proxy.addDocument("users", doc);

Bson bson=new BasicDBObject("user","張三");

proxy.deleteDocument("users", bson);

后續(xù)再深入學(xué)習(xí),先用demo上上手哇,哈哈。

版權(quán)聲明:轉(zhuǎn)載請(qǐng)注明博文地址,尊重作者勞動(dòng)成果。歡迎關(guān)注,一起成長(zhǎng)。

上一篇MongoDB初探系列之三:MongoDB用戶權(quán)限操作

0

0

總結(jié)

以上是生活随笔為你收集整理的mongodb与java结合_MongoDB初探系列之四:MongoDB与Java共舞的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。