欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MongoDB操作类封装实例代码

程序员文章站 2022-11-23 20:15:59
前言 最近接到一个需求,要做mongodb打点数据的统计,在学习过mongodb的操作之后,封装了一个mongodb的操作类,分为两部分,基本思想是参照了自己写过的mys...

前言

最近接到一个需求,要做mongodb打点数据的统计,在学习过mongodb的操作之后,封装了一个mongodb的操作类,分为两部分,基本思想是参照了自己写过的mysql的操作类。一个是基本的操作类,包括所有基本操作的静态方法,还有一个是mongoobject,就是具体操作的实现类。

以后再写如何用spring boot写一个简单的统计服务。

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操作类的基础类
 */
public class mongobase extends sourcecode {
 
 /**
  * 获取服务地址list
  *
  * @param addresses
  * @return
  */
 public static list<serveraddress> getservers(serveraddress... addresses) {
  return arrays.aslist(addresses);
 }
 
 /**
  * 获取服务地址
  *
  * @param host
  * @param port
  * @return
  */
 public static serveraddress getserveradress(string host, int port) {
  return new serveraddress(host, port);
 }
 
 /**
  * 获取认证list
  *
  * @param credentials
  * @return
  */
 public static list<mongocredential> getcredentials(mongocredential... credentials) {
  return arrays.aslist(credentials);
 }
 
 /**
  * 获取验证
  *
  * @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(list<serveraddress> addresses, list<mongocredential> credentials) {
  return new mongoclient(addresses, credentials);
 }
 
 /**
  * 连接mongo数据库
  *
  * @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 mongocollection<document> getmongocollection(mongodatabase mongodatabase, string collectionname) {
  return mongodatabase.getcollection(collectionname);
 }
 
 /**
  * 关闭数据库连接
  *
  * @param mongoclient
  */
 public static void mongoover(mongoclient mongoclient) {
  mongoclient.close();
 }
 
 /**
  * 获取mongo客户端对象,通过servers和credentials对象创建
  *
  * @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客户端对象,通过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对象
  *
  * @param mongoobject
  * @return
  */
 public static mongocollection<document> getcollection(mongoobject mongoobject, string collectionname) {
  return getmongoclient(mongoobject).getdatabase(mongoobject.database).getcollection(collectionname);
 }
 
 /**
  * 获取collection对象
  *
  * @param mongoobject
  * @return
  */
 public static mongocollection<document> 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数据库配置对象,针对单个数据服务,单个身份验证
 */
public class mongoobject extends mongobase {
 
  string host;
 
  int port;
 
  string user;
 
  string password;
 
  string database;
 
  mongoclient mongoclient;
 
  /**
   * 创建数据连接
   *
   * @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);
  }
 
  /**
   * 创建数据库连接
   *
   * @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对象
   *
   * @param collectionname
   * @return
   */
  public mongocollection<document> getmongocollection(string collectionname) {
    mongoclient mongoclientonline = getmongoclientonline(this);
    return mongoclientonline.getdatabase(database).getcollection(collectionname);
  }
 
 
  /**
   * 关闭连接
   */
  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);
  }

具体效果非常不错,测试代码如下:

 public static void main(string[] args) {
    mongoobject ready = new mongoobject("*****", 5117, "fission_record", "fission_record", "fission_record");
    mongocollection<document> app = ready.getmongocollection("app_logs_20181109");
    document first = app.find().first();
    output(first);
    ready.over();
  }

打印内容如下:

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}}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。