Spring Boot WebFlux整合mongoDB
引入maven文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
创建配置文件
spring:
data:
mongodb:
host: 127.0.0.1
port: 27017
username: mickey
password: 123456
database: mongoTest
创建document实体类
@Data
@Document("person")
public class PersonEntity { @Id
private String id; private String userName; private String gender; /**
* 设置TTL,单位秒
*/
@Indexed(name = "idx_create_time", expireAfterSeconds = 10)
private Date createTime = new Date(); }
创建repository
@Repository
public interface PersonRepository extends ReactiveMongoRepository<PersonEntity,String> { /**
* 根据name查找Person
* @param name
* @return
*/
Flux<PersonEntity> findByUserName(String name);
}
编写controller
package com.xzsx.openapi.thirdparty.controller; import com.xzsx.openapi.dto.MongoDBOutput;
import com.xzsx.openapi.thirdparty.entity.PersonEntity;
import com.xzsx.openapi.thirdparty.repository.PersonRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; /**
* @author J·K
* @Description: TODO
* @date 2019-04-22 10:19
*/
@RestController
public class IndexController
{
/**
* 可以使用
*/
@Autowired
private PersonRepository personRepository; @GetMapping("/save")
public Mono<PersonEntity> save(){
PersonEntity person = new PersonEntity();
person.setUserName("mickey");
person.setGender("male");
return personRepository.save(person);
} @GetMapping("/list")
public Flux<MongoDBOutput> list(){
Flux<MongoDBOutput> flux = personRepository.findAll().map(x->{
MongoDBOutput mongoDBOutput = new MongoDBOutput();
BeanUtils.copyProperties(x,mongoDBOutput);
return mongoDBOutput;
});
return flux;
} @GetMapping("/delete/{id}")
public Mono<String> delete(@PathVariable("id") String id){
//没有返回值
// personRepository.deleteById(id); //如果要操作数据,并返回一个Mono,这时候使用flatMap
//如果不操作数据,只是对数据做一个转换,使用map
return personRepository.findById(id)
.flatMap(x->
personRepository.deleteById(x.getId())
.then(Mono.just("ok")))
.defaultIfEmpty("not found");
} @GetMapping("/update/{id}")
public Mono<String> update(@PathVariable("id") String id){
return personRepository.findById(id)
.flatMap(x->{
x.setUserName("jack");
return personRepository.save(x);
})
.map(x->x.toString())
.defaultIfEmpty("error");
} @GetMapping("/find/{id}")
public Mono<PersonEntity> findById(@PathVariable("id") String id){
return personRepository.findById(id)
.map(x-> x)
.defaultIfEmpty(new PersonEntity());
} @GetMapping("/findByName/{name}")
public Flux<PersonEntity> findByName(@PathVariable("name") String name){
return personRepository.findByUserName(name)
.map(x-> x)
.defaultIfEmpty(new PersonEntity());
} @GetMapping(value = "/stream/findByName/{name}",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<PersonEntity> findByName1(@PathVariable("name") String name){
return personRepository.findByUserName(name)
.map(x-> x)
.defaultIfEmpty(new PersonEntity());
} public static void main(String[] args) {
String [] strs = {"1","2","3"};
// List<Integer> collect = Flux.fromArray(strs).map(x -> Integer.parseInt(x))
// .toStream().collect(Collectors.toList());
// System.out.println(collect); // Flux.fromArray(strs).map(x->Integer.parseInt(x))
// .subscribe(x->{
// System.out.println(x);
// }); Flux.fromArray(strs).map(x->{
throw new RuntimeException("error");
})
.subscribe(x->{
System.out.println(x);
},x->{
System.out.println("error");
}); } }
启动项目后,就可以按原来访问springboot的方式访问数据了
Spring Boot WebFlux整合mongoDB的更多相关文章
- Spring Boot WebFlux 集成 Mongodb 数据源操作
WebFlux 整合 Mongodb 前言 上一讲用 Map 数据结构内存式存储了数据.这样数据就不会持久化,本文我们用 MongoDB 来实现 WebFlux 对数据源的操作. 什么是 MongoD ...
- Spring Boot WebFlux-03——WebFlux 整合 MongoDB
第03课:WebFlux 整合 MongoDB 前言 上一课的内容讲解了用 Map 数据结构内存式存储了数据,这样数据就不会持久化,本文我们用 MongoDB 来实现 WebFlux 对数据源的操作. ...
- Spring Boot WebFlux 快速入门实践
02:WebFlux 快速入门实践 Spring Boot 2.0 spring.io 官网有句醒目的话是: BUILD ANYTHING WITH SPRING BOOT Spring Boot ( ...
- Spring Boot (十四): 响应式编程以及 Spring Boot Webflux 快速入门
1. 什么是响应式编程 在计算机中,响应式编程或反应式编程(英语:Reactive programming)是一种面向数据流和变化传播的编程范式.这意味着可以在编程语言中很方便地表达静态或动态的数据流 ...
- Spring Boot Security 整合 JWT 实现 无状态的分布式API接口
简介 JSON Web Token(缩写 JWT)是目前最流行的跨域认证解决方案.JSON Web Token 入门教程 - 阮一峰,这篇文章可以帮你了解JWT的概念.本文重点讲解Spring Boo ...
- Spring boot Mybatis 整合(完整版)
个人开源项目 springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页.个人作品.个人博客) 朋友自制的springboot接口文档组件swagge ...
- Spring boot Mybatis 整合
PS: 参考博客 PS: spring boot配置mybatis和事务管理 PS: Spring boot Mybatis 整合(完整版) 这篇博客里用到了怎样 生成 mybatis 插件来写程 ...
- spring boot 学习(二)spring boot 框架整合 thymeleaf
spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...
- Spring Boot 应用系列 5 -- Spring Boot 2 整合logback
上一篇我们梳理了Spring Boot 2 整合log4j2的配置过程,其中讲到了Spring Boot 2原装适配logback,并且在非异步环境下logback和log4j2的性能差别不大,所以对 ...
随机推荐
- 初识消息中间件之 ==> ActiveMQ
一.消息队列概述 消息(Message)是指在应用间传送的数据.消息可以非常简单,比如只包含文本字符串,也可以更复杂,可能包含嵌入对象. 消息队列(Message Queue)是一种应用间的通信方式, ...
- delphi设置鼠标图形
//Screen.Cursor := crHourGlass;//忙 //Screen.Cursor := crDefault;//不忙时
- JAVA 基础编程练习题16 【程序 16 输入 9*9 表】
16 [程序 16 输入 9*9 表] 题目:输出 9*9 口诀. 程序分析:分行与列考虑,共 9 行 9 列,i 控制行,j 控制列. package cskaoyan; public class ...
- JAVA 基础编程练习题9 【程序 9 求完数】
9 [程序 9 求完数] 题目:一个数如果恰好等于它的因子之和,这个数就称为"完数".例如 6=1+2+3.编程找出 1000 以内的 所有完数. package cskaoyan ...
- ElasticSearch——Logstash输出到Elasticsearch配置
位置 在Logstash的.conf配置文件中的output中配置ElasticSearch 示例: output { elasticsearch{ action => "index& ...
- phoenix创建表失败:phoenixIOException: Max attempts exceeded
下面的问题,搞了1天才解决,太坑了,在这里记录一下. 问题现像:执行命令后,1分钟没有返回, 然后报下面的错,偶尔会出现以下不同的报错信息. jdbc:phoenix:10.0.xx.1:2181&g ...
- ASP.NET Core 入门笔记4,ASP.NET Core MVC路由入门
敲了一部分,懒得全部敲完,直接复制大佬的博客了,如有侵权,请通知我尽快删除修改 摘抄自https://www.cnblogs.com/ken-io/p/aspnet-core-tutorial-mvc ...
- 【并行计算-CUDA开发】CUDA并行存储模型
CUDA并行存储模型 CUDA将CPU作为主机(Host),GPU作为设备(Device).一个系统中可以有一个主机和多个设备.CPU负责逻辑性强的事务处理和串行计算,GPU专注于执行高度线程化的并行 ...
- JavaScript 控制台打印window对象
示例代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- Vuecli 3.0 项目自定义添加静态目录,支持在index.html引入
参考链接:https://blog.csdn.net/qq_15253407/article/details/89491255