• Java(99):Java针对Mongo数据库的工具类MongoDBUtil


    Java针对Mongo数据库的工具类MongoDBUtil

    1.Maven引用

     
           
                org.mongodb
                mongo-java-driver
                3.12.11
           

    数据配置文件db-java.properties

    # 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

    2.Java调用Mongo工具类

    1. package com.ciphergateway.utils;
    2. import com.ciphergateway.bean.AoeData;
    3. import com.mongodb.MongoClientSettings;
    4. import com.mongodb.MongoCredential;
    5. import com.mongodb.ServerAddress;
    6. import com.mongodb.client.*;
    7. import com.mongodb.client.model.Filters;
    8. import org.bson.Document;
    9. import org.slf4j.Logger;
    10. import org.slf4j.LoggerFactory;
    11. import java.util.ArrayList;
    12. import java.util.List;
    13. /**
    14. * ClassName MongoDBUtil
    15. * description MongoDBUtil工具类
    16. *
    17. * @author : HMF
    18. * date: 2022/8/12 10:20
    19. **/
    20. public class MongoDBUtil {
    21. protected static Logger log = LoggerFactory.getLogger(MongoDBUtil.class);
    22. private static String mongoServer;
    23. private static String aoeServer;
    24. private static String dbName;
    25. private static String dbUser;
    26. private static String dbSecret;
    27. private static String coll;
    28. private static MongoClient mongoClient;
    29. public MongoDBUtil(){
    30. String configFile="db-java.properties";
    31. PropertiesUtil pro= new PropertiesUtil(configFile);
    32. mongoServer = pro.readProperty("MONGO_SERVER");
    33. aoeServer = pro.readProperty("AOE_SERVER");
    34. dbName = pro.readProperty("MONGO_DB");
    35. dbUser = pro.readProperty("MONGO_USER");
    36. dbSecret = pro.readProperty("MONGO_SECRET");
    37. coll = pro.readProperty("MONGO_COLL");
    38. }
    39. public static void MongoConnect(){
    40. MongoConnect(null);
    41. }
    42. public static void MongoConnect(String aoeString){
    43. List addressList=new ArrayList<>();
    44. ServerAddress address = new ServerAddress(mongoServer);
    45. if (aoeString !=null) {
    46. address = new ServerAddress(aoeServer);
    47. }
    48. addressList.add(address);
    49. MongoCredential credential=MongoCredential.createCredential(dbUser, dbName, dbSecret.toCharArray());
    50. mongoClient = MongoClients.create(
    51. MongoClientSettings.builder()
    52. .applyToClusterSettings(builder -> builder.hosts(addressList))
    53. .credential(credential)
    54. .build());
    55. log.info("Connect to database successfully,mongoDatabase:"+dbName );
    56. }
    57. public void insert(String[] column_aa, String[] value_aa){
    58. MongoCollection dbColl= mongoClient.getDatabase(dbName).getCollection(coll);
    59. Document document = new Document();
    60. for(int i=0;i
    61. if(column_aa.length == value_aa.length){
    62. document.append(column_aa[i], value_aa[i]);
    63. }
    64. }
    65. dbColl.insertOne(document);
    66. log.info("插入文档成功");
    67. }
    68. public List query(){
    69. MongoCollection dbColl= mongoClient.getDatabase(dbName).getCollection(coll);
    70. List datalist=new ArrayList<>();
    71. AoeData data=new AoeData();
    72. MongoCursor mongoCursor = dbColl.find().iterator();
    73. if(mongoCursor.hasNext()){
    74. Document doc =mongoCursor.next();
    75. log.info("查询结果: "+doc);
    76. data.setAoeId(Integer.parseInt(doc.getString("aoeId")));
    77. data.setAoeAes(doc.getString("aoeAes"));
    78. data.setAoeSm4(doc.getString("aoeSm4"));
    79. data.setAoeSm4_a(doc.getString("aoeSm4_a"));
    80. data.setAoeEmail(doc.getString("aoeEmail"));
    81. data.setAoePhone(doc.getString("aoePhone"));
    82. data.setAoeIdCard(doc.getString("aoeIdCard"));
    83. data.setAoeOfficerCard(doc.getString("aoeOfficerCard"));
    84. data.setAoePassport(doc.getString("aoePassport"));
    85. data.setAoeGeneralIdCard(doc.getString("aoeGeneralIdCard"));
    86. data.setAoeCreditCard(doc.getString("aoeCreditCard"));
    87. data.setAoePosition(doc.getString("aoePosition"));
    88. datalist.add(data);
    89. }
    90. return datalist;
    91. }
    92. public void removeData() {
    93. MongoCollection dbColl= mongoClient.getDatabase(dbName).getCollection(coll);
    94. dbColl.deleteMany(Filters.eq("aoeAes","吴秀梅"));
    95. log.info("删除文档成功");
    96. close();
    97. }
    98. /**
    99. * 关闭连接对象
    100. */
    101. public void close(){
    102. if(mongoClient != null){
    103. mongoClient.close();
    104. }
    105. mongoClient = null;
    106. }
    107. }

    3.调用和执行结果

    1. public static void main(String[] args){
    2. MongoDBUtil mdb = new MongoDBUtil();
    3. mdb.MongoConnect();
    4. //字段名和字段值
    5. String column = "aoeId,aoeAes,aoeSm4,aoeSm4_a,aoeEmail,aoePhone,aoeIdCard,aoeOfficerCard,aoePassport,aoeGeneralIdCard,aoeCreditCard,aoePosition";
    6. String value="1,吴秀梅,Beijing Refining Network Technology Co.Ltd.,海淀区,qianxiulan@yahoo.com,15652996964,210302199608124861,武水电字第3632734号,BWP018930705,0299233902,6212262502009182455,测试数据";
    7. //解析字段名
    8. String[] column_aa = column.split(",");
    9. //解析值
    10. String[] value_aa = value.split(",");
    11. mdb.insert(column_aa, value_aa);
    12. mdb.query();
    13. mdb.removeData();
    14. }

    Java操作MongoDB详解

    https://blog.csdn.net/m0_37583655/article/details/123827112

  • 相关阅读:
    ProtoBuf、Grpc、GORM、Go-redis 入门基础
    自定义注解
    洛谷 P7695 [COCI2009-2010#4] PLANINA
    iNFTnews | 86年前的DAO:嗜酒者互诫协会
    【自用】三阶段项目-起步
    基于jeecg-boot的flowable流程自定义业务驳回到发起人的一种处理方式
    管正雄:基于预训练模型、智能运维的QA生成算法落地
    Android studio将一个项目作为module导入另一个项目
    css属性clip-path的使用说明
    spark SQLQueryTestSuite sql 自动化测试用例
  • 原文地址:https://blog.csdn.net/fen_fen/article/details/126306102