`
mazhiyuan
  • 浏览: 62678 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

开源的Mongodb java client -- mango发布

阅读更多

Mango  ----   一个非常简单的操作mongodb的小工具,使用java语言,基于mongodb的java driver包。

 

其主要的灵感来自于Jongo 项目,这是一个非常有创意的工具,将mongodb shell编程扩展到了java语言包内。mango主要做的工作,是重写了Jongo的一些方法,使其更符合我们在开发中的需求,另外与spring相结合,将配置参数等记录在资源文件中进行管理,并适当的做了一些扩展。

 

新手上路

step 1  -- 配置

资源文件  driver.property

#database url ---> host:port
url=
user=
password=

#see mongoOptions.class in mongodb java driver
slaveOk=
connectionsPerHost=
minPoolsSize=
threadsAllowedToBlockForConnectionMultiplier=
maxWaitTime=
connectTimeout=
socketTimeout=
autoConnectRetry=
safe=

 

配置文件  spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="collection" class="org.mango.MangoCollection" init-method="init" destroy-method="close">
        <property name="mango">
            <ref bean="mango"/>
        </property>
        <property name="collection" value="test"/>
        <!-- there needs the full path of the model class , it's not a good idea , but I can't find a better way to solve this -->
        <property name="clazz" value="org.mango.Mazhiyuan"/>
    </bean>
    <bean id="mango" class="org.mango.Mango" init-method="init" destroy-method="close">
        <property name="driver">
            <ref bean="driver"/>
        </property>
    </bean>
    <bean id="driver" class="org.mango.MangoDriver" init-method="init" destroy-method="close">
        <property name="db" value="local"/>
        <property name="driver" ref = "config"/>
    </bean>
    <bean id="config" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location">
            <value>classpath:driver.properties</value>
        </property>
    </bean>
</beans>

 

资源文件主要是记录了mongodb的相关配置和mongodb的连接串,若是Replication模式,则以逗号分开,端口号一定要有。

spring配置文件,依次注册了MangoDriver,这个类加载配置项并初始化了数据库,MangoCollection只暴露给用户的client类,这个类需要要操作的mongodb collection和该collection对应的model类。

 

 

step 2 Model 类

model类最好继承与Madel类,虽然这是一个可选的条件,但强烈建议这样做,Madel类非常简单,只是含有一个ObjectsId来对应mongodb中的_id字段

 

step 3 启动

 

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");
MangoCollection collection = (MangoCollection) context.getBean("collection");

 得到MangoCollection之后,就可以进行一系列的操作了

 

 

Mazhiyuan mazhiyuan = collection.findOne().as();
System.out.println(mazhiyuan.getName());

Mazhiyuan mazhiyuan = collection.findOne("{age:3}").as();

List<Mazhiyuan> mazhiyuans = collection.find("{age:3}").asList();

Iterable<Mazhiyuan> mazhiyuans = collection.find("{age:3}").as();

List<Mazhiyuan> mazhiyuans = collection.find("{age:3}").field("{age:0}").skip(10).sort("{age:1}").limit(10).asList();
 

 这些语句和mongodb shell的语言非常的类似,注意asList是返回List对象,而as方法返回的是Iterable对象

 

collection.update("{name: 'mzy1'}").with("{$inc: {age: 1}}");
collection.update("{name: 'mzy1'}").upsert().multi().with("{$inc: {age: 1}}");
collection.update("{name: 'mzy1'}").upsert().with("{$inc: {age: 1}}");
 

更新操作也是一样的

 

collection.save(new Mazhiyuan("mzy", 23));
collection.save("{name:'mzy',age:23}");

插入操作

 

 

collection.remove("{name: 'mzy'}");
collection.remove(new ObjectId("4c...e"));

删除操作

 

需要说明的是这些操作返回都是boolean类型,可以判断操作是否成功。

 

目前,这个项目还没有完全开发完成,对group这样的操作,我本人理解也不够深刻,所以在理解group之后再将这个补充到mango中,另外,mango还没有经受过足够的测试,可能在使用中会有一些问题,请给我发邮件,或是提交bug报告。如果你有好的建议,也请告知我。

 

 

源码地址:https://github.com/mazhiyuan/mango-spring

 

0
4
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics