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:
@@ -1,10 +1,10 @@
|
||||
package demo.springboot.dao;
|
||||
|
||||
import demo.springboot.domain.Book;
|
||||
import demo.springboot.domain.City;
|
||||
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface BookRepository extends ReactiveMongoRepository<Book, Long> {
|
||||
public interface CityRepository extends ReactiveMongoRepository<City, Long> {
|
||||
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
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,60 @@
|
||||
package demo.springboot.domain;
|
||||
|
||||
/**
|
||||
* 城市实体类
|
||||
*
|
||||
*/
|
||||
public class City {
|
||||
|
||||
/**
|
||||
* 城市编号
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 省份编号
|
||||
*/
|
||||
private Long provinceId;
|
||||
|
||||
/**
|
||||
* 城市名称
|
||||
*/
|
||||
private String cityName;
|
||||
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
private String description;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getProvinceId() {
|
||||
return provinceId;
|
||||
}
|
||||
|
||||
public void setProvinceId(Long provinceId) {
|
||||
this.provinceId = provinceId;
|
||||
}
|
||||
|
||||
public String getCityName() {
|
||||
return cityName;
|
||||
}
|
||||
|
||||
public void setCityName(String cityName) {
|
||||
this.cityName = cityName;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
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,18 @@
|
||||
package demo.springboot.service;
|
||||
|
||||
import demo.springboot.domain.City;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
public interface CityService {
|
||||
|
||||
Flux<City> findAll();
|
||||
|
||||
Mono<City> insertByCity(City city);
|
||||
|
||||
Mono<City> update(City city);
|
||||
|
||||
Mono<Void> delete(Long id);
|
||||
|
||||
Mono<City> findById(Long id);
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
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,45 @@
|
||||
package demo.springboot.service.impl;
|
||||
|
||||
import demo.springboot.dao.CityRepository;
|
||||
import demo.springboot.domain.City;
|
||||
import demo.springboot.service.CityService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class CityServiceImpl implements CityService {
|
||||
|
||||
private final CityRepository cityRepository;
|
||||
|
||||
@Autowired
|
||||
public CityServiceImpl(CityRepository cityRepository) {
|
||||
this.cityRepository = cityRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<City> findAll() {
|
||||
return cityRepository.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<City> insertByCity(City city) {
|
||||
return cityRepository.save(city);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<City> update(City city) {
|
||||
return cityRepository.save(city);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> delete(Long id) {
|
||||
return cityRepository.deleteById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<City> findById(Long id) {
|
||||
return cityRepository.findById(id);
|
||||
}
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
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,71 @@
|
||||
package demo.springboot.web;
|
||||
|
||||
import demo.springboot.domain.City;
|
||||
import demo.springboot.service.CityService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.awt.print.Book;
|
||||
|
||||
/**
|
||||
* city 控制层
|
||||
* <p>
|
||||
* Created by bysocket
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping(value = "/city")
|
||||
public class CityController {
|
||||
|
||||
private static final String CITY_FORM_PATH_NAME = "cityForm";
|
||||
private static final String CITY_LIST_PATH_NAME = "cityList";
|
||||
private static final String REDIRECT_TO_CITY_URL = "redirect:/city";
|
||||
|
||||
@Autowired
|
||||
CityService cityService;
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
public String getCityList(final Model model) {
|
||||
model.addAttribute("cityList", cityService.findAll());
|
||||
return CITY_LIST_PATH_NAME;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/create", method = RequestMethod.GET)
|
||||
public String createCityForm(final Model model) {
|
||||
model.addAttribute("city", new City());
|
||||
model.addAttribute("action", "create");
|
||||
return CITY_FORM_PATH_NAME;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/create", method = RequestMethod.POST)
|
||||
public String postCity(@ModelAttribute City city) {
|
||||
cityService.insertByCity(city);
|
||||
return REDIRECT_TO_CITY_URL;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
|
||||
public String getCity(@PathVariable Long id, final Model model) {
|
||||
final Mono<City> city = cityService.findById(id);
|
||||
model.addAttribute("city", city);
|
||||
model.addAttribute("action", "update");
|
||||
return CITY_FORM_PATH_NAME;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/update", method = RequestMethod.POST)
|
||||
public String putBook(@ModelAttribute City city) {
|
||||
cityService.update(city);
|
||||
return REDIRECT_TO_CITY_URL;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
|
||||
public String deleteCity(@PathVariable Long id) {
|
||||
cityService.delete(id);
|
||||
return CITY_LIST_PATH_NAME;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
<!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,61 @@
|
||||
<!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="@{/city/{action}(action=${action})}" method="post" class="form-horizontal">
|
||||
|
||||
<div class="form-group">
|
||||
<label for="city_id" class="col-sm-2 control-label">城市编号:</label>
|
||||
<div class="col-xs-4">
|
||||
<input type="text" class="form-control" id="city_id" name="id" th:value="${city.id}"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="city_name" class="col-sm-2 control-label">城市名称:</label>
|
||||
<div class="col-xs-4">
|
||||
<input type="text" class="form-control" id="city_name" name="cityName" th:value="${city.cityName}"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="city_description" class="col-sm-2 control-label">城市描述:</label>
|
||||
<div class="col-xs-4">
|
||||
<input class="form-control" id="city_description" rows="3" name="description"
|
||||
th:value="${city.description}" />
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="city_provinceId" class="col-sm-2 control-label">省份编号:</label>
|
||||
<div class="col-xs-4">
|
||||
<input type="text" class="form-control" id="city_provinceId" name="provinceId" th:value="${city.provinceId}"
|
||||
/>
|
||||
</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>
|
||||
@@ -6,7 +6,7 @@
|
||||
<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>
|
||||
<title>城市列表</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
@@ -15,29 +15,30 @@
|
||||
|
||||
<table class="table table-hover table-condensed">
|
||||
<legend>
|
||||
<strong>书籍列表</strong>
|
||||
<strong>城市列表</strong>
|
||||
</legend>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>书籍编号</th>
|
||||
<th>书名</th>
|
||||
<th>作者</th>
|
||||
<th>简介</th>
|
||||
<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 th:each="city : ${cityList}">
|
||||
<th scope="row" th:text="${city.id}"></th>
|
||||
<td><a th:href="@{/city/update/{cityId}(cityId=${city.id})}" th:text="${city.cityName}"></a></td>
|
||||
<td th:text="${city.description}"></td>
|
||||
<td th:text="${city.provinceId}"></td>
|
||||
<td><a class="btn btn-danger" th:href="@{/city/delete/{cityId}(cityId=${city.id})}">删除</a></td>
|
||||
</tr>
|
||||
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<div><a class="btn btn-primary" href="/book/create" role="button">新增书籍</a></div>
|
||||
<div><a class="btn btn-primary" href="/city/create" role="button">新增城市</a></div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
Reference in New Issue
Block a user