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的性能差别不大,所以对 ...
随机推荐
- nexus私服库被误删如何恢复
恢复步骤: 1,登录nexus服务器,找到nexus安装目录(默认/usr/local/下): 2,找到sonatype-work/nexus/trash 下找到你删除的库: 3,copy到指定的so ...
- iptables基本命令到深入
1.关闭firewalld,安装iptables-server并启动服务 systemctl stop firewalld systemctl disable firewalld yun -y ins ...
- vue-wechat-title动态修改title
在使用Vue制作项目的过程中,发现title没有变化 所以使用vue-wechat-title插件动态修改tilte 一.安装 npm vue-wechat-title --save 二.引入和使用 ...
- python常见队列queue分类
import queue # 1.普通q# 2.先进后出q# 3.优先级q 普通Queue q=queue.Queue(3)q.put(1)q.put(2)q.put(3)print(q.get()) ...
- ZOJ Problem Set - 1009
1.参考 http://blog.csdn.net/xiaogugood/article/details/17922105 这篇博客对算法介绍的很详细,我看这道题的时候,将题目理解出错,所以进入了一个 ...
- vim中ctrl+z,jobs,fg命令用法
例如:vi test.cctrl + z 将vi 打开的test.c停止并转到后台又打开一个vi buf.cctrl + z 将vi 打开的buf.c停止并转到后台此时通过jobs可以看到任务列表通过 ...
- php学习历程1——注册、登录(面向过程、面向对象)
首先放一张天空之城 Php入门来的第一个小项目,首先做的是一个简陋的文章管理系统.有登录.注册.文章list.添加文章.修改文章.删除文章.分页这几个小功能. 面向过程的编码 面向对象的编码 首先做的 ...
- python安装OpenCV后import cv2报错解决办法
现在python安装完成后,运行pip install opencv-python安装成功后,import cv2时会失败 看到有人给出下载https://www.lfd.uci.edu/~gohlk ...
- 使用JWT的ASP.NET CORE令牌身份验证和授权(无Cookie)——第1部分
原文:使用JWT的ASP.NET CORE令牌身份验证和授权(无Cookie)--第1部分 原文链接:https://www.codeproject.com/Articles/5160941/ASP- ...
- dev grid的一些使用
保留选中数据,其他数据删除,不操作数据库 private void butnoremove_Click(object sender, EventArgs e) { int iSelectRowCoun ...