From e79d4586f0099200a9af92554827c441462cbb9f Mon Sep 17 00:00:00 2001 From: liqiangqiang Date: Fri, 4 May 2018 21:25:59 +0800 Subject: [PATCH] =?UTF-8?q?WebFlux=20=E5=9B=BE=E4=B9=A6=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- springboot-webflux-10-book-manage-sys/pom.xml | 60 ++++++++++++ .../java/demo/springboot/WebApplication.java | 16 ++++ .../demo/springboot/dao/BookRepository.java | 10 ++ .../java/demo/springboot/domain/Book.java | 68 +++++++++++++ .../demo/springboot/service/BookService.java | 45 +++++++++ .../service/impl/BookServiceImpl.java | 50 ++++++++++ .../demo/springboot/web/BookController.java | 89 ++++++++++++++++++ .../src/main/resources/application.properties | 5 + .../src/main/resources/static/css/default.css | 2 + .../main/resources/static/images/favicon.ico | Bin 0 -> 946 bytes .../main/resources/templates/bookForm.html | 56 +++++++++++ .../main/resources/templates/bookList.html | 44 +++++++++ 12 files changed, 445 insertions(+) create mode 100644 springboot-webflux-10-book-manage-sys/pom.xml create mode 100644 springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/WebApplication.java create mode 100644 springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/dao/BookRepository.java create mode 100644 springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/domain/Book.java create mode 100644 springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/service/BookService.java create mode 100644 springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/service/impl/BookServiceImpl.java create mode 100644 springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/web/BookController.java create mode 100644 springboot-webflux-10-book-manage-sys/src/main/resources/application.properties create mode 100755 springboot-webflux-10-book-manage-sys/src/main/resources/static/css/default.css create mode 100755 springboot-webflux-10-book-manage-sys/src/main/resources/static/images/favicon.ico create mode 100644 springboot-webflux-10-book-manage-sys/src/main/resources/templates/bookForm.html create mode 100644 springboot-webflux-10-book-manage-sys/src/main/resources/templates/bookList.html diff --git a/springboot-webflux-10-book-manage-sys/pom.xml b/springboot-webflux-10-book-manage-sys/pom.xml new file mode 100644 index 0000000..320c7a3 --- /dev/null +++ b/springboot-webflux-10-book-manage-sys/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + WebFlux 实战图书管理系统 + + demo.springboot + springboot-webflux-10-book-manage-sys + 1.0 + jar + + + + org.springframework.boot + spring-boot-starter-parent + 2.0.1.RELEASE + + + + UTF-8 + UTF-8 + 1.8 + + + + + + + org.springframework.boot + spring-boot-starter-webflux + + + + + org.springframework.boot + spring-boot-starter-data-mongodb-reactive + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + junit + junit + 4.12 + + + + diff --git a/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/WebApplication.java b/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/WebApplication.java new file mode 100644 index 0000000..a014376 --- /dev/null +++ b/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/WebApplication.java @@ -0,0 +1,16 @@ +package demo.springboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Spring Boot 应用启动类 + * + * Created by bysocket + */ +@SpringBootApplication +public class WebApplication { + public static void main(String[] args) { + SpringApplication.run(WebApplication.class, args); + } +} diff --git a/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/dao/BookRepository.java b/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/dao/BookRepository.java new file mode 100644 index 0000000..adc52e9 --- /dev/null +++ b/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/dao/BookRepository.java @@ -0,0 +1,10 @@ +package demo.springboot.dao; + +import demo.springboot.domain.Book; +import org.springframework.data.mongodb.repository.ReactiveMongoRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface BookRepository extends ReactiveMongoRepository { + +} diff --git a/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/domain/Book.java b/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/domain/Book.java new file mode 100644 index 0000000..39417c6 --- /dev/null +++ b/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/domain/Book.java @@ -0,0 +1,68 @@ +package demo.springboot.domain; + +import org.springframework.data.annotation.Id; + +import java.io.Serializable; + +/** + * Book 实体类 + * + * Created by bysocket + */ +public class Book implements Serializable { + + private static final long serialVersionUID = 8033624715179323397L; + + /** + * 编号 + */ + @Id + private Long id = 0L; + + /** + * 书名 + */ + private String name; + + /** + * 作者 + */ + private String writer; + + /** + * 简介 + */ + private String introduction; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getWriter() { + return writer; + } + + public void setWriter(String writer) { + this.writer = writer; + } + + public String getIntroduction() { + return introduction; + } + + public void setIntroduction(String introduction) { + this.introduction = introduction; + } +} diff --git a/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/service/BookService.java b/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/service/BookService.java new file mode 100644 index 0000000..00b39ad --- /dev/null +++ b/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/service/BookService.java @@ -0,0 +1,45 @@ +package demo.springboot.service; + +import demo.springboot.domain.Book; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +/** + * Book 业务接口层 + * + * Created by bysocket + */ +public interface BookService { + /** + * 获取所有 Book + */ + Flux findAll(); + + /** + * 新增 Book + * + * @param book {@link Book} + */ + Mono insertByBook(Book book); + + /** + * 更新 Book + * + * @param book {@link Book} + */ + Mono update(Book book); + + /** + * 删除 Book + * + * @param id 编号 + */ + Mono delete(Long id); + + /** + * 获取 Book + * + * @param id 编号 + */ + Mono findById(Long id); +} diff --git a/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/service/impl/BookServiceImpl.java b/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/service/impl/BookServiceImpl.java new file mode 100644 index 0000000..38bdc93 --- /dev/null +++ b/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/service/impl/BookServiceImpl.java @@ -0,0 +1,50 @@ +package demo.springboot.service.impl; + +import demo.springboot.dao.BookRepository; +import demo.springboot.domain.Book; +import demo.springboot.service.BookService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +/** + * Book 业务层实现 + * + * Created by bysocket + */ +@Component +public class BookServiceImpl implements BookService { + + private final BookRepository bookRepository; + + @Autowired + public BookServiceImpl(BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + @Override + public Flux findAll() { + return bookRepository.findAll(); + } + + @Override + public Mono insertByBook(Book book) { + return bookRepository.save(book); + } + + @Override + public Mono update(Book book) { + return bookRepository.save(book); + } + + @Override + public Mono delete(Long id) { + return bookRepository.deleteById(id); + } + + @Override + public Mono findById(Long id) { + return bookRepository.findById(id); + } +} diff --git a/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/web/BookController.java b/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/web/BookController.java new file mode 100644 index 0000000..7590974 --- /dev/null +++ b/springboot-webflux-10-book-manage-sys/src/main/java/demo/springboot/web/BookController.java @@ -0,0 +1,89 @@ +package demo.springboot.web; + +import demo.springboot.domain.Book; +import demo.springboot.service.BookService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +/** + * Book 控制层 + *

+ * Created by bysocket + */ +@Controller +@RequestMapping(value = "/book") +public class BookController { + + private static final String BOOK_FORM_PATH_NAME = "bookForm"; + private static final String BOOK_LIST_PATH_NAME = "bookList"; + private static final String REDIRECT_TO_BOOK_URL = "redirect:/book"; + + @Autowired + BookService bookService; + + /** + * 获取 Book 列表 + * 处理 "/book" 的 GET 请求,用来获取 Book 列表 + */ + @RequestMapping(method = RequestMethod.GET) + public String getBookList(final Model model) { + model.addAttribute("bookList", bookService.findAll()); + return BOOK_LIST_PATH_NAME; + } + + /** + * 获取创建 Book 表单 + */ + @RequestMapping(value = "/create", method = RequestMethod.GET) + public String createBookForm(final Model model) { + model.addAttribute("book", new Book()); + model.addAttribute("action", "create"); + return BOOK_FORM_PATH_NAME; + } + + /** + * 创建 Book + * 处理 "/book/create" 的 POST 请求,用来新建 Book 信息 + * 通过 @ModelAttribute 绑定表单实体参数,也通过 @RequestParam 传递参数 + */ + @RequestMapping(value = "/create", method = RequestMethod.POST) + public String postBook(@ModelAttribute Book book) { + bookService.insertByBook(book); + return REDIRECT_TO_BOOK_URL; + } + + /** + * 获取更新 Book 表单 + * 处理 "/book/update/{id}" 的 GET 请求,通过 URL 中的 id 值获取 Book 信息 + * URL 中的 id ,通过 @PathVariable 绑定参数 + */ + @RequestMapping(value = "/update/{id}", method = RequestMethod.GET) + public String getUser(@PathVariable Long id, final Model model) { + model.addAttribute("book", bookService.findById(id)); + model.addAttribute("action", "update"); + return BOOK_FORM_PATH_NAME; + } + + /** + * 更新 Book + * 处理 "/update" 的 PUT 请求,用来更新 Book 信息 + */ + @RequestMapping(value = "/update", method = RequestMethod.POST) + public String putBook(@ModelAttribute Book book) { + bookService.update(book); + return REDIRECT_TO_BOOK_URL; + } + + /** + * 删除 Book + * 处理 "/book/{id}" 的 GET 请求,用来删除 Book 信息 + */ + @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET) + public String deleteBook(@PathVariable Long id) { + bookService.delete(id); + return REDIRECT_TO_BOOK_URL; + } + +} diff --git a/springboot-webflux-10-book-manage-sys/src/main/resources/application.properties b/springboot-webflux-10-book-manage-sys/src/main/resources/application.properties new file mode 100644 index 0000000..270284a --- /dev/null +++ b/springboot-webflux-10-book-manage-sys/src/main/resources/application.properties @@ -0,0 +1,5 @@ +spring.data.mongodb.host=localhost +spring.data.mongodb.database=admin +spring.data.mongodb.port=27017 +spring.data.mongodb.username=admin +spring.data.mongodb.password=admin diff --git a/springboot-webflux-10-book-manage-sys/src/main/resources/static/css/default.css b/springboot-webflux-10-book-manage-sys/src/main/resources/static/css/default.css new file mode 100755 index 0000000..4da3051 --- /dev/null +++ b/springboot-webflux-10-book-manage-sys/src/main/resources/static/css/default.css @@ -0,0 +1,2 @@ +/* contentDiv */ +.contentDiv {padding:20px 60px;} \ No newline at end of file diff --git a/springboot-webflux-10-book-manage-sys/src/main/resources/static/images/favicon.ico b/springboot-webflux-10-book-manage-sys/src/main/resources/static/images/favicon.ico new file mode 100755 index 0000000000000000000000000000000000000000..e5a293420da31e952b5d47660a089cee51c008e8 GIT binary patch literal 946 zcma)3O=}ZT6n*u3BXlKFT-eFXWSS<8PDNu2+6JlJbyvlW=%O24)K;WLDk+!>BGfNj zgc2)8v^I(qD_ALQGc(B~cIM5DsGBZaC?fuWVs)7(4(}VkZCQsdp&)(?oE-m!Mt)=swRKI%I$=_{v zj7-?pGpduH?)1#j-Vdb+YHX|2Ig+}dM#tkDtz#V!O7?OY-A2u_6r{D{Y34&Y1+T`cO#b+STOhD+~Sx}}1LDXM^Kn<=T zqK_`G*n>K2OfP_Q4yKPahppp34dl&1@c9HN=^+rcxlJGKPCkqL(trH24$Bo@ZQ*iT@hP%nRl+k&eev`{otLX z|FGZ<7j`?wmt$k*F=ekpsE+Zj z4qo+5!kW6ZwiyC literal 0 HcmV?d00001 diff --git a/springboot-webflux-10-book-manage-sys/src/main/resources/templates/bookForm.html b/springboot-webflux-10-book-manage-sys/src/main/resources/templates/bookForm.html new file mode 100644 index 0000000..9a893eb --- /dev/null +++ b/springboot-webflux-10-book-manage-sys/src/main/resources/templates/bookForm.html @@ -0,0 +1,56 @@ + + + + + + + + + 书籍管理 + + + +

+ + + 书籍管理 + + +
+ + + +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+    + +
+
+
+
+ + diff --git a/springboot-webflux-10-book-manage-sys/src/main/resources/templates/bookList.html b/springboot-webflux-10-book-manage-sys/src/main/resources/templates/bookList.html new file mode 100644 index 0000000..e78de20 --- /dev/null +++ b/springboot-webflux-10-book-manage-sys/src/main/resources/templates/bookList.html @@ -0,0 +1,44 @@ + + + + + + + + + 书籍列表 + + + + +
+ + + + 书籍列表 + + + + + + + + + + + + + + + + + + + +
书籍编号书名作者简介管理
删除
+ + +
+ + +