补全代码public class MongoDBTest { private MongoClient mongoClient; @Before public void initMongoClient() throws lOException { mongoClient = MongoDBUtilinitMongo(); @Test(imeout = 1000) public void saveUser Test() throws llgalArgumentException, llegalAccessException { Document userInfoDoc = ConvertUtil.convertDoc((1)); Document userBaseDoc = ConvertUtil.convertDoc( this.initUserExtendInfo().(2) ); mongoClient.getDatabase("Userest").getColletion((3) ) .insertOne((4)); } @Test public void queryUserTest() { (5)<Document> findlterable = mongoClient .getDatabase("User Test"),getCollection("user") (6).(new Document("age", 20).append("name", "张三")); System.out.println(findlterable. frst(),toJson)); } @After public void closeMongoClient() { mongoClient.(7); } private Userlnfo initUserlnfo() { UserInfo userInfo = new UserInfo(); userInfo.setAge(20); userlnfo.setName("张三"); userlInfo.setNickName("小明"); userInfo.setSex("男"); userInfo.setUserName("zhangsan"); return (8);
时间: 2024-04-01 12:33:31 浏览: 102
public class MongoDBTest {
private MongoClient mongoClient;
@Before
public void initMongoClient() throws IOException {
mongoClient = MongoDBUtil.initMongo();
}
@Test(timeout = 1000)
public void saveUserTest() throws IllegalArgumentException, IllegalAccessException {
Document userInfoDoc = ConvertUtil.convertDoc(initUserInfo()); //1
Document userBaseDoc = ConvertUtil.convertDoc(initUserExtendInfo()); //2
mongoClient.getDatabase("UserTest").getCollection("user") //3
.insertOne(userInfoDoc.append("userBaseInfo", userBaseInfo)); //4
}
@Test
public void queryUserTest() {
FindIterable<Document> findIterable = mongoClient
.getDatabase("UserTest").getCollection("user") //5
.find(new Document("age", 20).append("name", "张三"));
System.out.println(findIterable.first().toJson()); //6
}
@After
public void closeMongoClient() {
mongoClient.close(); //7
}
private UserInfo initUserInfo() {
UserInfo userInfo = new UserInfo();
userInfo.setAge(20);
userInfo.setName("张三");
userInfo.setNickName("小明");
userInfo.setSex("男");
userInfo.setUserName("zhangsan");
return userInfo; //8
}
private UserBaseInfo initUserExtendInfo() {
UserBaseInfo userBaseInfo = new UserBaseInfo();
userBaseInfo.setAddress("北京市海淀区");
userBaseInfo.setEmail("zhangsan@qq.com");
userBaseInfo.setPhone("13888888888");
return userBaseInfo;
}
}
阅读全文