当前位置:   article > 正文

springboot整和mogodb(MongoRepository)简单操作(增删改查,模糊、条件和分页查询)和SpringData 方法定义规范_mongorepository 分页

mongorepository 分页

Spring Data提供了对mongodb数据访问的支持,我们只需要继承MongoRepository类

前面的基础流程应用依赖,创建实体类参考下这篇文章参考文本

1.创建接口继承MongoRepository

  1. package com.example.mongodb.repository;
  2. import com.example.mongodb.entity.Student;
  3. import org.springframework.data.mongodb.repository.MongoRepository;
  4. public interface StudentRespostiory extends MongoRepository <Student,String>{
  5. }

然后我们在测试类中注入StudentRepository调用 MongoRepository类封装好的方法

2.添加记录方法

  1. @Test
  2. public void create(){
  3. Student student=new Student();
  4. student.setName("mary");
  5. student.setEmail("mary@168.com");
  6. student.setAge(17);
  7. Object s1 = studentRespostiory.save(student);
  8. System.out.println("保存的对象:"+s1);
  9. }

通过.save()将依据创建好的对象存入数据库

我们在linux下查询

成功的添加了数据

3.查询所有数据

  1. @Test
  2. public void findList(){
  3. List<Student> studentList = studentRespostiory.findAll();
  4. for (Student s:studentList) {
  5. System.out.println("查询记录:"+s);
  6. }
  7. }

 结果

 4.根据id查询

  1. @Test
  2. public void findById(){
  3. Student student = studentRespostiory.findById("62049665dd5a9245cc968271").get();
  4. System.out.println("根据id查询"+student);
  5. }

 5.添加查询

  1. @Test
  2. public void findStudentList(){
  3. Student student = new Student();
  4. student.setAge(16);
  5. student.setName("jock");
  6. Example<Student> stExample= Example.of(student);
  7. List<Student> userList = studentRespostiory.findAll(stExample);
  8. System.out.println(userList);
  9. }

 通过findAll查询数据记录但是我们要把封装好的条件对象stExample传入到这个方法中,先把封装好的对象用Example.of()处理

 6.模糊查询

  1. @Test
  2. public void findStudentList(){
  3. //固定格式--模糊匹配规则
  4. ExampleMatcher matcher=ExampleMatcher.matching()
  5. .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
  6. .withIgnoreCase(true);
  7. Student student = new Student();
  8. student.setName("c");
  9. Example<Student> stExample = Example.of(student,matcher);
  10. List<Student> studentList = studentRespostiory.findAll(stExample);
  11. for (Student s:studentList) {
  12. System.out.println("查询记录:"+s);
  13. }
  14. }

条件查询差不多只要传入模糊查询的匹配规则。

7.分页查询

条件带分页

  1. @Test
  2. public void findPage(){
  3. Pageable pageable= PageRequest.of(0,3);
  4. Student student=new Student();
  5. student.setName("mary");
  6. Example<Student> stExample = Example.of(student);
  7. Page<Student> all = studentRespostiory.findAll(stExample, pageable);
  8. System.out.println(all);
  9. }

我们调用PageRequest.of(0,3)设置当前页是第一页 并且每一页记录数为3然后返回给pageable对象 在做查询的时候把这个pageable对象传入到查询方法中

结果

 8.更新数据

  1. @Test
  2. public void updata(){
  3. Student student = studentRespostiory.findById("62049665dd5a9245cc968271").get();
  4. student.setName("DFP77");
  5. studentRespostiory.save(student);
  6. }

和插入数据方法一样但是这里如果有识别带有id值会根据id修改之前的值

查看数据库

9.删除 

  1. @Test
  2. public void del(){
  3. studentRespostiory.deleteById("62049665dd5a9245cc968271");
  4. }

调用方法传入id

10.定义规范

Spring Data提供了对mongodb数据访问的支持,我们只需要继承MongoRepository类,按照Spring Data规范就可以了

SpringData 方法定义规范

1、不是随便声明的,而需要符合一定的规范
2、 查询方法以find | read | get开头
3、 涉及条件查询时,条件的属性用条件关键字连接
4、 要注意的是:条件属性首字母需要大写
5、 支持属性的级联查询,但若当前类有符合条件的属性则优先使用,而不使用级联属性,若需要使用级联属性,则属性之间使用_强制进行连接

这样我们在接口写的方法不需要我们自己实现,MongoRepository根据定义的规则自己帮我们实现方法。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/55978
推荐阅读
相关标签
  

闽ICP备14008679号