Skip to content

Commit e79d458

Browse files
author
liqiangqiang
committed
WebFlux 图书管理系统
1 parent 5f0ebef commit e79d458

File tree

12 files changed

+445
-0
lines changed

12 files changed

+445
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
<description>WebFlux 实战图书管理系统</description>
6+
7+
<groupId>demo.springboot</groupId>
8+
<artifactId>springboot-webflux-10-book-manage-sys</artifactId>
9+
<version>1.0</version>
10+
<packaging>jar</packaging>
11+
12+
<!-- Spring Boot 启动父依赖 -->
13+
<parent>
14+
<groupId>org.springframework.boot</groupId>
15+
<artifactId>spring-boot-starter-parent</artifactId>
16+
<version>2.0.1.RELEASE</version>
17+
</parent>
18+
19+
<properties>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
22+
<java.version>1.8</java.version>
23+
</properties>
24+
25+
<dependencies>
26+
27+
<!-- Spring Boot Web Flux 依赖 -->
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-webflux</artifactId>
31+
</dependency>
32+
33+
<!-- Spring Boot 响应式 MongoDB 依赖 -->
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
37+
</dependency>
38+
39+
<!-- 模板引擎 Thymeleaf 依赖 -->
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
43+
</dependency>
44+
45+
<!-- Spring Boot Test 依赖 -->
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-test</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
52+
<!-- Junit -->
53+
<dependency>
54+
<groupId>junit</groupId>
55+
<artifactId>junit</artifactId>
56+
<version>4.12</version>
57+
</dependency>
58+
</dependencies>
59+
60+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package demo.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
10+
*/
11+
@SpringBootApplication
12+
public class WebApplication {
13+
public static void main(String[] args) {
14+
SpringApplication.run(WebApplication.class, args);
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package demo.springboot.dao;
2+
3+
import demo.springboot.domain.Book;
4+
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface BookRepository extends ReactiveMongoRepository<Book, Long> {
9+
10+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package demo.springboot.domain;
2+
3+
import org.springframework.data.annotation.Id;
4+
5+
import java.io.Serializable;
6+
7+
/**
8+
* Book 实体类
9+
*
10+
* Created by bysocket
11+
*/
12+
public class Book implements Serializable {
13+
14+
private static final long serialVersionUID = 8033624715179323397L;
15+
16+
/**
17+
* 编号
18+
*/
19+
@Id
20+
private Long id = 0L;
21+
22+
/**
23+
* 书名
24+
*/
25+
private String name;
26+
27+
/**
28+
* 作者
29+
*/
30+
private String writer;
31+
32+
/**
33+
* 简介
34+
*/
35+
private String introduction;
36+
37+
public Long getId() {
38+
return id;
39+
}
40+
41+
public void setId(Long id) {
42+
this.id = id;
43+
}
44+
45+
public String getName() {
46+
return name;
47+
}
48+
49+
public void setName(String name) {
50+
this.name = name;
51+
}
52+
53+
public String getWriter() {
54+
return writer;
55+
}
56+
57+
public void setWriter(String writer) {
58+
this.writer = writer;
59+
}
60+
61+
public String getIntroduction() {
62+
return introduction;
63+
}
64+
65+
public void setIntroduction(String introduction) {
66+
this.introduction = introduction;
67+
}
68+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package demo.springboot.service;
2+
3+
import demo.springboot.domain.Book;
4+
import reactor.core.publisher.Flux;
5+
import reactor.core.publisher.Mono;
6+
7+
/**
8+
* Book 业务接口层
9+
*
10+
* Created by bysocket
11+
*/
12+
public interface BookService {
13+
/**
14+
* 获取所有 Book
15+
*/
16+
Flux<Book> findAll();
17+
18+
/**
19+
* 新增 Book
20+
*
21+
* @param book {@link Book}
22+
*/
23+
Mono<Book> insertByBook(Book book);
24+
25+
/**
26+
* 更新 Book
27+
*
28+
* @param book {@link Book}
29+
*/
30+
Mono<Book> update(Book book);
31+
32+
/**
33+
* 删除 Book
34+
*
35+
* @param id 编号
36+
*/
37+
Mono<Void> delete(Long id);
38+
39+
/**
40+
* 获取 Book
41+
*
42+
* @param id 编号
43+
*/
44+
Mono<Book> findById(Long id);
45+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package demo.springboot.service.impl;
2+
3+
import demo.springboot.dao.BookRepository;
4+
import demo.springboot.domain.Book;
5+
import demo.springboot.service.BookService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Component;
8+
import reactor.core.publisher.Flux;
9+
import reactor.core.publisher.Mono;
10+
11+
/**
12+
* Book 业务层实现
13+
*
14+
* Created by bysocket
15+
*/
16+
@Component
17+
public class BookServiceImpl implements BookService {
18+
19+
private final BookRepository bookRepository;
20+
21+
@Autowired
22+
public BookServiceImpl(BookRepository bookRepository) {
23+
this.bookRepository = bookRepository;
24+
}
25+
26+
@Override
27+
public Flux<Book> findAll() {
28+
return bookRepository.findAll();
29+
}
30+
31+
@Override
32+
public Mono<Book> insertByBook(Book book) {
33+
return bookRepository.save(book);
34+
}
35+
36+
@Override
37+
public Mono<Book> update(Book book) {
38+
return bookRepository.save(book);
39+
}
40+
41+
@Override
42+
public Mono<Void> delete(Long id) {
43+
return bookRepository.deleteById(id);
44+
}
45+
46+
@Override
47+
public Mono<Book> findById(Long id) {
48+
return bookRepository.findById(id);
49+
}
50+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package demo.springboot.web;
2+
3+
import demo.springboot.domain.Book;
4+
import demo.springboot.service.BookService;
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+
10+
/**
11+
* Book 控制层
12+
* <p>
13+
* Created by bysocket
14+
*/
15+
@Controller
16+
@RequestMapping(value = "/book")
17+
public class BookController {
18+
19+
private static final String BOOK_FORM_PATH_NAME = "bookForm";
20+
private static final String BOOK_LIST_PATH_NAME = "bookList";
21+
private static final String REDIRECT_TO_BOOK_URL = "redirect:/book";
22+
23+
@Autowired
24+
BookService bookService;
25+
26+
/**
27+
* 获取 Book 列表
28+
* 处理 "/book" 的 GET 请求,用来获取 Book 列表
29+
*/
30+
@RequestMapping(method = RequestMethod.GET)
31+
public String getBookList(final Model model) {
32+
model.addAttribute("bookList", bookService.findAll());
33+
return BOOK_LIST_PATH_NAME;
34+
}
35+
36+
/**
37+
* 获取创建 Book 表单
38+
*/
39+
@RequestMapping(value = "/create", method = RequestMethod.GET)
40+
public String createBookForm(final Model model) {
41+
model.addAttribute("book", new Book());
42+
model.addAttribute("action", "create");
43+
return BOOK_FORM_PATH_NAME;
44+
}
45+
46+
/**
47+
* 创建 Book
48+
* 处理 "/book/create" 的 POST 请求,用来新建 Book 信息
49+
* 通过 @ModelAttribute 绑定表单实体参数,也通过 @RequestParam 传递参数
50+
*/
51+
@RequestMapping(value = "/create", method = RequestMethod.POST)
52+
public String postBook(@ModelAttribute Book book) {
53+
bookService.insertByBook(book);
54+
return REDIRECT_TO_BOOK_URL;
55+
}
56+
57+
/**
58+
* 获取更新 Book 表单
59+
* 处理 "/book/update/{id}" 的 GET 请求,通过 URL 中的 id 值获取 Book 信息
60+
* URL 中的 id ,通过 @PathVariable 绑定参数
61+
*/
62+
@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
63+
public String getUser(@PathVariable Long id, final Model model) {
64+
model.addAttribute("book", bookService.findById(id));
65+
model.addAttribute("action", "update");
66+
return BOOK_FORM_PATH_NAME;
67+
}
68+
69+
/**
70+
* 更新 Book
71+
* 处理 "/update" 的 PUT 请求,用来更新 Book 信息
72+
*/
73+
@RequestMapping(value = "/update", method = RequestMethod.POST)
74+
public String putBook(@ModelAttribute Book book) {
75+
bookService.update(book);
76+
return REDIRECT_TO_BOOK_URL;
77+
}
78+
79+
/**
80+
* 删除 Book
81+
* 处理 "/book/{id}" 的 GET 请求,用来删除 Book 信息
82+
*/
83+
@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
84+
public String deleteBook(@PathVariable Long id) {
85+
bookService.delete(id);
86+
return REDIRECT_TO_BOOK_URL;
87+
}
88+
89+
}
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* contentDiv */
2+
.contentDiv {padding:20px 60px;}
Binary file not shown.

0 commit comments

Comments
 (0)