ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 新建一个测试用的实体类 ``` package cn.javaex.springboot.view; import java.io.Serializable; import org.springframework.data.mongodb.core.mapping.Document; @Document(collection="user_info") public class UserInfo implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private String id; private String name; private Integer age; private String sex; 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "UserInfo [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]"; } } ``` ## 测试 ``` package cn.javaex.springboot; import java.util.List; import java.util.UUID; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import cn.javaex.springboot.util.MongoDBUtils; import cn.javaex.springboot.view.UserInfo; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootDemo2ApplicationTests { @Test public void save1() { UserInfo entity = new UserInfo(); entity.setName("张三"); entity.setAge(20); entity.setSex("男"); MongoDBUtils.save(entity); } @Test public void save2() { UserInfo entity = new UserInfo(); entity.setId(UUID.randomUUID().toString().replace("-", "")); entity.setName("猪八戒"); entity.setAge(21); entity.setSex("男"); MongoDBUtils.save(entity, "users"); } @Test public void delete1() { UserInfo entity = new UserInfo(); entity.setId("5c5a471e1ef42a15d89b17fe"); MongoDBUtils.delete(entity); } @Test public void delete2() { UserInfo entity = new UserInfo(); entity.setId("49c599b6885d4f758f2c9e610fc45530"); MongoDBUtils.delete(entity, "users"); } @Test public void delete3() { String key = "age"; int value = 21; String collectionName = "users"; MongoDBUtils.deleteByKey(key, value, collectionName); } @Test public void update1() { String key = "_id"; String value = "5c59b4d91ef42a725c6465ec"; String[] updateKeyArr = {"name", "age"}; Object[] updateValueArr = {"新名字", 25}; String collectionName = "users"; MongoDBUtils.updateFirst(key, value, updateKeyArr, updateValueArr, collectionName); } @Test public void update2() { String key = "age"; int value = 21; String[] updateKeyArr = {"age"}; Object[] updateValueArr = {30}; String collectionName = "users"; MongoDBUtils.updateAll(key, value, updateKeyArr, updateValueArr, collectionName); } @Test public void select1() { UserInfo entity = new UserInfo(); String[] findKeyArr = {"name", "age"}; Object[] findValueArr = {"张三", 20}; List<? extends Object> find = MongoDBUtils.find(entity, findKeyArr, findValueArr); System.out.println(find); } @Test public void select2() { UserInfo entity = new UserInfo(); String[] findKeyArr = {"name"}; String[] findValueArr = {"孙悟空"}; String collectionName = "users"; List<? extends Object> find = MongoDBUtils.find(entity, findKeyArr, findValueArr, collectionName); System.out.println(find); } @Test public void select3() { UserInfo entity = new UserInfo(); String[] findKeyArr = {"name"}; String[] findValueArr = {"孙悟空"}; String collectionName = "users"; Object findOne = MongoDBUtils.findOne(entity, findKeyArr, findValueArr, collectionName); System.out.println(findOne.toString()); } @Test public void select4() { UserInfo entity = new UserInfo(); List<? extends Object> findAll = MongoDBUtils.findAll(entity); System.out.println(findAll); } @Test public void select5() { UserInfo entity = new UserInfo(); List<? extends Object> findAll = MongoDBUtils.findAll(entity, "user_info"); System.out.println(findAll); } } ```