MongoTemplate
//条件查询
@GetMapping("findUser")
public void findUserList() {
Query query = new Query(
Criteria.where("name").is("zhang3")
.and("age").is(33)
);
List<User> users = mongoTemplate.find(query, User.class);
users.forEach(System.out::println);
}
//条件模糊查询
@GetMapping("findUserLike")
public void findUserLike() {
String name = "ang";
String regex = String.format("%s%s%s", "^.*", name, ".*$");
Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Query query = new Query(
Criteria.where("name").regex(pattern)
);
List<User> users = mongoTemplate.find(query, User.class);
users.forEach(System.out::println);
}
//条件模糊分页查询
@GetMapping("findUserPage")
public void findUserPage() {
//0定义分页参数
int pageNo = 1;
int pageSize = 10;
//1设置筛选条件
String name = "ang";
String regex = String.format("%s%s%s", "^.*", name, ".*$");
Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Query query = new Query(
Criteria.where("name").regex(pattern)
);
//2查询总记录数total
long total = mongoTemplate.count(query, User.class);
//3设置分页条件
query.skip((pageNo-1)*pageSize).limit(pageSize);
List<User> users = mongoTemplate.find(query, User.class);
System.out.println("total = " + total);
users.forEach(System.out::println);
}
//修改
@GetMapping("update")
public void updateUser() {
User user = mongoTemplate.findById("5ffbfa2ac290f356edf9b5aa", User.class);
user.setName("test_1");
user.setAge(25);
user.setEmail("493220990@qq.com");
Query query = new Query(Criteria.where("_id").is(user.getId()));
Update update = new Update();
update.set("name", user.getName());
update.set("age", user.getAge());
update.set("email", user.getEmail());
UpdateResult result = mongoTemplate.upsert(query, update, User.class);
long count = result.getModifiedCount();
System.out.println(count);
}
MongoRepository
@GetMapping("create")
public void createUser() {
User user = new User();
user.setAge(20);
user.setName("张三");
user.setEmail("3332200@qq.com");
User user1 = userRepository.save(user);
System.out.println("user1 = " + user1);
}
//查询所有
@GetMapping("findAll")
public void findUser() {
List<User> users = userRepository.findAll();
users.forEach(System.out::println);
}
//根据id查询
@GetMapping("findId")
public void getById() {
User user = userRepository.findById("64b4a856a2225f220ec3fa0f").get();
System.out.println("user = " + user);
}
//条件查询
@GetMapping("findUser")
public void findUserList() {
User user = new User();
user.setName("张三");
user.setAge(20);
Example<User> example = Example.of(user);
List<User> users = userRepository.findAll(example);
users.forEach(System.out::println);
}
//条件模糊查询
@GetMapping("findUserLike")
public void findUserLike() {
//查询条件
User user = new User();
user.setName("三");
//模板构造器
ExampleMatcher matcher = ExampleMatcher.matching()
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
.withIgnoreCase(true);
//查询条件模板
Example<User> example = Example.of(user,matcher);
List<User> users = userRepository.findAll(example);
users.forEach(System.out::println);
}
//条件模糊分页查询
@GetMapping("findUserPage")
public void findUserPage() {
//创建排序对象
Sort sort = Sort.by(Sort.Direction.DESC,"age");
//创建分页对象 第一页从0开始
Pageable pageable = PageRequest.of(0,2,sort);
//查询条件
User user = new User();
user.setName("三");
//模板构造器
ExampleMatcher matcher = ExampleMatcher.matching()
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
.withIgnoreCase(true);
//查询条件模板
Example<User> example = Example.of(user,matcher);
Page<User> userPage = userRepository.findAll(example, pageable);
userPage.getContent().forEach(System.out::println);
System.out.println("userPage.getTotalElements() = " + userPage.getTotalElements());
}
//修改
@GetMapping("update")
public void updateUser() {
User user = userRepository.findById("60b8d57ed539ed5b124942de").get();
user.setName("张三_1");
user.setAge(25);
user.setEmail("883220990@qq.com");
User save = userRepository.save(user);
System.out.println(save);
}
//删除操作
@GetMapping("delete")
public void delete() {
userRepository.deleteById("60b8d57ed539ed5b124942de");
}
@GetMapping("testMethod1")
public void testMethod1() {
List<User> users = userRepository.getByNameAndAge("张三",20);
users.forEach(System.out::println);
}
@GetMapping("testMethod2")
public void testMethod2() {
List<User> users = userRepository.getByNameLike("三");
users.forEach(System.out::println);
文章来源地址https://www.toymoban.com/news/detail-591095.html
文章来源:https://www.toymoban.com/news/detail-591095.html
到了这里,关于MongoDB(MongoTemplate和MongoRepository)对比的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!