#Java#Spring#SpringBoot#Mongo#reactor#webflux#数据库#新增#修改#查询#删除#
Spring Boot WebFlux Mongo数据库新增、删除、查询、修改
视频讲解 : https://www.bilibili.com/video/av84240038/
Employee.java
package com.example.spring.webfluxmongo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Document @Data @AllArgsConstructor @NoArgsConstructor public class Employee { @Id private String id; private String name; }
EmployeeController.java
package com.example.spring.webfluxmongo; import lombok.AllArgsConstructor; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @RestController @AllArgsConstructor @RequestMapping("/employee") public class EmployeeController { private final EmployeeRep employeeRep; @DeleteMapping("/{id}") public Mono delete(@PathVariable String id){ return employeeRep.deleteById(id); } @GetMapping("/{id}") public Mono findById(@PathVariable String id){ return employeeRep.findById(id); } @PutMapping public Mono update(@RequestBody Employee employee){ return employeeRep.save(employee); } @PostMapping public Mono<Employee> save(@RequestBody Employee employee){ return employeeRep.save(employee); } @GetMapping public Flux<Employee> findAll(){ return employeeRep.findAll(); } }
EmployeeRep.java
package com.example.spring.webfluxmongo; import org.springframework.data.repository.reactive.ReactiveCrudRepository; public interface EmployeeRep extends ReactiveCrudRepository<Employee,String> { }
WebfluxMongoApplication.java
package com.example.spring.webfluxmongo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class WebfluxMongoApplication { public static void main(String[] args) { SpringApplication.run(WebfluxMongoApplication.class, args); } }
application.properties
spring.data.mongodb.uri=mongodb://localhost:27017/test
公众号,坚持每天3分钟视频学习