forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Nov 26, 2019
1 parent
4636d54
commit f6b91bb
Showing
22 changed files
with
759 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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-elasticsearch</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 Elasticsearch --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-elasticsearch</artifactId> | ||
</dependency> | ||
|
||
<!-- 方便等会写单元测试 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
14 changes: 14 additions & 0 deletions
14
...ux-elasticsearch/src/main/java/cn/iocoder/springboot/lab27/springwebflux/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
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) { | ||
// System.setProperty("es.set.netty.runtime.available.processors", "false"); | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...ain/java/cn/iocoder/springboot/lab27/springwebflux/config/ElasticsearchConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package cn.iocoder.springboot.lab27.springwebflux.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.elasticsearch.repository.config.EnableReactiveElasticsearchRepositories; | ||
|
||
@Configuration | ||
@EnableReactiveElasticsearchRepositories // 开启响应式的 Elasticsearch 的 Repository 的自动化配置 | ||
public class ElasticsearchConfiguration { | ||
} |
161 changes: 161 additions & 0 deletions
161
...ch/src/main/java/cn/iocoder/springboot/lab27/springwebflux/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
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 { | ||
|
||
private static final UserDO USER_NULL = new UserDO(); | ||
|
||
@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.defaultIfEmpty(USER_NULL) // 设置 USER_NULL 作为 null 的情况,否则 flatMap 不会往下走 | ||
.flatMap(new Function<UserDO, Mono<Integer>>() { | ||
|
||
@Override | ||
public Mono<Integer> apply(UserDO userDO) { | ||
if (userDO != USER_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.defaultIfEmpty(USER_NULL) // 设置 USER_NULL 作为 null 的情况,否则 flatMap 不会往下走 | ||
.flatMap(new Function<UserDO, Mono<Boolean>>() { | ||
|
||
@Override | ||
public Mono<Boolean> apply(UserDO userDO) { | ||
// 如果不存在该用户,则直接返回 false 失败 | ||
if (userDO == USER_NULL) { | ||
return Mono.just(false); | ||
} | ||
// 查询用户是否存在 | ||
return userRepository.findByUsername(updateDTO.getUsername()) | ||
.defaultIfEmpty(USER_NULL) // 设置 USER_NULL 作为 null 的情况,否则 flatMap 不会往下走 | ||
.flatMap(new Function<UserDO, Mono<? extends Boolean>>() { | ||
|
||
@Override | ||
public Mono<? extends Boolean> apply(UserDO usernameUserDO) { | ||
// 如果用户名已经使用(该用户名对应的 id 不是自己,说明就已经被使用了) | ||
if (usernameUserDO != USER_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.defaultIfEmpty(USER_NULL) // 设置 USER_NULL 作为 null 的情况,否则 flatMap 不会往下走 | ||
.flatMap(new Function<UserDO, Mono<Boolean>>() { | ||
|
||
@Override | ||
public Mono<Boolean> apply(UserDO userDO) { | ||
// 如果不存在该用户,则直接返回 false 失败 | ||
if (userDO == USER_NULL) { | ||
return Mono.just(false); | ||
} | ||
// 执行删除 | ||
return userRepository.deleteById(id).map(aVoid -> true); // 返回 true 成功 | ||
} | ||
|
||
}); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...ticsearch/src/main/java/cn/iocoder/springboot/lab27/springwebflux/dao/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cn.iocoder.springboot.lab27.springwebflux.dao; | ||
|
||
import cn.iocoder.springboot.lab27.springwebflux.dataobject.UserDO; | ||
import org.springframework.data.elasticsearch.repository.ReactiveElasticsearchRepository; | ||
import reactor.core.publisher.Mono; | ||
|
||
public interface UserRepository extends ReactiveElasticsearchRepository<UserDO, Integer> { | ||
|
||
Mono<UserDO> findByUsername(String username); | ||
|
||
} | ||
|
||
//public interface UserRepository extends ElasticsearchRepository<UserDO, Integer> { | ||
// | ||
//// Mono<UserDO> findByUsername(String username); | ||
// | ||
//} |
80 changes: 80 additions & 0 deletions
80
...sticsearch/src/main/java/cn/iocoder/springboot/lab27/springwebflux/dataobject/UserDO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package cn.iocoder.springboot.lab27.springwebflux.dataobject; | ||
|
||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.elasticsearch.annotations.Document; | ||
|
||
import java.util.Date; | ||
|
||
@Document(indexName = "user", // 索引名 | ||
type = "user", // 类型。未来的版本即将废弃 | ||
shards = 1, // 默认索引分区数 | ||
replicas = 0, // 每个分区的备份数 | ||
refreshInterval = "-1" // 刷新间隔 | ||
) | ||
public class UserDO { | ||
|
||
/** | ||
* ID 主键 | ||
*/ | ||
@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 + | ||
'}'; | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...elasticsearch/src/main/java/cn/iocoder/springboot/lab27/springwebflux/dto/UserAddDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
Oops, something went wrong.