mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +08:00
WebFlux 图书管理系统
This commit is contained in:
60
springboot-webflux-10-book-manage-sys/pom.xml
Normal file
60
springboot-webflux-10-book-manage-sys/pom.xml
Normal file
@@ -0,0 +1,60 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<description>WebFlux 实战图书管理系统</description>
|
||||
|
||||
<groupId>demo.springboot</groupId>
|
||||
<artifactId>springboot-webflux-10-book-manage-sys</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<!-- Spring Boot 启动父依赖 -->
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.1.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Spring Boot Web Flux 依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot 响应式 MongoDB 依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 模板引擎 Thymeleaf 依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot Test 依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Junit -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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<Book, Long> {
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<Book> findAll();
|
||||
|
||||
/**
|
||||
* 新增 Book
|
||||
*
|
||||
* @param book {@link Book}
|
||||
*/
|
||||
Mono<Book> insertByBook(Book book);
|
||||
|
||||
/**
|
||||
* 更新 Book
|
||||
*
|
||||
* @param book {@link Book}
|
||||
*/
|
||||
Mono<Book> update(Book book);
|
||||
|
||||
/**
|
||||
* 删除 Book
|
||||
*
|
||||
* @param id 编号
|
||||
*/
|
||||
Mono<Void> delete(Long id);
|
||||
|
||||
/**
|
||||
* 获取 Book
|
||||
*
|
||||
* @param id 编号
|
||||
*/
|
||||
Mono<Book> findById(Long id);
|
||||
}
|
||||
@@ -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<Book> findAll() {
|
||||
return bookRepository.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Book> insertByBook(Book book) {
|
||||
return bookRepository.save(book);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Book> update(Book book) {
|
||||
return bookRepository.save(book);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> delete(Long id) {
|
||||
return bookRepository.deleteById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Book> findById(Long id) {
|
||||
return bookRepository.findById(id);
|
||||
}
|
||||
}
|
||||
@@ -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 控制层
|
||||
* <p>
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
@@ -0,0 +1,2 @@
|
||||
/* contentDiv */
|
||||
.contentDiv {padding:20px 60px;}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 946 B |
@@ -0,0 +1,56 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<script type="text/javascript" th:src="@{https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js}"></script>
|
||||
<link th:href="@{https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css}" rel="stylesheet"/>
|
||||
<link th:href="@{/css/default.css}" rel="stylesheet"/>
|
||||
<link rel="icon" th:href="@{/images/favicon.ico}" type="image/x-icon"/>
|
||||
<meta charset="UTF-8"/>
|
||||
<title>书籍管理</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="contentDiv">
|
||||
|
||||
<legend>
|
||||
<strong>书籍管理</strong>
|
||||
</legend>
|
||||
|
||||
<form th:action="@{/book/{action}(action=${action})}" method="post" class="form-horizontal">
|
||||
|
||||
<input type="hidden" name="id" th:value="1"/>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="book_name" class="col-sm-2 control-label">书名:</label>
|
||||
<div class="col-xs-4">
|
||||
<input type="text" class="form-control" id="book_name" name="name" th:value="${book.name}"
|
||||
th:field="*{book.name}"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="book_writer" class="col-sm-2 control-label">作者:</label>
|
||||
<div class="col-xs-4">
|
||||
<input type="text" class="form-control" id="book_writer" name="writer" th:value="${book.writer}"
|
||||
th:field="*{book.writer}"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="book_introduction" class="col-sm-2 control-label">简介:</label>
|
||||
<div class="col-xs-4">
|
||||
<textarea class="form-control" id="book_introduction" rows="3" name="introduction"
|
||||
th:value="${book.introduction}" th:field="*{book.introduction}"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<div class="col-sm-offset-2 col-sm-10">
|
||||
<input class="btn btn-primary" type="submit" value="提交"/>
|
||||
<input class="btn" type="button" value="返回" onclick="history.back()"/>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,44 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<script type="text/javascript" th:src="@{https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js}"></script>
|
||||
<link th:href="@{https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css}" rel="stylesheet"/>
|
||||
<link th:href="@{/css/default.css}" rel="stylesheet"/>
|
||||
<link rel="icon" th:href="@{/images/favicon.ico}" type="image/x-icon"/>
|
||||
<meta charset="UTF-8"/>
|
||||
<title>书籍列表</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div class="contentDiv">
|
||||
|
||||
<table class="table table-hover table-condensed">
|
||||
<legend>
|
||||
<strong>书籍列表</strong>
|
||||
</legend>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>书籍编号</th>
|
||||
<th>书名</th>
|
||||
<th>作者</th>
|
||||
<th>简介</th>
|
||||
<th>管理</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="book : ${bookList}">
|
||||
<th scope="row" th:text="${book.id}"></th>
|
||||
<td><a th:href="@{/book/update/{bookId}(bookId=${book.id})}" th:text="${book.name}"></a></td>
|
||||
<td th:text="${book.writer}"></td>
|
||||
<td th:text="${book.introduction}"></td>
|
||||
<td><a class="btn btn-danger" th:href="@{/book/delete/{bookId}(bookId=${book.id})}">删除</a></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<div><a class="btn btn-primary" href="/book/create" role="button">新增书籍</a></div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user