增加 webflux & mongodb 的示例

This commit is contained in:
YunaiV
2019-11-26 00:25:20 +08:00
parent a68980ca4c
commit 4636d5403b
11 changed files with 438 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lab-27-webflux-mongodb</artifactId>
<dependencies>
<!-- 实现对 Spring WebFlux 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<!-- 自动化配置响应式的 Spring Data Mongodb -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<!-- 方便等会写单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,13 @@
package cn.iocoder.springboot.lab27.springwebflux;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@@ -0,0 +1,154 @@
package cn.iocoder.springboot.lab27.springwebflux.controller;
import cn.iocoder.springboot.lab27.springwebflux.dao.UserRepository;
import cn.iocoder.springboot.lab27.springwebflux.dataobject.UserDO;
import cn.iocoder.springboot.lab27.springwebflux.dto.UserAddDTO;
import cn.iocoder.springboot.lab27.springwebflux.dto.UserUpdateDTO;
import cn.iocoder.springboot.lab27.springwebflux.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.Date;
import java.util.Objects;
import java.util.function.Function;
/**
* 用户 Controller
*/
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserRepository userRepository;
/**
* 查询用户列表
*
* @return 用户列表
*/
@GetMapping("/list")
public Flux<UserVO> list() {
// 返回列表
return userRepository.findAll()
.map(userDO -> new UserVO().setId(userDO.getId()).setUsername(userDO.getUsername()));
}
/**
* 获得指定用户编号的用户
*
* @param id 用户编号
* @return 用户
*/
@GetMapping("/get")
public Mono<UserVO> get(@RequestParam("id") Integer id) {
// 返回
return userRepository.findById(id)
.map(userDO -> new UserVO().setId(userDO.getId()).setUsername(userDO.getUsername()));
}
/**
* 添加用户
*
* @param addDTO 添加用户信息 DTO
* @return 添加成功的用户编号
*/
@PostMapping("add")
public Mono<Integer> add(UserAddDTO addDTO) {
// 查询用户
Mono<UserDO> user = userRepository.findByUsername(addDTO.getUsername());
// 执行插入
return user.flatMap(new Function<UserDO, Mono<? extends Integer>>() {
@Override
public Mono<? extends Integer> apply(UserDO userDO) {
if (userDO != null) {
// 返回 -1 表示插入失败。
// 实际上,一般是抛出 ServiceException 异常。因为这个示例项目里暂时没做全局异常的定义,所以暂时返回 -1 啦
return Mono.just(-1);
}
// 将 addDTO 转成 UserDO
userDO = new UserDO().setId((int) (System.currentTimeMillis() / 1000)) // 使用当前时间戳的描述,作为 ID 。
.setUsername(addDTO.getUsername())
.setPassword(addDTO.getPassword())
.setCreateTime(new Date());
// 插入数据库
return userRepository.save(userDO).map(UserDO::getId);
}
});
}
/**
* 更新指定用户编号的用户
*
* @param updateDTO 更新用户信息 DTO
* @return 是否修改成功
*/
@PostMapping("/update")
public Mono<Boolean> update(UserUpdateDTO updateDTO) {
// 查询用户
Mono<UserDO> user = userRepository.findById(updateDTO.getId());
// 执行更新
return user.flatMap(new Function<UserDO, Mono<Boolean>>() {
@Override
public Mono<Boolean> apply(UserDO userDO) {
// 如果不存在该用户,则直接返回 false 失败
if (userDO == null) {
return Mono.just(false);
}
// 查询用户是否存在
return userRepository.findByUsername(updateDTO.getUsername())
.flatMap(new Function<UserDO, Mono<? extends Boolean>>() {
@Override
public Mono<? extends Boolean> apply(UserDO usernameUserDO) {
// 如果用户名已经使用
if (usernameUserDO != null && !Objects.equals(updateDTO.getId(), usernameUserDO.getId())) {
return Mono.just(false);
}
// 执行更新
userDO.setUsername(updateDTO.getUsername());
userDO.setPassword(updateDTO.getPassword());
return userRepository.save(userDO).map(userDO -> true); // 返回 true 成功
}
});
}
});
}
/**
* 删除指定用户编号的用户
*
* @param id 用户编号
* @return 是否删除成功
*/
@PostMapping("/delete") // URL 修改成 /delete RequestMethod 改成 DELETE
public Mono<Boolean> delete(@RequestParam("id") Integer id) {
// 查询用户
Mono<UserDO> user = userRepository.findById(id);
// 执行删除。这里仅仅是示例,项目中不要物理删除,而是标记删除
return user.flatMap(new Function<UserDO, Mono<? extends Boolean>>() {
@Override
public Mono<? extends Boolean> apply(UserDO userDO) {
// 如果不存在该用户,则直接返回 false 失败
if (userDO == null) {
return Mono.just(false);
}
// 执行删除
return userRepository.deleteById(id).map(aVoid -> true); // 返回 true 成功
}
});
}
}

View File

@@ -0,0 +1,11 @@
package cn.iocoder.springboot.lab27.springwebflux.dao;
import cn.iocoder.springboot.lab27.springwebflux.dataobject.UserDO;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import reactor.core.publisher.Mono;
public interface UserRepository extends ReactiveMongoRepository<UserDO, Integer> {
Mono<UserDO> findByUsername(String username);
}

View File

@@ -0,0 +1,75 @@
package cn.iocoder.springboot.lab27.springwebflux.dataobject;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date;
/**
* 用户 DO
*/
@Document(collection = "User")
public class UserDO {
@Id
private Integer id;
/**
* 账号
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 创建时间
*/
private Date createTime;
public Integer getId() {
return id;
}
public UserDO setId(Integer id) {
this.id = id;
return this;
}
public String getUsername() {
return username;
}
public UserDO setUsername(String username) {
this.username = username;
return this;
}
public String getPassword() {
return password;
}
public UserDO setPassword(String password) {
this.password = password;
return this;
}
public Date getCreateTime() {
return createTime;
}
public UserDO setCreateTime(Date createTime) {
this.createTime = createTime;
return this;
}
@Override
public String toString() {
return "UserDO{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", createTime=" + createTime +
'}';
}
}

View File

@@ -0,0 +1,35 @@
package cn.iocoder.springboot.lab27.springwebflux.dto;
/**
* 用户添加 DTO
*/
public class UserAddDTO {
/**
* 账号
*/
private String username;
/**
* 密码
*/
private String password;
public String getUsername() {
return username;
}
public UserAddDTO setUsername(String username) {
this.username = username;
return this;
}
public String getPassword() {
return password;
}
public UserAddDTO setPassword(String password) {
this.password = password;
return this;
}
}

View File

@@ -0,0 +1,45 @@
package cn.iocoder.springboot.lab27.springwebflux.dto;
public class UserUpdateDTO {
/**
* 编号
*/
private Integer id;
/**
* 账号
*/
private String username;
/**
* 密码
*/
private String password;
public Integer getId() {
return id;
}
public UserUpdateDTO setId(Integer id) {
this.id = id;
return this;
}
public String getUsername() {
return username;
}
public UserUpdateDTO setUsername(String username) {
this.username = username;
return this;
}
public String getPassword() {
return password;
}
public UserUpdateDTO setPassword(String password) {
this.password = password;
return this;
}
}

View File

@@ -0,0 +1,13 @@
package cn.iocoder.springboot.lab27.springwebflux.service;
import cn.iocoder.springboot.lab27.springwebflux.vo.UserVO;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public UserVO get(Integer id) {
return new UserVO().setId(id).setUsername("test");
}
}

View File

@@ -0,0 +1,35 @@
package cn.iocoder.springboot.lab27.springwebflux.vo;
/**
* 用户 VO
*/
public class UserVO {
/**
* 编号
*/
private Integer id;
/**
* 账号
*/
private String username;
public Integer getId() {
return id;
}
public UserVO setId(Integer id) {
this.id = id;
return this;
}
public String getUsername() {
return username;
}
public UserVO setUsername(String username) {
this.username = username;
return this;
}
}

View File

@@ -0,0 +1,18 @@
spring:
data:
# MongoDB 配置项,对应 MongoProperties 类
mongodb:
host: 127.0.0.1
port: 27017
database: yourdatabase
username: test01
password: password01
# 上述属性,也可以只配置 uri
logging:
level:
org:
springframework:
data:
mongodb:
core: DEBUG # 打印 mongodb 操作的具体语句。生产环境下,不建议开启。

View File

@@ -15,6 +15,7 @@
<module>lab-27-webflux-01</module>
<module>lab-27-webflux-02</module>
<module>lab-27-webflux-03</module>
<module>lab-27-webflux-mongodb</module>
</modules>