MongoDB框架Jongo的使用

一、什么是Jongo

jongo的主要作用是在java程序中使用mongo shell命令


二、jongo的使用

1、需要的包有

(1) bson4jackson-2.2.3.jar

(2)jackson-annotations-2.3.3.jar

(3)jackson-core-2.3.3.jar

(4)jackson-databind-2.2.3.jar

(5)jongo-1.0.jar

(6)mongo-java-driver-2.10.1.jar

    jongo依赖jackson和bson4jackson两个包

2、使用

   

package testjson;

import java.net.UnknownHostException;

import org.jongo.FindOne;
import org.jongo.Jongo;
import org.jongo.MongoCollection;
import org.junit.Before;
import org.junit.Test;

import com.mongodb.DB;
import com.mongodb.MongoClient;

import entity.Student;

/**
 * @author Administrator
 *测试使用jongo
 */
public class TestJongo {
	
	Jongo jongo = null;
	MongoCollection col = null;
	@Before
	public void beforeTest(){
		DB db;
		try {
			db = new MongoClient().getDB("mydata");
			jongo = new Jongo(db);
			col = jongo.getCollection("mytest");
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//查找
	@Test
	public void testJongoRead(){
		Iterable<Student> iterables = col.find("{name:'zhangsan'}").as(Student.class);
		for(Student stu:iterables){
			System.out.println(stu);
		}
		Student one = col.findOne("{name:'zhangsan1'}").as(Student.class);
		System.out.println(one);
	}
	//插入
	@Test
	public void testJongoWrite(){
		Student student = new Student();
		student.setNum("112");
		student.setName("zhangkeke");
		student.setAge(20);
		col.save(student);
	}
	
	/**
	 * #表示占位符,动态添加参数 动态参数的添加支持类如col.find("{address;#}",new Address(....))
	 * 
	 */
	@Test
	public void testJongoQuery(){
		Student student = new Student();
		student.setName("zhangkeke");
		Student s = col.findOne("{name:#,age:#}","zhangkeke",24).as(Student.class);
		System.out.println(s);
	}
	//修改
	@Test
	public void testJongoUpdate(){
		col.update("{name:#}", "zhangkeke").upsert().multi().with("{$inc:{age:#}}",1);
		testJongoQuery();
	}
}
to be continue.......

mongodb-orm简介Mongodb ORM是基于java的ORM框架,简化了SDK的使用,使代码变得更清晰、简单。 与Ibatis类似,将查询、执行语句封装在xml中,与代码隔离。简称MQL。 项目中使用加入mongodb orm的支持包1. 添加jar包或maven支持<dependency>     <groupId>com.mongodborm</groupId>     <artifactId>mongodb-orm</artifactId>     <version>0.0.1-RELEASE</version> </dependency>2. 初始化mongodb templet        spring中初始化<bean id="mongoTemplet" class="com.mongodb.client.MongoClientTemplet">     <property name="factory">         <bean class="com.mongodb.client.MongoORMFactoryBean">             <property name="dataSource">                 <bean class="com.mongodb.client.MongoDataSource">                     <property name="nodeList" value="127.0.0.1:27017" />                     <property name="dbName" value="your db name" />                     <property name="userName" value="user name" />                     <property name="passWord" value="password" /> <!-- 可使用默认值 --> <property name="connectionsPerHost" value="" />                     <property name="threadsAllowedToBlock" value="" />                     <property name="connectionTimeOut" value="" />                     <property name="maxRetryTime" value="" />                     <property name="socketTimeOut" value="" />                 </bean>             </property>             <property name="configLocations">                 <list>                     <value>classpath:mql/mongo-mql.xml</value>                 </list>             </property>         </bean>     </property> </bean>        代码初始化    try {       Resource resource =  new ClassPathResource("mongo-mql.xml");           MongoORMFactoryBean factory = new MongoORMFactoryBean();       factory.setConfigLocations(new Resource[]{resource});       factory.init();          MongoClientTemplet templet = new MongoClientTemplet();       templet.setFactory(factory);       templet.init();     } catch(Exception e) {       e.printStackTrace();     }编写MQLMapping<mapping id="model" class="test.mongodborm.Model">         <property column="_id" name="id" />         <property column="name" name="name" />         <property column="time" name="time" value="0" />         <property column="status" name="status" /> </mapping> <mapping id="extendModel" class="test.mongodborm.Model" extends="model">     <property column="newProperty" name="newProperty" /> </mapping>  select<select id="queryModelList" collection="test_sample">     <query class="java.lang.String">         <property column="name" name="${value}" />     </query>     <field mapping="model" />     <order>         <property column="time" value="desc" />     </order> </select> update/findAndModify<update id="updateModel" collection="test_sample">     <query class="test.mongodborm.Model$Child">         <property column="name" name="name" ignoreNull="true" />         <property column="time" operate="gte" value="0" type="number" />         <property column="status" operate="in">             <list type="number">0,1</list>         </property>     </query>     <action class="java.util.Map">         <property column="name" name="name" operate="set" />         <property column="status" operate="set" />     </action> </update>有嵌套的查询<select id="queryModelList3" collection="test_sample">     <query class="java.lang.String">         <property column="_id" value="${value}" />         <property column="time" value="0" type="number" />     </query>     <field class="java.util.Map">         <property column="name" name="name" />         <property column="parent" name="parent">             <value class="test.mongodborm.Model$Parent">                 <property column="name" name="name" />                 <property column="child" name="child">                     <value class="test.mongodborm.Model$Child">                         <property column="name" name="name" />                         <property column="time" name="time" value="0" />                     </value>                 </property>                 <property column="data" name="data">                     <value class="java.util.Map">                         <property column="title" name="title" />                         <property column="content" name="content" />                     </value>                 </property>             </value>         </property>         <property column="data" name="data">             <value class="java.util.Map">                 <property column="title" name="title" />                 <property column="content" name="content" />             </value>         </property>     </field>     <order class="java.util.Map">         <property column="time" name="time" value="desc" />     </order> </select>Templet用法Model model = mongoTemplet.findOne("queryModelList", "yuxiangping"); List<Model> list = mongoTemplet.findOne("queryModelList", ""); Model model = new Model(); model.setTime(1L); Map<String, String> action = new HashMap<String, String>(); action.put("name", "yuxiangping-update"); int update = mongoT emplet.update("updateModel", model, action);        更多的使用方法参见 sample.xml 标签:Mongodb
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值