Java针对Mongo数据库的工具类MongoDBUtil
org.mongodb
mongo-java-driver
3.12.11
# mongo MONGO_SERVER=10.1.1.196:27017 AOE_SERVER=10.1.1.196:29019 MONGO_DB=aoe_mongo MONGO_USER=hmf MONGO_SECRET=lianshi MONGO_COLL=autotest
- package com.ciphergateway.utils;
-
- import com.ciphergateway.bean.AoeData;
- import com.mongodb.MongoClientSettings;
- import com.mongodb.MongoCredential;
- import com.mongodb.ServerAddress;
- import com.mongodb.client.*;
- import com.mongodb.client.model.Filters;
- import org.bson.Document;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * ClassName MongoDBUtil
- * description MongoDBUtil工具类
- *
- * @author : HMF
- * date: 2022/8/12 10:20
- **/
-
-
- public class MongoDBUtil {
- protected static Logger log = LoggerFactory.getLogger(MongoDBUtil.class);
-
- private static String mongoServer;
- private static String aoeServer;
- private static String dbName;
- private static String dbUser;
- private static String dbSecret;
- private static String coll;
-
- private static MongoClient mongoClient;
-
- public MongoDBUtil(){
- String configFile="db-java.properties";
- PropertiesUtil pro= new PropertiesUtil(configFile);
- mongoServer = pro.readProperty("MONGO_SERVER");
- aoeServer = pro.readProperty("AOE_SERVER");
- dbName = pro.readProperty("MONGO_DB");
- dbUser = pro.readProperty("MONGO_USER");
- dbSecret = pro.readProperty("MONGO_SECRET");
- coll = pro.readProperty("MONGO_COLL");
- }
-
- public static void MongoConnect(){
- MongoConnect(null);
- }
-
- public static void MongoConnect(String aoeString){
- List
addressList=new ArrayList<>(); - ServerAddress address = new ServerAddress(mongoServer);
- if (aoeString !=null) {
- address = new ServerAddress(aoeServer);
- }
- addressList.add(address);
-
- MongoCredential credential=MongoCredential.createCredential(dbUser, dbName, dbSecret.toCharArray());
- mongoClient = MongoClients.create(
- MongoClientSettings.builder()
- .applyToClusterSettings(builder -> builder.hosts(addressList))
- .credential(credential)
- .build());
- log.info("Connect to database successfully,mongoDatabase:"+dbName );
-
- }
-
-
- public void insert(String[] column_aa, String[] value_aa){
- MongoCollection dbColl= mongoClient.getDatabase(dbName).getCollection(coll);
- Document document = new Document();
- for(int i=0;i
- if(column_aa.length == value_aa.length){
- document.append(column_aa[i], value_aa[i]);
- }
- }
- dbColl.insertOne(document);
- log.info("插入文档成功");
- }
-
- public List
query(){ - MongoCollection dbColl= mongoClient.getDatabase(dbName).getCollection(coll);
- List
datalist=new ArrayList<>(); - AoeData data=new AoeData();
- MongoCursor
mongoCursor = dbColl.find().iterator(); - if(mongoCursor.hasNext()){
- Document doc =mongoCursor.next();
- log.info("查询结果: "+doc);
- data.setAoeId(Integer.parseInt(doc.getString("aoeId")));
- data.setAoeAes(doc.getString("aoeAes"));
- data.setAoeSm4(doc.getString("aoeSm4"));
- data.setAoeSm4_a(doc.getString("aoeSm4_a"));
- data.setAoeEmail(doc.getString("aoeEmail"));
- data.setAoePhone(doc.getString("aoePhone"));
- data.setAoeIdCard(doc.getString("aoeIdCard"));
- data.setAoeOfficerCard(doc.getString("aoeOfficerCard"));
- data.setAoePassport(doc.getString("aoePassport"));
- data.setAoeGeneralIdCard(doc.getString("aoeGeneralIdCard"));
- data.setAoeCreditCard(doc.getString("aoeCreditCard"));
- data.setAoePosition(doc.getString("aoePosition"));
- datalist.add(data);
- }
- return datalist;
- }
-
-
- public void removeData() {
- MongoCollection dbColl= mongoClient.getDatabase(dbName).getCollection(coll);
- dbColl.deleteMany(Filters.eq("aoeAes","吴秀梅"));
- log.info("删除文档成功");
- close();
- }
-
-
- /**
- * 关闭连接对象
- */
- public void close(){
- if(mongoClient != null){
- mongoClient.close();
- }
- mongoClient = null;
- }
-
- }
3.调用和执行结果
- public static void main(String[] args){
-
- MongoDBUtil mdb = new MongoDBUtil();
- mdb.MongoConnect();
- //字段名和字段值
- String column = "aoeId,aoeAes,aoeSm4,aoeSm4_a,aoeEmail,aoePhone,aoeIdCard,aoeOfficerCard,aoePassport,aoeGeneralIdCard,aoeCreditCard,aoePosition";
- String value="1,吴秀梅,Beijing Refining Network Technology Co.Ltd.,海淀区,qianxiulan@yahoo.com,15652996964,210302199608124861,武水电字第3632734号,BWP018930705,0299233902,6212262502009182455,测试数据";
- //解析字段名
- String[] column_aa = column.split(",");
-
- //解析值
- String[] value_aa = value.split(",");
- mdb.insert(column_aa, value_aa);
- mdb.query();
- mdb.removeData();
-
- }
Java操作MongoDB详解