Skip to content

Commit 1975b9b

Browse files
author
liqiangqiang
committed
Spring Boot WebFlux 中 Thymeleaf 和 Mongodb 实践
1 parent 7f68137 commit 1975b9b

File tree

9 files changed

+329
-0
lines changed

9 files changed

+329
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>springboot</groupId>
7+
<artifactId>springboot-webflux-5-thymeleaf-mongodb</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<name>springboot-webflux-5-thymeleaf-mongodb :: Spring Boot WebFlux 中 Thymeleaf 和 Mongodb 实践</name>
10+
11+
<!-- Spring Boot 启动父依赖 -->
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>2.0.1.RELEASE</version>
16+
</parent>
17+
18+
<dependencies>
19+
20+
<!-- Spring Boot Web Flux 依赖 -->
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-webflux</artifactId>
24+
</dependency>
25+
26+
<!-- Spring Boot 响应式 MongoDB 依赖 -->
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
30+
</dependency>
31+
32+
<!-- 模板引擎 Thymeleaf 依赖 -->
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
36+
</dependency>
37+
38+
<!-- Spring Boot Test 依赖 -->
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-test</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
45+
<!-- Junit -->
46+
<dependency>
47+
<groupId>junit</groupId>
48+
<artifactId>junit</artifactId>
49+
<version>4.12</version>
50+
</dependency>
51+
</dependencies>
52+
53+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.spring.springboot;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* Spring Boot 应用启动类
8+
*
9+
*/
10+
// Spring Boot 应用的标识
11+
@SpringBootApplication
12+
public class Application {
13+
14+
public static void main(String[] args) {
15+
// 程序启动入口
16+
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
17+
SpringApplication.run(Application.class,args);
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.spring.springboot.dao;
2+
3+
import org.spring.springboot.domain.City;
4+
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
5+
import org.springframework.stereotype.Repository;
6+
import reactor.core.publisher.Mono;
7+
8+
@Repository
9+
public interface CityRepository extends ReactiveMongoRepository<City, Long> {
10+
11+
Mono<City> findByCityName(String cityName);
12+
13+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.spring.springboot.domain;
2+
3+
/**
4+
* 城市实体类
5+
*
6+
*/
7+
public class City {
8+
9+
/**
10+
* 城市编号
11+
*/
12+
private Long id;
13+
14+
/**
15+
* 省份编号
16+
*/
17+
private Long provinceId;
18+
19+
/**
20+
* 城市名称
21+
*/
22+
private String cityName;
23+
24+
/**
25+
* 描述
26+
*/
27+
private String description;
28+
29+
public Long getId() {
30+
return id;
31+
}
32+
33+
public void setId(Long id) {
34+
this.id = id;
35+
}
36+
37+
public Long getProvinceId() {
38+
return provinceId;
39+
}
40+
41+
public void setProvinceId(Long provinceId) {
42+
this.provinceId = provinceId;
43+
}
44+
45+
public String getCityName() {
46+
return cityName;
47+
}
48+
49+
public void setCityName(String cityName) {
50+
this.cityName = cityName;
51+
}
52+
53+
public String getDescription() {
54+
return description;
55+
}
56+
57+
public void setDescription(String description) {
58+
this.description = description;
59+
}
60+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.spring.springboot.handler;
2+
3+
import org.spring.springboot.dao.CityRepository;
4+
import org.spring.springboot.domain.City;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Component;
7+
import reactor.core.publisher.Flux;
8+
import reactor.core.publisher.Mono;
9+
10+
@Component
11+
public class CityHandler {
12+
13+
private final CityRepository cityRepository;
14+
15+
@Autowired
16+
public CityHandler(CityRepository cityRepository) {
17+
this.cityRepository = cityRepository;
18+
}
19+
20+
public Mono<City> save(City city) {
21+
return cityRepository.save(city);
22+
}
23+
24+
public Mono<City> findCityById(Long id) {
25+
26+
return cityRepository.findById(id);
27+
}
28+
29+
public Flux<City> findAllCity() {
30+
31+
return cityRepository.findAll();
32+
}
33+
34+
public Mono<City> modifyCity(City city) {
35+
36+
return cityRepository.save(city);
37+
}
38+
39+
public Mono<Long> deleteCity(Long id) {
40+
cityRepository.deleteById(id);
41+
return Mono.create(cityMonoSink -> cityMonoSink.success(id));
42+
}
43+
44+
public Mono<City> getByCityName(String cityName) {
45+
return cityRepository.findByCityName(cityName);
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.spring.springboot.webflux.controller;
2+
3+
import org.spring.springboot.domain.City;
4+
import org.spring.springboot.handler.CityHandler;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.Model;
8+
import org.springframework.web.bind.annotation.*;
9+
import reactor.core.publisher.Flux;
10+
import reactor.core.publisher.Mono;
11+
12+
@Controller
13+
@RequestMapping(value = "/city")
14+
public class CityWebFluxController {
15+
16+
@Autowired
17+
private CityHandler cityHandler;
18+
19+
@GetMapping(value = "/{id}")
20+
@ResponseBody
21+
public Mono<City> findCityById(@PathVariable("id") Long id) {
22+
return cityHandler.findCityById(id);
23+
}
24+
25+
@GetMapping()
26+
@ResponseBody
27+
public Flux<City> findAllCity() {
28+
return cityHandler.findAllCity();
29+
}
30+
31+
@PostMapping()
32+
@ResponseBody
33+
public Mono<City> saveCity(@RequestBody City city) {
34+
return cityHandler.save(city);
35+
}
36+
37+
@PutMapping()
38+
@ResponseBody
39+
public Mono<City> modifyCity(@RequestBody City city) {
40+
return cityHandler.modifyCity(city);
41+
}
42+
43+
@DeleteMapping(value = "/{id}")
44+
@ResponseBody
45+
public Mono<Long> deleteCity(@PathVariable("id") Long id) {
46+
return cityHandler.deleteCity(id);
47+
}
48+
49+
private static final String CITY_LIST_PATH_NAME = "cityList";
50+
private static final String CITY_PATH_NAME = "city";
51+
52+
@GetMapping("/page/list")
53+
public String listPage(final Model model) {
54+
final Flux<City> cityFluxList = cityHandler.findAllCity();
55+
model.addAttribute("cityList", cityFluxList);
56+
return CITY_LIST_PATH_NAME;
57+
}
58+
59+
@GetMapping("/getByName")
60+
public String getByCityName(final Model model,
61+
@RequestParam("cityName") String cityName) {
62+
final Mono<City> city = cityHandler.getByCityName(cityName);
63+
model.addAttribute("city", city);
64+
return CITY_PATH_NAME;
65+
}
66+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
spring.data.mongodb.host=localhost
2+
spring.data.mongodb.database=admin
3+
spring.data.mongodb.port=27017
4+
spring.data.mongodb.username=admin
5+
spring.data.mongodb.password=admin
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html lang="zh-CN">
3+
<head>
4+
<meta charset="UTF-8"/>
5+
<title>城市</title>
6+
</head>
7+
8+
<body>
9+
10+
<div>
11+
12+
13+
<table>
14+
<legend>
15+
<strong>城市单个查询</strong>
16+
</legend>
17+
<tbody>
18+
<td th:text="${city.id}"></td>
19+
<td th:text="${city.provinceId}"></td>
20+
<td th:text="${city.cityName}"></td>
21+
<td th:text="${city.description}"></td>
22+
</tbody>
23+
</table>
24+
25+
</div>
26+
27+
</body>
28+
</html>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html lang="zh-CN">
3+
<head>
4+
<meta charset="UTF-8"/>
5+
<title>城市列表</title>
6+
</head>
7+
8+
<body>
9+
10+
<div>
11+
12+
13+
<table>
14+
<legend>
15+
<strong>城市列表</strong>
16+
</legend>
17+
<thead>
18+
<tr>
19+
<th>城市编号</th>
20+
<th>省份编号</th>
21+
<th>名称</th>
22+
<th>描述</th>
23+
</tr>
24+
</thead>
25+
<tbody>
26+
<tr th:each="city : ${cityList}">
27+
<td th:text="${city.id}"></td>
28+
<td th:text="${city.provinceId}"></td>
29+
<td th:text="${city.cityName}"></td>
30+
<td th:text="${city.description}"></td>
31+
</tr>
32+
</tbody>
33+
</table>
34+
35+
</div>
36+
37+
</body>
38+
</html>

0 commit comments

Comments
 (0)