Spring Boot(4) Mongo数据库新增、删除、查询、修改

Java#Spring#SpringBoot#Mongo#数据库#新增#修改#查询#删除

Spring Boot Mongo数据库新增、删除、查询、修改java

视频讲解: https://www.bilibili.com/vide...

Employee.javaweb

package com.example.spring.mogon;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document("employee")
@Data
public class Employee {
    @Id
    private String id;
    private String name;
}

EmployeeController.javaspring

package com.example.spring.mogon;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
public class EmployeeController {

    @Autowired
    private EmployeeRep employeeRep;

    @DeleteMapping("/delete/{id}")
    public Boolean delete(@PathVariable String id){
        employeeRep.deleteById(id);
        return Boolean.TRUE;
    }

    @GetMapping("/findById/{id}")
    public Optional<Employee> findById(@PathVariable String id){
        return employeeRep.findById(id);
    }

    @PutMapping("/update")
    public Boolean update(@RequestBody Employee employee){
        employeeRep.save(employee);
        return Boolean.TRUE;
    }

    @PostMapping("/save")
    public Boolean save(@RequestBody Employee employee){
        employeeRep.save(employee);
        return Boolean.TRUE;
    }

    @GetMapping("/list")
    public List<Employee> find(){
        return employeeRep.findAll();
    }
}

EmployeeRep.javamongodb

package com.example.spring.mogon;

import org.springframework.data.mongodb.repository.MongoRepository;

public interface EmployeeRep extends MongoRepository<Employee,String> {
}

MogonApplication.java数据库

package com.example.spring.mogon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MogonApplication {

    public static void main(String[] args) {
        SpringApplication.run(MogonApplication.class, args);
    }

}

公众号,坚持天天3分钟视频学习
app