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