主頁(yè) > 知識(shí)庫(kù) > MongoDB操作類封裝實(shí)例代碼

MongoDB操作類封裝實(shí)例代碼

熱門標(biāo)簽:crm外呼系統(tǒng)聯(lián)系方式 長(zhǎng)沙電銷外呼防封卡是什么 內(nèi)蒙古營(yíng)銷智能外呼系統(tǒng)哪個(gè)好 呼和浩特外呼系統(tǒng)原理是什么 河南電話外呼系統(tǒng)招商 小裙科技電銷機(jī)器人怎樣 智能外呼系統(tǒng)官網(wǎng) 外呼線路資源屬于電信業(yè)務(wù)嗎 青白江400企業(yè)電話申請(qǐng)

前言

最近接到一個(gè)需求,要做MongoDB打點(diǎn)數(shù)據(jù)的統(tǒng)計(jì),在學(xué)習(xí)過MongoDB的操作之后,封裝了一個(gè)MongoDB的操作類,分為兩部分,基本思想是參照了自己寫過的mysql的操作類。一個(gè)是基本的操作類,包括所有基本操作的靜態(tài)方法,還有一個(gè)是mongoobject,就是具體操作的實(shí)現(xiàn)類。

以后再寫如何用spring boot寫一個(gè)簡(jiǎn)單的統(tǒng)計(jì)服務(wù)。

MongoDB操作類封裝

mongobase代碼如下:

package com.fun.mongodb;
 
import com.fun.frame.SourceCode;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
 
import java.util.Arrays;
import java.util.List;
 
/**
 * mongo操作類的基礎(chǔ)類
 */
public class MongoBase extends SourceCode {
 
 /**
  * 獲取服務(wù)地址list
  *
  * @param addresses
  * @return
  */
 public static ListServerAddress> getServers(ServerAddress... addresses) {
  return Arrays.asList(addresses);
 }
 
 /**
  * 獲取服務(wù)地址
  *
  * @param host
  * @param port
  * @return
  */
 public static ServerAddress getServerAdress(String host, int port) {
  return new ServerAddress(host, port);
 }
 
 /**
  * 獲取認(rèn)證list
  *
  * @param credentials
  * @return
  */
 public static ListMongoCredential> getCredentials(MongoCredential... credentials) {
  return Arrays.asList(credentials);
 }
 
 /**
  * 獲取驗(yàn)證
  *
  * @param userName
  * @param database
  * @param password
  * @return
  */
 public static MongoCredential getMongoCredential(String userName, String database, String password) {
  return MongoCredential.createCredential(userName, database, password.toCharArray());
 }
 
 /**
  * 獲取mongo客戶端
  *
  * @param addresses
  * @param credentials
  * @return
  */
 public static MongoClient getMongoClient(ListServerAddress> addresses, ListMongoCredential> credentials) {
  return new MongoClient(addresses, credentials);
 }
 
 /**
  * 連接mongo數(shù)據(jù)庫(kù)
  *
  * @param mongoClient
  * @param databaseName
  * @return
  */
 public static MongoDatabase getMongoDatabase(MongoClient mongoClient, String databaseName) {
  return mongoClient.getDatabase(databaseName);
 }
 
 /**
  * 連接mongo集
  *
  * @param mongoDatabase
  * @param collectionName
  * @return
  */
 public static MongoCollectionDocument> getMongoCollection(MongoDatabase mongoDatabase, String collectionName) {
  return mongoDatabase.getCollection(collectionName);
 }
 
 /**
  * 關(guān)閉數(shù)據(jù)庫(kù)連接
  *
  * @param mongoClient
  */
 public static void MongoOver(MongoClient mongoClient) {
  mongoClient.close();
 }
 
 /**
  * 獲取mongo客戶端對(duì)象,通過servers和credentials對(duì)象創(chuàng)建
  *
  * @param mongoObject
  * @return
  */
 public static MongoClient getMongoClient(MongoObject mongoObject) {
  MongoClient mongoClient = new MongoClient(getServers(getServerAdress(mongoObject.host, mongoObject.port)), getCredentials(getMongoCredential(mongoObject.user, mongoObject.database, mongoObject.password)));
  return mongoClient;
 }
 
 /**
  * 獲取mongo客戶端對(duì)象,通過uri方式連接
  *
  * @param mongoObject
  * @return
  */
 public static MongoClient getMongoClientOnline(MongoObject mongoObject) {
  String format = String.format("mongodb://%s:%s@%s:%d/%s", mongoObject.user, mongoObject.password, mongoObject.host, mongoObject.port, mongoObject.database);
  return new MongoClient(new MongoClientURI(format));
 }
 
 /**
  * 獲取collection對(duì)象
  *
  * @param mongoObject
  * @return
  */
 public static MongoCollectionDocument> getCollection(MongoObject mongoObject, String collectionName) {
  return getMongoClient(mongoObject).getDatabase(mongoObject.database).getCollection(collectionName);
 }
 
 /**
  * 獲取collection對(duì)象
  *
  * @param mongoObject
  * @return
  */
 public static MongoCollectionDocument> getCollectionOnline(MongoObject mongoObject, String collectionName) {
  return getMongoClientOnline(mongoObject).getDatabase(mongoObject.database).getCollection(collectionName);
 }
 
}

mongoobject的代碼如下:

package com.fun.mongodb;
 
 
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
 
/**
 * mongo數(shù)據(jù)庫(kù)配置對(duì)象,針對(duì)單個(gè)數(shù)據(jù)服務(wù),單個(gè)身份驗(yàn)證
 */
public class MongoObject extends MongoBase {
 
  String host;
 
  int port;
 
  String user;
 
  String password;
 
  String database;
 
  MongoClient mongoClient;
 
  /**
   * 創(chuàng)建數(shù)據(jù)連接
   *
   * @param host
   * @param port
   * @param user
   * @param password
   * @param database
   */
  public MongoObject(String host, int port, String user, String password, String database) {
    this.host = host;
    this.port = port;
    this.user = user;
    this.password = password;
    this.database = database;
    this.mongoClient = getMongoClient(this);
  }
 
  /**
   * 創(chuàng)建數(shù)據(jù)庫(kù)連接
   *
   * @param port
   * @param host
   * @param user
   * @param password
   * @param database
   */
  public MongoObject(int port, String host, String user, String password, String database) {
    this.host = host;
    this.port = port;
    this.user = user;
    this.password = password;
    this.database = database;
    this.mongoClient = getMongoClientOnline(this);
  }
 
  /**
   * 獲取colletion對(duì)象
   *
   * @param collectionName
   * @return
   */
  public MongoCollectionDocument> getMongoCollection(String collectionName) {
    MongoClient mongoClientOnline = getMongoClientOnline(this);
    return mongoClientOnline.getDatabase(database).getCollection(collectionName);
  }
 
 
  /**
   * 關(guān)閉連接
   */
  public void over() {
    MongoOver(this.mongoClient);
  }
}
 @Override
  public MongoObject clone() {
    return new MongoObject(this.host, this.port, this.user, this.password, this.database);
  }

  public MongoObject clone2() {
    return new MongoObject(this.port, this.host, this.user, this.password, this.database);
  }

具體效果非常不錯(cuò),測(cè)試代碼如下:

 public static void main(String[] args) {
    MongoObject ready = new MongoObject("*****", 5117, "fission_record", "fission_record", "fission_record");
    MongoCollectionDocument> app = ready.getMongoCollection("app_logs_20181109");
    Document first = app.find().first();
    output(first);
    ready.over();
  }

打印內(nèi)容如下:

Document{{_id=5be4ce052ce01b21b6c26a64, _class=com.fission.next.record.bean.AppRecordBean, user_id=5482, action_type={"gameId":2,"userId":"5482"}, action_extern=DataSta_Game_Starts, client_version=15, client_ip=114.5.146.239, client_imei=UNKNOWN, client_dev=xiaomi-Redmi 5 Plus, client_type=200, server_time=1541721601655, os_name=200, os_version=15, client_time=1541721577025}}

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。

您可能感興趣的文章:
  • php封裝的mongodb操作類代碼
  • PHP實(shí)現(xiàn)的MongoDB數(shù)據(jù)庫(kù)操作類分享
  • php實(shí)現(xiàn)的mongodb操作類
  • PHP實(shí)現(xiàn)的mongoDB數(shù)據(jù)庫(kù)操作類完整實(shí)例
  • php實(shí)現(xiàn)的mongodb操作類實(shí)例
  • php實(shí)現(xiàn)的mongoDB單例模式操作類
  • php mongodb操作類 帶幾個(gè)簡(jiǎn)單的例子
  • PHP mongodb操作類定義與用法示例【適合mongodb2.x和mongodb3.x】

標(biāo)簽:菏澤 安順 楚雄 池州 呼倫貝爾 白山 黃石 舟山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MongoDB操作類封裝實(shí)例代碼》,本文關(guān)鍵詞  MongoDB,操作,類,封裝,實(shí)例,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MongoDB操作類封裝實(shí)例代碼》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于MongoDB操作類封裝實(shí)例代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章