首页 > 数据库技术 > 详细

Java POJO类直接存储在MongoDB中

时间:2019-04-15 23:12:19      阅读:347      评论:0      收藏:0      [点我收藏+]

记录Java POJO类直接存储在MongoDB中的策略。

 

maven:

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver</artifactId>
    <version>3.8.0</version>
</dependency>

 

Java POJO类定义:

public class DiagnoseDocument {
    private ObjectId id;
    private String time;
    private String instance;
    private ArrayList<DiagnsoeResult> result;
}


public class DiagnsoeResult {
    private String instance_name;
    private String rule_name;
    private String problem_level;
    private String problem_name;
    private String problem_detail;
    private String advice;
}

 

插入的方法:

//复制集连接MongoDB
String uriString = getURIString(primary);
// 需要针对对象中的类型设置Codec
CodecProvider pojoCodecProvider = PojoCodecProvider.builder().register(DiagnoseDocument.class, DiagnsoeResult.class).build();
CodecRegistry pojoCodecRegistry = fromRegistries(fromCodecs(new StringCodec(), new ObjectIdCodec(), new BsonArrayCodec()), fromProviders(pojoCodecProvider));
MongoClient mongoClient = new MongoClient(new MongoClientURI(uriString));
MongoDatabase database = mongoClient.getDatabase("mydb");
//对Collection中的类型设置Codec
MongoCollection<DiagnoseDocument> collection = database.getCollection("soar", DiagnoseDocument.class).withCodecRegistry(pojoCodecRegistry);
collection.insertOne(diagnoseDocument);

 

Java POJO类直接存储在MongoDB中

原文:https://www.cnblogs.com/jayinnn/p/10713304.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!