MongoMongo简介

简介

MongMongo是一个用Java写的ODM框架,使得对MongoDB的操作更加便捷。

MongoMongo努力为Java开发者提供类似于ActiveORM 或者 Hibernate的操作API,并且保留了MongoDB的schemaless,document-based 设计,动态查询,原子修改操作等特性。当然你可以很方便的绕开MongoMongo而使用Java Driver 原生提供的功能。

示例代码

public class Blog extends Document {  
    static {  
        storeIn("blogs");                  
        hasManyEmbedded("articles", new Options(map(  
                Options.n_kclass, Article.class  
        )));  
        //create index  
        index(map("blogTitle", -1), map(unique,true));  
        //validate uerName field  
        validate("userName",map(length,map(  
                minimum,5  
        )));  

    }  
    //association related  
    public AssociationEmbedded articles() {throw new AutoGeneration();}  
    private String userName;  
    private String blogTitle;  
}  

public class Article extends Document {  
    static {          
        belongsToEmbedded("blog", new Options(map(  
                Options.n_kclass, Blog.class  
        )));  
    }  
    public AssociationEmbedded blog() {throw new AutoGeneration();}  
    private String title;  
    private String body;  
}  

public class Usage{  
  public static void main(String[] args){  

     Blog blog = Blog.where(map("userName","sexy java")).in(map("id",list(1,2,3))).singleFetch();  
     blog.articles().build(map("title","i am title","body","i am body"));  
     blog.save();  
  }  

}  
从上面代码中能够学习到关联,存储,创建索引,设置别名等操作都简单的在static 块中调用一个函数即可实现。

和Spring-data对比

Spring-data的写法为
public static void main( String[] args )  
   {  
       MongoOperations mongoOps = new MongoTemplate(new Mongo(), "mydb");  
       Person person = new Person();  
       person.setName("Joe");  
       person.setAge(10);  
       mongoOps.insert(person);  
       log.info(mongoOps.findOne(new Query(Criteria.where("name").is("Joe")), Person.class));  
   }  

而MongoMongo写法为
public static void main( String[] args )  
 {  
     Person person =  Person.create(map("name","Joe","age",34));  
     person.save();  
     log.info(Person.where(map("name","Joe")).singleFetch());   
 } 
从上面能够看出Spring-dat为了构造查询串引入Criteria对象,为了进行查询引入Query对象,查询时还要申明Person对象等,而MongoMongo写的比较简单而且清晰。

总结

针对MongoDB非关系型数据库的操作框架有很多,在项目中也可以选择合适项目的比较关系,通过对比之后单独使用MongoMongo在代码易用性和清晰度上占优势,而其他的可能和其他框架集成上有优势,所以需要用到的时候做出正确的判断就可以了。
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值