mongodb dbobject、document和java bean 对象相互转换

 

import com.alibaba.fastjson.JSON;

import com.mongodb.BasicDBObject;

import org.bson.Document;

import org.bson.json.JsonWriterSettings;

public class util{

public <T> T toBean(BasicDBObject dbObject,Class<T> clzss){

String realJson = dbObject.toJson(JsonWriterSettings.builder().build());

T obj = JSON.parseObject(realJson,clzss);

return obj;

}

public <T> T toBean(Document document,Class<T> clzss){

String realJson = document.toJson(JsonWriterSettings.builder().build());

T obj = JSON.parseObject(realJson,clzss);

return obj;

}

public static <T> BasicDBObject toDBObject(T object){

String json = JSON.toJSONString(object);

BasicDBObject basicDBObject = BasicDBObject.parse(json);

return basicDBObject;

}

public static <T> Document toDocument(T object){

String json = JSON.toJSONString(object);

Document document = Document.parse(json);

return document;

}

}

    原文作者:稚枭天卓
    原文地址: https://blog.csdn.net/u013630349/article/details/100521673
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞