MongoDB操作类封装实例代码
前言
最近接到一个需求,要做MongoDB打点数据的统计,在学习过MongoDB的操作之后,封装了一个MongoDB的操作类,分为两部分,基本思想是参照了自己写过的mysql的操作类。一个是基本的操作类,包括所有基本操作的静态方法,还有一个是mongoobject,就是具体操作的实现类。
以后再写如何用springboot写一个简单的统计服务。
MongoDB操作类封装
mongobase代码如下:
packagecom.fun.mongodb; importcom.fun.frame.SourceCode; importcom.mongodb.MongoClient; importcom.mongodb.MongoClientURI; importcom.mongodb.MongoCredential; importcom.mongodb.ServerAddress; importcom.mongodb.client.MongoCollection; importcom.mongodb.client.MongoDatabase; importorg.bson.Document; importjava.util.Arrays; importjava.util.List; /** *mongo操作类的基础类 */ publicclassMongoBaseextendsSourceCode{ /** *获取服务地址list * *@paramaddresses *@return */ publicstaticListgetServers(ServerAddress...addresses){ returnArrays.asList(addresses); } /** *获取服务地址 * *@paramhost *@paramport *@return */ publicstaticServerAddressgetServerAdress(Stringhost,intport){ returnnewServerAddress(host,port); } /** *获取认证list * *@paramcredentials *@return */ publicstaticList getCredentials(MongoCredential...credentials){ returnArrays.asList(credentials); } /** *获取验证 * *@paramuserName *@paramdatabase *@parampassword *@return */ publicstaticMongoCredentialgetMongoCredential(StringuserName,Stringdatabase,Stringpassword){ returnMongoCredential.createCredential(userName,database,password.toCharArray()); } /** *获取mongo客户端 * *@paramaddresses *@paramcredentials *@return */ publicstaticMongoClientgetMongoClient(List addresses,List credentials){ returnnewMongoClient(addresses,credentials); } /** *连接mongo数据库 * *@parammongoClient *@paramdatabaseName *@return */ publicstaticMongoDatabasegetMongoDatabase(MongoClientmongoClient,StringdatabaseName){ returnmongoClient.getDatabase(databaseName); } /** *连接mongo集 * *@parammongoDatabase *@paramcollectionName *@return */ publicstaticMongoCollection getMongoCollection(MongoDatabasemongoDatabase,StringcollectionName){ returnmongoDatabase.getCollection(collectionName); } /** *关闭数据库连接 * *@parammongoClient */ publicstaticvoidMongoOver(MongoClientmongoClient){ mongoClient.close(); } /** *获取mongo客户端对象,通过servers和credentials对象创建 * *@parammongoObject *@return */ publicstaticMongoClientgetMongoClient(MongoObjectmongoObject){ MongoClientmongoClient=newMongoClient(getServers(getServerAdress(mongoObject.host,mongoObject.port)),getCredentials(getMongoCredential(mongoObject.user,mongoObject.database,mongoObject.password))); returnmongoClient; } /** *获取mongo客户端对象,通过uri方式连接 * *@parammongoObject *@return */ publicstaticMongoClientgetMongoClientOnline(MongoObjectmongoObject){ Stringformat=String.format("mongodb://%s:%s@%s:%d/%s",mongoObject.user,mongoObject.password,mongoObject.host,mongoObject.port,mongoObject.database); returnnewMongoClient(newMongoClientURI(format)); } /** *获取collection对象 * *@parammongoObject *@return */ publicstaticMongoCollection getCollection(MongoObjectmongoObject,StringcollectionName){ returngetMongoClient(mongoObject).getDatabase(mongoObject.database).getCollection(collectionName); } /** *获取collection对象 * *@parammongoObject *@return */ publicstaticMongoCollection getCollectionOnline(MongoObjectmongoObject,StringcollectionName){ returngetMongoClientOnline(mongoObject).getDatabase(mongoObject.database).getCollection(collectionName); } }
mongoobject的代码如下:
packagecom.fun.mongodb; importcom.mongodb.MongoClient; importcom.mongodb.client.MongoCollection; importorg.bson.Document; /** *mongo数据库配置对象,针对单个数据服务,单个身份验证 */ publicclassMongoObjectextendsMongoBase{ Stringhost; intport; Stringuser; Stringpassword; Stringdatabase; MongoClientmongoClient; /** *创建数据连接 * *@paramhost *@paramport *@paramuser *@parampassword *@paramdatabase */ publicMongoObject(Stringhost,intport,Stringuser,Stringpassword,Stringdatabase){ this.host=host; this.port=port; this.user=user; this.password=password; this.database=database; this.mongoClient=getMongoClient(this); } /** *创建数据库连接 * *@paramport *@paramhost *@paramuser *@parampassword *@paramdatabase */ publicMongoObject(intport,Stringhost,Stringuser,Stringpassword,Stringdatabase){ this.host=host; this.port=port; this.user=user; this.password=password; this.database=database; this.mongoClient=getMongoClientOnline(this); } /** *获取colletion对象 * *@paramcollectionName *@return */ publicMongoCollectiongetMongoCollection(StringcollectionName){ MongoClientmongoClientOnline=getMongoClientOnline(this); returnmongoClientOnline.getDatabase(database).getCollection(collectionName); } /** *关闭连接 */ publicvoidover(){ MongoOver(this.mongoClient); } } @Override publicMongoObjectclone(){ returnnewMongoObject(this.host,this.port,this.user,this.password,this.database); } publicMongoObjectclone2(){ returnnewMongoObject(this.port,this.host,this.user,this.password,this.database); }
具体效果非常不错,测试代码如下:
publicstaticvoidmain(String[]args){ MongoObjectready=newMongoObject("*****",5117,"fission_record","fission_record","fission_record"); MongoCollectionapp=ready.getMongoCollection("app_logs_20181109"); Documentfirst=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-Redmi5Plus,client_type=200,server_time=1541721601655,os_name=200,os_version=15,client_time=1541721577025}}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。