您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關MongoDB中怎么封裝一個操作類,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
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}}
以上就是MongoDB中怎么封裝一個操作類,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。