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

MongoDB(4) java api

?????? java api 的应用,可不是jdbc啊,mongodb 很奇怪,很奇怪,

代码的例子包括了,常用的 添加,修改,删除,统计,分页,条件,文档嵌套查询

?

测试代码

import java.util.HashSet;
import java.util.Set;

import org.junit.Test;

import dao.MobileDao;
import entity.Mobile;
import entity.Params;

public class MongoDBTest {

	@Test
	public void testInsert() throws Exception {
		MobileDao dao = new MobileDao();
		Mobile m = new Mobile();
		m.setId(102);
		m.setBrand("HTC");
		m.setName("dream");
		Set<Params> set = new HashSet<Params>();
		Params p1 = new Params();
		p1.setName("app");
		p1.setValue("中文情况");

		Params p2 = new Params();
		p2.setName("上市时间");
		p2.setValue("2011-1-1");
		set.add(p1);
		set.add(p2);
		m.setSet(set);
		dao.insert(m);
	}

	@Test
	public void testModify() throws Exception {
		MobileDao dao = new MobileDao();
		Mobile m = new Mobile();
		Mobile m2 = new Mobile();
		m.setName("tattoo");
		m2.setBrand("lenovo");
		m2.setId(m2.getId());
		m2.setName("s1");
		dao.modify(m, m2);
	}

	@Test
	public void testRemove() throws Exception {
		MobileDao dao = new MobileDao();
		Mobile m = new Mobile();
		m.setName("s1");
		dao.remove(m);
	}

	@Test
	public void testQueryAll() throws Exception {
		MobileDao dao = new MobileDao();
		dao.queryAll(new Mobile());

	}

	@Test
	public void testQuery() throws Exception {
		MobileDao dao = new MobileDao();
		dao.query(new Mobile());

	}

	@Test
	public void testLoad() throws Exception {
		MobileDao dao = new MobileDao();
		Mobile m = new Mobile();
		m.setId(1);
		dao.load(m);
	}

}

?完整代码在附件中