File tree Expand file tree Collapse file tree 7 files changed +229
-0
lines changed
springboot-webflux-3-mongodb
java/org/spring/springboot Expand file tree Collapse file tree 7 files changed +229
-0
lines changed Original file line number Diff line number Diff line change
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-3-mongodb</artifactId >
8
+ <version >0.0.1-SNAPSHOT</version >
9
+ <name >springboot-webflux-3-mongodb :: Spring Boot WebFlux 整合 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
+ <!-- Spring Boot Test 依赖 -->
33
+ <dependency >
34
+ <groupId >org.springframework.boot</groupId >
35
+ <artifactId >spring-boot-starter-test</artifactId >
36
+ <scope >test</scope >
37
+ </dependency >
38
+
39
+ <!-- Junit -->
40
+ <dependency >
41
+ <groupId >junit</groupId >
42
+ <artifactId >junit</artifactId >
43
+ <version >4.12</version >
44
+ </dependency >
45
+ </dependencies >
46
+
47
+ </project >
Original file line number Diff line number Diff line change
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
+ * Created by bysocket on 09/29/2017.
10
+ */
11
+ // Spring Boot 应用的标识
12
+ @ SpringBootApplication
13
+ public class Application {
14
+
15
+ public static void main (String [] args ) {
16
+ // 程序启动入口
17
+ // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18
+ SpringApplication .run (Application .class ,args );
19
+ }
20
+ }
Original file line number Diff line number Diff line change
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
+
7
+ @ Repository
8
+ public interface CityRepository extends ReactiveMongoRepository <City , Long > {
9
+
10
+ }
Original file line number Diff line number Diff line change
1
+ package org .spring .springboot .domain ;
2
+
3
+ import org .springframework .data .annotation .Id ;
4
+
5
+ /**
6
+ * 城市实体类
7
+ *
8
+ */
9
+ public class City {
10
+
11
+ /**
12
+ * 城市编号
13
+ */
14
+ @ Id
15
+ private Long id ;
16
+
17
+ /**
18
+ * 省份编号
19
+ */
20
+ private Long provinceId ;
21
+
22
+ /**
23
+ * 城市名称
24
+ */
25
+ private String cityName ;
26
+
27
+ /**
28
+ * 描述
29
+ */
30
+ private String description ;
31
+
32
+ public Long getId () {
33
+ return id ;
34
+ }
35
+
36
+ public void setId (Long id ) {
37
+ this .id = id ;
38
+ }
39
+
40
+ public Long getProvinceId () {
41
+ return provinceId ;
42
+ }
43
+
44
+ public void setProvinceId (Long provinceId ) {
45
+ this .provinceId = provinceId ;
46
+ }
47
+
48
+ public String getCityName () {
49
+ return cityName ;
50
+ }
51
+
52
+ public void setCityName (String cityName ) {
53
+ this .cityName = cityName ;
54
+ }
55
+
56
+ public String getDescription () {
57
+ return description ;
58
+ }
59
+
60
+ public void setDescription (String description ) {
61
+ this .description = description ;
62
+ }
63
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 .web .bind .annotation .*;
7
+ import reactor .core .publisher .Flux ;
8
+ import reactor .core .publisher .Mono ;
9
+
10
+ @ RestController
11
+ @ RequestMapping (value = "/city" )
12
+ public class CityWebFluxController {
13
+
14
+ @ Autowired
15
+ private CityHandler cityHandler ;
16
+
17
+ @ GetMapping (value = "/{id}" )
18
+ public Mono <City > findCityById (@ PathVariable ("id" ) Long id ) {
19
+ return cityHandler .findCityById (id );
20
+ }
21
+
22
+ @ GetMapping ()
23
+ public Flux <City > findAllCity () {
24
+ return cityHandler .findAllCity ();
25
+ }
26
+
27
+ @ PostMapping ()
28
+ public Mono <City > saveCity (@ RequestBody City city ) {
29
+ return cityHandler .save (city );
30
+ }
31
+
32
+ @ PutMapping ()
33
+ public Mono <City > modifyCity (@ RequestBody City city ) {
34
+ return cityHandler .modifyCity (city );
35
+ }
36
+
37
+ @ DeleteMapping (value = "/{id}" )
38
+ public Mono <Long > deleteCity (@ PathVariable ("id" ) Long id ) {
39
+ return cityHandler .deleteCity (id );
40
+ }
41
+ }
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments