|
| 1 | +```text |
| 2 | +1.引入webflux和reactive mongodb依赖: |
| 3 | + <dependency> |
| 4 | + <groupId>org.springframework.boot</groupId> |
| 5 | + <artifactId>spring-boot-starter-webflux</artifactId> |
| 6 | + </dependency> |
| 7 | + <dependency> |
| 8 | + <groupId>org.springframework.boot</groupId> |
| 9 | + <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId> |
| 10 | + </dependency> |
| 11 | +2.开启Reactive MongoDB: (在入口类上添加@EnableReactiveMongoRepositories注解) |
| 12 | + @SpringBootApplication |
| 13 | + @EnableReactiveMongoRepositories |
| 14 | + public class WebfluxApplication { |
| 15 | + public static void main(String[] args) { |
| 16 | + SpringApplication.run(WebfluxApplication.class, args); |
| 17 | + } |
| 18 | + } |
| 19 | +3.在yml配置文件里配置MongoDB连接: |
| 20 | + spring: |
| 21 | + data: |
| 22 | + mongodb: |
| 23 | + host: localhost |
| 24 | + # 启动MongoDB使用的端口号 |
| 25 | + port: 27017 |
| 26 | + # MongoDB中操作的database,需要创建 |
| 27 | + database: webflux |
| 28 | +4.Mongo Shell或者Mongo Compass工具创建数据库webflux,并新增user文档: |
| 29 | + (1)使用Mongo Shell: |
| 30 | + 1)创建数据库webflux: use webflux |
| 31 | + 2)新增user文档: db.createCollection(user) |
| 32 | + (2)使用Mongo Compass工具创建数据库: 图形化操作 |
| 33 | +5.创建User实体类: |
| 34 | + @Document(collection = "user") |
| 35 | + public class User { |
| 36 | + @Id |
| 37 | + private String id; |
| 38 | + private String name; |
| 39 | + private Integer age; |
| 40 | + private String description; |
| 41 | + // get set 略 |
| 42 | + } |
| 43 | +6.编写Mapper接口: (ReactiveMongoRepository提供的方法都是响应式的) |
| 44 | + @Repository |
| 45 | + public interface UserDao extends ReactiveMongoRepository<User, String> { |
| 46 | + } |
| 47 | +7.编写Service接口: |
| 48 | + public interface UserService { |
| 49 | + // 查询文档内所有数据 |
| 50 | + Flux<User> selectAll(); |
| 51 | + // 通过id查询文档内的数据 |
| 52 | + Mono<User> selectById(String id); |
| 53 | + // 向文档内插入一条记录 |
| 54 | + Mono<User> create(User user); |
| 55 | + // 通过id更新文档内容 |
| 56 | + Mono<User> updateById(String id,User user); |
| 57 | + // 通过id删除文档内容 |
| 58 | + Mono<Void> deleteById(String id); |
| 59 | + } |
| 60 | +8.编写Service实现类: |
| 61 | + @Service |
| 62 | + public class UserServiceImpl implements UserService { |
| 63 | + @Autowired |
| 64 | + private UserMapper mapper; |
| 65 | + @Override |
| 66 | + public Flux<User> selectAll() { |
| 67 | + return this.mapper.findAll(); |
| 68 | + } |
| 69 | + @Override |
| 70 | + public Mono<User> selectById(String id) { |
| 71 | + return this.mapper.findById(id); |
| 72 | + } |
| 73 | + @Override |
| 74 | + public Mono<User> create(User user) { |
| 75 | + return this.mapper.save(user); |
| 76 | + } |
| 77 | + @Override |
| 78 | + public Mono<User> updateById(String id, User user) { |
| 79 | + return this.mapper.findById(id) |
| 80 | + .flatMap( |
| 81 | + u -> { |
| 82 | + u.setName(user.getName()); |
| 83 | + u.setAge(user.getAge()); |
| 84 | + u.setDescription(user.getDescription()); |
| 85 | + return this.mapper.save(user); |
| 86 | + } |
| 87 | + ); |
| 88 | + } |
| 89 | + @Override |
| 90 | + public Mono<Void> deleteById(String id) { |
| 91 | + return this.mapper.findById(id).flatMap(user -> this.mapper.delete(user)); |
| 92 | + } |
| 93 | + } |
| 94 | +9.编写Controller: |
| 95 | + @RestController |
| 96 | + public class UserController { |
| 97 | + @Autowired |
| 98 | + private UserService service; |
| 99 | + @GetMapping("/user") |
| 100 | + public Flux<User> getUsers(){ |
| 101 | + return this.service.selectAll(); |
| 102 | + } |
| 103 | + @GetMapping("/user/{id}") |
| 104 | + public Mono<ResponseEntity<User>> getUser(@PathVariable String id){ |
| 105 | + return this.service.selectById(id) |
| 106 | + .map(user -> new ResponseEntity<>(user, HttpStatus.OK)) |
| 107 | + .defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND)); |
| 108 | + } |
| 109 | + @PostMapping("/user") |
| 110 | + public Mono<User> createUser(User user){ |
| 111 | + return this.service.create(user); |
| 112 | + } |
| 113 | + @PutMapping("/user/{id}") |
| 114 | + public Mono<ResponseEntity<User>> updateUser(@PathVariable String id,User user){ |
| 115 | + return this.service.updateById(id,user) |
| 116 | + .map(u -> new ResponseEntity<>(u,HttpStatus.OK)) |
| 117 | + .defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND)); |
| 118 | + } |
| 119 | + @DeleteMapping("/user/{id}") |
| 120 | + public Mono<ResponseEntity<Void>> deleteUser(@PathVariable String id){ |
| 121 | + return this.service.deleteById(id) |
| 122 | + .then(Mono.just(new ResponseEntity<Void>(HttpStatus.OK))) |
| 123 | + .defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND)); |
| 124 | + } |
| 125 | + } |
| 126 | +``` |
0 commit comments