日期:2014-05-16  浏览次数:20685 次

spring data mongoDb的小例子
spring的配置
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    	<constructor-arg name="mongo" ref="mongo"/>
    	<constructor-arg name="databaseName" value="test"/>
    </bean>

	<!-- Factory bean that creates the Mongo instance -->
    <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
    	<property name="host" value="localhost"/>
    </bean>



java代码,包括增加数据。根据id查询,条件查询,聚合group 和 distinct
import static org.junit.Assert.assertEquals;

import java.util.List;


import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapreduce.GroupBy;
import org.springframework.data.mongodb.core.mapreduce.GroupByResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.ContextConfiguration;
import org.springside.modules.test.spring.SpringTxTestCase;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.CommandResult;

@ContextConfiguration(locations = { "/applicationContext-test.xml", "/applicationContext-mongodb.xml" })
public class MongoDbTest extends SpringTxTestCase {
	
	@Autowired
	private MongoTemplate mongoTemplate;
	
	@Test
	public void crud() {
		mongoTemplate.dropCollection(Tuser.class);
		Tuser user = new Tuser();
		
		for (int i = 0; i < 11; i ++) {
			user.setId(""+i);
			user.setSex(""+ i % 2);
			user.setName("amu" + i);
			mongoTemplate.save(user);
		}
		
		List<Tuser> userList = mongoTemplate.findAll(Tuser.class);
		
		for (Tuser t : userList) {
			System.out.println(t.getName());
		}

		System.out.println("done");
	}
	
	@Test
	public void find() {
		Tuser tuser = mongoTemplate.findById("2", Tuser.class);
		assertEquals("amu2", tuser.getName());
		
		Query query = new Query();
		Criteria criteria = new Criteria();
		criteria.and("sex").is("1");
		query.addCriteria(criteria);
		query.limit(2);
		List<Tuser> tuserList = mongoTemplate.find(query, Tuser.class);
		for (Tuser t : tuserList) {
			System.out.println(t.getName());
		}
		
		System.out.println(tuserList.size());
	}
	
	@Test
	public void group() {
		GroupBy groupBy = GroupBy.key("sex").initialDocument("{count:0}")
		.reduceFunction("function(doc, prev){prev.count+=1}");

		GroupByResults<Tuser> r = mongoTemplate.group("tuser", groupBy, Tuser.class);
		BasicDBList list = (BasicDBList)r.getRawResults().get("retval");
		for (int i = 0; i < list.size(); i ++) {
			BasicDBObject obj = (BasicDBObject)list.get(i);
			System.out.println(obj.get("count"));
		}
		System.out.println(r);
	}
	
	@Test
	public void distinct() {
		CommandResult result = mongoTemplate.executeCommand("{distinct:'tuser', key:'sex'}");
		System.out.println();
		BasicDBList list = (BasicDBList)result.get("values");
		for (int i = 0; i < list.size(); i ++) {
			System.out.println(list.get(i));
		}
		System.out.println();
	}
}

class Tuser {
	private String id;
	private String sex;
	private String name;
	
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}


	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
}