Home > Database > Mysql Tutorial > MongoDB保存Java对象的三种方法

MongoDB保存Java对象的三种方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 17:14:18
Original
2104 people have browsed it

MongoDB有三种方法来保存java对象,一种是通过类实现DBObject接口,一种是利用spring-mongodb工具的对象映射功能,把java对象转换

MongoDB有三种方法来保存java对象,一种是通过类实现DBObject接口,一种是利用spring-mongodb工具的对象映射功能,把java对象转换成MongoDB的BSON文档,一种是通过序列化类实例,转成二进制存储,MongoDB是支持二进制的数据格式的。下面分别介绍三种方法:

1.实现DBObject接口

用这种方法的话类还是个DBObject,存取对象属性都要用到put和get方法,感觉很不方便,这个我们可以参考官网的例子:

  • }     
  •   
  • collection.insert(myTweet);  
  •   
  • Tweet myTweet = (Tweet)collection.findOne();  
  • Tweet myTweet = (Tweet)collection.findOne();  
  •   
  • collection.save(myTweet);  
  • 可能大家已经发现,这种方法只适用于新的类,如果是原有的类这样做就不合适了。

    linux

    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template