Spring Boot WebFlux整合mongoDB

引入maven文件

1<dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter</artifactId> 4</dependency> 5<dependency> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-webflux</artifactId> 8</dependency> 9<dependency> 10 <groupId>org.springframework.boot</groupId> 11 <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId> 12</dependency>

创建配置文件

1spring: 2 data: 3 mongodb: 4 host: 127.0.0.1 5 port: 27017 6 username: mickey 7 password: 123456 8 database: mongoTest

创建document实体类

1@Data 2@Document("person") 3public class PersonEntity { 4 5 @Id 6 private String id; 7 8 private String userName; 9 10 private String gender; 11 12 /** 13 * 设置TTL,单位秒 14 */ 15 @Indexed(name = "idx_create_time", expireAfterSeconds = 10) 16 private Date createTime = new Date(); 17 18}

创建repository

1@Repository 2public interface PersonRepository extends ReactiveMongoRepository<PersonEntity,String> { 3 4 /** 5 * 根据name查找Person 6 * @param name 7 * @return 8 */ 9 Flux<PersonEntity> findByUserName(String name); 10}

编写controller

1package com.xzsx.openapi.thirdparty.controller; 2 3import com.xzsx.openapi.dto.MongoDBOutput; 4import com.xzsx.openapi.thirdparty.entity.PersonEntity; 5import com.xzsx.openapi.thirdparty.repository.PersonRepository; 6import org.springframework.beans.BeanUtils; 7import org.springframework.beans.factory.annotation.Autowired; 8import org.springframework.http.MediaType; 9import org.springframework.web.bind.annotation.GetMapping; 10import org.springframework.web.bind.annotation.PathVariable; 11import org.springframework.web.bind.annotation.RestController; 12import reactor.core.publisher.Flux; 13import reactor.core.publisher.Mono; 14 15/** 16 * @author J·K 17 * @Description: TODO 18 * @date 2019-04-22 10:19 19 */ 20@RestController 21public class IndexController 22{ 23 /** 24 * 可以使用 25 */ 26 @Autowired 27 private PersonRepository personRepository; 28 29 @GetMapping("/save") 30 public Mono<PersonEntity> save(){ 31 PersonEntity person = new PersonEntity(); 32 person.setUserName("mickey"); 33 person.setGender("male"); 34 return personRepository.save(person); 35 } 36 37 @GetMapping("/list") 38 public Flux<MongoDBOutput> list(){ 39 Flux<MongoDBOutput> flux = personRepository.findAll().map(x->{ 40 MongoDBOutput mongoDBOutput = new MongoDBOutput(); 41 BeanUtils.copyProperties(x,mongoDBOutput); 42 return mongoDBOutput; 43 }); 44 return flux; 45 } 46 47 @GetMapping("/delete/{id}") 48 public Mono<String> delete(@PathVariable("id") String id){ 49 //没有返回值 50// personRepository.deleteById(id); 51 52 //如果要操作数据,并返回一个Mono,这时候使用flatMap 53 //如果不操作数据,只是对数据做一个转换,使用map 54 return personRepository.findById(id) 55 .flatMap(x-> 56 personRepository.deleteById(x.getId()) 57 .then(Mono.just("ok"))) 58 .defaultIfEmpty("not found"); 59 } 60 61 @GetMapping("/update/{id}") 62 public Mono<String> update(@PathVariable("id") String id){ 63 return personRepository.findById(id) 64 .flatMap(x->{ 65 x.setUserName("jack"); 66 return personRepository.save(x); 67 }) 68 .map(x->x.toString()) 69 .defaultIfEmpty("error"); 70 } 71 72 @GetMapping("/find/{id}") 73 public Mono<PersonEntity> findById(@PathVariable("id") String id){ 74 return personRepository.findById(id) 75 .map(x-> x) 76 .defaultIfEmpty(new PersonEntity()); 77 } 78 79 @GetMapping("/findByName/{name}") 80 public Flux<PersonEntity> findByName(@PathVariable("name") String name){ 81 return personRepository.findByUserName(name) 82 .map(x-> x) 83 .defaultIfEmpty(new PersonEntity()); 84 } 85 86 @GetMapping(value = "/stream/findByName/{name}",produces = MediaType.TEXT_EVENT_STREAM_VALUE) 87 public Flux<PersonEntity> findByName1(@PathVariable("name") String name){ 88 return personRepository.findByUserName(name) 89 .map(x-> x) 90 .defaultIfEmpty(new PersonEntity()); 91 } 92 93 public static void main(String[] args) { 94 String [] strs = {"1","2","3"}; 95// List<Integer> collect = Flux.fromArray(strs).map(x -> Integer.parseInt(x)) 96// .toStream().collect(Collectors.toList()); 97// System.out.println(collect); 98 99 100// Flux.fromArray(strs).map(x->Integer.parseInt(x)) 101// .subscribe(x->{ 102// System.out.println(x); 103// }); 104 105 Flux.fromArray(strs).map(x->{ 106 throw new RuntimeException("error"); 107 }) 108 .subscribe(x->{ 109 System.out.println(x); 110 },x->{ 111 System.out.println("error"); 112 }); 113 114 } 115 116}

启动项目后,就可以按原来访问springboot的方式访问数据了

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Spring Boot WebFlux整合mongoDB - HelloWorld