栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

1.创建接口继承MongoRepository
package com.example.mongodb.repository;

import com.example.mongodb.entity.Student;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface StudentRespostiory extends MongoRepository {
}

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

2.添加记录方法
  @Test
    public void create(){
         Student student=new Student();
         student.setName("mary");
         student.setEmail("mary@168.com");
         student.setAge(17);
         Object s1 = studentRespostiory.save(student);
         System.out.println("保存的对象:"+s1);
     }

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

我们在linux下查询

成功的添加了数据

3.查询所有数据
     @Test
    public void  findList(){
         List studentList = studentRespostiory.findAll();
         for (Student s:studentList) {
             System.out.println("查询记录:"+s);
         }
     }

 结果

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

 5.添加查询
    @Test
    public void findStudentList(){
        Student student = new Student();
        student.setAge(16);
        student.setName("jock");
        Example stExample= Example.of(student);
        List userList = studentRespostiory.findAll(stExample);
        System.out.println(userList);
    }

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

 6.模糊查询
    @Test
    public void findStudentList(){
         //固定格式--模糊匹配规则
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        
        Student student = new Student();
        student.setName("c");
        Example stExample = Example.of(student,matcher);
        List studentList = studentRespostiory.findAll(stExample);
        for (Student s:studentList) {
            System.out.println("查询记录:"+s);
        }
    }

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

7.分页查询

条件带分页

    @Test
    public void findPage(){
        Pageable pageable= PageRequest.of(0,3);
        Student student=new Student();
        student.setName("mary");

        Example stExample = Example.of(student);
        Page all = studentRespostiory.findAll(stExample, pageable);
        System.out.println(all);

    }

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

结果

 8.更新数据
    @Test
    public void updata(){
        Student student = studentRespostiory.findById("62049665dd5a9245cc968271").get();
        student.setName("DFP77");
        studentRespostiory.save(student);
    }

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

查看数据库

9.删除 
    @Test
    public void del(){
         studentRespostiory.deleteById("62049665dd5a9245cc968271");
    }

调用方法传入id

10.定义规范

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

SpringData 方法定义规范

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/737574.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号