Skip to content

Commit 4636d54

Browse files
author
YunaiV
committed
增加 webflux & mongodb 的示例
1 parent a68980c commit 4636d54

File tree

11 files changed

+438
-0
lines changed

11 files changed

+438
-0
lines changed

lab-27/lab-27-webflux-mongodb/pom.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.2.1.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>lab-27-webflux-mongodb</artifactId>
14+
15+
<dependencies>
16+
<!-- 实现对 Spring WebFlux 的自动化配置 -->
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-webflux</artifactId>
20+
<version>2.2.1.RELEASE</version>
21+
</dependency>
22+
23+
<!-- 自动化配置响应式的 Spring Data Mongodb -->
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
27+
</dependency>
28+
29+
<!-- 方便等会写单元测试 -->
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-test</artifactId>
33+
<scope>test</scope>
34+
</dependency>
35+
36+
</dependencies>
37+
38+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springboot.lab27.springwebflux;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.controller;
2+
3+
import cn.iocoder.springboot.lab27.springwebflux.dao.UserRepository;
4+
import cn.iocoder.springboot.lab27.springwebflux.dataobject.UserDO;
5+
import cn.iocoder.springboot.lab27.springwebflux.dto.UserAddDTO;
6+
import cn.iocoder.springboot.lab27.springwebflux.dto.UserUpdateDTO;
7+
import cn.iocoder.springboot.lab27.springwebflux.vo.UserVO;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.web.bind.annotation.*;
10+
import reactor.core.publisher.Flux;
11+
import reactor.core.publisher.Mono;
12+
13+
import java.util.Date;
14+
import java.util.Objects;
15+
import java.util.function.Function;
16+
17+
/**
18+
* 用户 Controller
19+
*/
20+
@RestController
21+
@RequestMapping("/users")
22+
public class UserController {
23+
24+
@Autowired
25+
private UserRepository userRepository;
26+
27+
/**
28+
* 查询用户列表
29+
*
30+
* @return 用户列表
31+
*/
32+
@GetMapping("/list")
33+
public Flux<UserVO> list() {
34+
// 返回列表
35+
return userRepository.findAll()
36+
.map(userDO -> new UserVO().setId(userDO.getId()).setUsername(userDO.getUsername()));
37+
}
38+
39+
/**
40+
* 获得指定用户编号的用户
41+
*
42+
* @param id 用户编号
43+
* @return 用户
44+
*/
45+
@GetMapping("/get")
46+
public Mono<UserVO> get(@RequestParam("id") Integer id) {
47+
// 返回
48+
return userRepository.findById(id)
49+
.map(userDO -> new UserVO().setId(userDO.getId()).setUsername(userDO.getUsername()));
50+
}
51+
52+
/**
53+
* 添加用户
54+
*
55+
* @param addDTO 添加用户信息 DTO
56+
* @return 添加成功的用户编号
57+
*/
58+
@PostMapping("add")
59+
public Mono<Integer> add(UserAddDTO addDTO) {
60+
// 查询用户
61+
Mono<UserDO> user = userRepository.findByUsername(addDTO.getUsername());
62+
63+
// 执行插入
64+
return user.flatMap(new Function<UserDO, Mono<? extends Integer>>() {
65+
66+
@Override
67+
public Mono<? extends Integer> apply(UserDO userDO) {
68+
if (userDO != null) {
69+
// 返回 -1 表示插入失败。
70+
// 实际上,一般是抛出 ServiceException 异常。因为这个示例项目里暂时没做全局异常的定义,所以暂时返回 -1 啦
71+
return Mono.just(-1);
72+
}
73+
// 将 addDTO 转成 UserDO
74+
userDO = new UserDO().setId((int) (System.currentTimeMillis() / 1000)) // 使用当前时间戳的描述,作为 ID 。
75+
.setUsername(addDTO.getUsername())
76+
.setPassword(addDTO.getPassword())
77+
.setCreateTime(new Date());
78+
// 插入数据库
79+
return userRepository.save(userDO).map(UserDO::getId);
80+
}
81+
82+
});
83+
}
84+
85+
/**
86+
* 更新指定用户编号的用户
87+
*
88+
* @param updateDTO 更新用户信息 DTO
89+
* @return 是否修改成功
90+
*/
91+
@PostMapping("/update")
92+
public Mono<Boolean> update(UserUpdateDTO updateDTO) {
93+
// 查询用户
94+
Mono<UserDO> user = userRepository.findById(updateDTO.getId());
95+
96+
// 执行更新
97+
return user.flatMap(new Function<UserDO, Mono<Boolean>>() {
98+
99+
@Override
100+
public Mono<Boolean> apply(UserDO userDO) {
101+
// 如果不存在该用户,则直接返回 false 失败
102+
if (userDO == null) {
103+
return Mono.just(false);
104+
}
105+
// 查询用户是否存在
106+
return userRepository.findByUsername(updateDTO.getUsername())
107+
.flatMap(new Function<UserDO, Mono<? extends Boolean>>() {
108+
109+
@Override
110+
public Mono<? extends Boolean> apply(UserDO usernameUserDO) {
111+
// 如果用户名已经使用
112+
if (usernameUserDO != null && !Objects.equals(updateDTO.getId(), usernameUserDO.getId())) {
113+
return Mono.just(false);
114+
}
115+
// 执行更新
116+
userDO.setUsername(updateDTO.getUsername());
117+
userDO.setPassword(updateDTO.getPassword());
118+
return userRepository.save(userDO).map(userDO -> true); // 返回 true 成功
119+
}
120+
121+
});
122+
}
123+
124+
});
125+
}
126+
127+
/**
128+
* 删除指定用户编号的用户
129+
*
130+
* @param id 用户编号
131+
* @return 是否删除成功
132+
*/
133+
@PostMapping("/delete") // URL 修改成 /delete ,RequestMethod 改成 DELETE
134+
public Mono<Boolean> delete(@RequestParam("id") Integer id) {
135+
// 查询用户
136+
Mono<UserDO> user = userRepository.findById(id);
137+
138+
// 执行删除。这里仅仅是示例,项目中不要物理删除,而是标记删除
139+
return user.flatMap(new Function<UserDO, Mono<? extends Boolean>>() {
140+
141+
@Override
142+
public Mono<? extends Boolean> apply(UserDO userDO) {
143+
// 如果不存在该用户,则直接返回 false 失败
144+
if (userDO == null) {
145+
return Mono.just(false);
146+
}
147+
// 执行删除
148+
return userRepository.deleteById(id).map(aVoid -> true); // 返回 true 成功
149+
}
150+
151+
});
152+
}
153+
154+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.dao;
2+
3+
import cn.iocoder.springboot.lab27.springwebflux.dataobject.UserDO;
4+
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
5+
import reactor.core.publisher.Mono;
6+
7+
public interface UserRepository extends ReactiveMongoRepository<UserDO, Integer> {
8+
9+
Mono<UserDO> findByUsername(String username);
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.dataobject;
2+
3+
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.mongodb.core.mapping.Document;
5+
6+
import java.util.Date;
7+
8+
/**
9+
* 用户 DO
10+
*/
11+
@Document(collection = "User")
12+
public class UserDO {
13+
14+
@Id
15+
private Integer id;
16+
/**
17+
* 账号
18+
*/
19+
private String username;
20+
/**
21+
* 密码
22+
*/
23+
private String password;
24+
/**
25+
* 创建时间
26+
*/
27+
private Date createTime;
28+
29+
public Integer getId() {
30+
return id;
31+
}
32+
33+
public UserDO setId(Integer id) {
34+
this.id = id;
35+
return this;
36+
}
37+
38+
public String getUsername() {
39+
return username;
40+
}
41+
42+
public UserDO setUsername(String username) {
43+
this.username = username;
44+
return this;
45+
}
46+
47+
public String getPassword() {
48+
return password;
49+
}
50+
51+
public UserDO setPassword(String password) {
52+
this.password = password;
53+
return this;
54+
}
55+
56+
public Date getCreateTime() {
57+
return createTime;
58+
}
59+
60+
public UserDO setCreateTime(Date createTime) {
61+
this.createTime = createTime;
62+
return this;
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return "UserDO{" +
68+
"id=" + id +
69+
", username='" + username + '\'' +
70+
", password='" + password + '\'' +
71+
", createTime=" + createTime +
72+
'}';
73+
}
74+
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.dto;
2+
3+
/**
4+
* 用户添加 DTO
5+
*/
6+
public class UserAddDTO {
7+
8+
/**
9+
* 账号
10+
*/
11+
private String username;
12+
/**
13+
* 密码
14+
*/
15+
private String password;
16+
17+
public String getUsername() {
18+
return username;
19+
}
20+
21+
public UserAddDTO setUsername(String username) {
22+
this.username = username;
23+
return this;
24+
}
25+
26+
public String getPassword() {
27+
return password;
28+
}
29+
30+
public UserAddDTO setPassword(String password) {
31+
this.password = password;
32+
return this;
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.dto;
2+
3+
public class UserUpdateDTO {
4+
5+
/**
6+
* 编号
7+
*/
8+
private Integer id;
9+
/**
10+
* 账号
11+
*/
12+
private String username;
13+
/**
14+
* 密码
15+
*/
16+
private String password;
17+
18+
public Integer getId() {
19+
return id;
20+
}
21+
22+
public UserUpdateDTO setId(Integer id) {
23+
this.id = id;
24+
return this;
25+
}
26+
27+
public String getUsername() {
28+
return username;
29+
}
30+
31+
public UserUpdateDTO setUsername(String username) {
32+
this.username = username;
33+
return this;
34+
}
35+
36+
public String getPassword() {
37+
return password;
38+
}
39+
40+
public UserUpdateDTO setPassword(String password) {
41+
this.password = password;
42+
return this;
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.service;
2+
3+
import cn.iocoder.springboot.lab27.springwebflux.vo.UserVO;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
public class UserService {
8+
9+
public UserVO get(Integer id) {
10+
return new UserVO().setId(id).setUsername("test");
11+
}
12+
13+
}

0 commit comments

Comments
 (0)