Spring Boot 入门、Spring Boot 配置、Web 开发、模板引擎、数据存储、数据缓存 案例更新

This commit is contained in:
liqiangqiang
2018-04-08 14:46:30 +08:00
parent c862ecd73b
commit fc5200546c
70 changed files with 2201 additions and 293 deletions

View File

@@ -33,6 +33,14 @@ Web Flux 努力进行中,尽情期待。唯一文章入口:[GitChat文章地
## 一、项目结构
「Spring Boot 那些事」:[传送门](http://www.bysocket.com/?page_id=1639 "Spring Boot 那些事")<br>
### 『 WebFlux 篇 』
WebFlux 系类文章入口:[GitChat文章地址](http://gitbook.cn/gitchat/author/58968d35f2b669527d7a7c57 "gitchat")
- springboot-webflux <br>
Spring Boot WebFlux 实现 Restful 服务
#### a. 『 基础 - 入门篇 』
- springboot-helloworld<br>
[《Spring Boot 之 HelloWorld 详解》](http://www.bysocket.com/?p=1124 "Spring Boot 之 HelloWorld详解")<br>

View File

@@ -0,0 +1,64 @@
<?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>
<name>chapter-1-spring-boot-quickstart</name>
<description>《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 1 章《Spring Boot 入门》Demo</description>
<groupId>demo.springboot</groupId>
<artifactId>chapter-1-spring-boot-quickstart</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.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>
<!-- Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Spring Boot Maven 插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>

View File

@@ -0,0 +1,16 @@
package demo.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Spring Boot 应用启动类
*
* Created by bysocket on 26/09/2017.
*/
@SpringBootApplication
public class QuickStartApplication {
public static void main(String[] args) {
SpringApplication.run(QuickStartApplication.class, args);
}
}

View File

@@ -0,0 +1,19 @@
package demo.springboot.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* Spring Boot Hello案例
*
* Created by bysocket on 26/09/2017.
*/
@RestController
public class HelloBookController {
@RequestMapping(value = "/book/hello",method = RequestMethod.GET)
public String sayHello() {
return "Hello《Spring Boot 2.x 核心技术实战 - 上 基础篇》!";
}
}

View File

@@ -0,0 +1,21 @@
package demo.springboot.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Spring Boot Hello案例
*
* Created by bysocket on 26/09/2017.
*/
@Controller
public class HelloController {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
@ResponseBody
public String sayHello() {
return "HelloSpring Boot";
}
}

View File

@@ -0,0 +1,16 @@
package demo.springboot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class QuickStartApplicationTests {
@Test
public void contextLoads() {
}
}

View File

@@ -0,0 +1,73 @@
<?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>
<name>chapter-2-spring-boot-config</name>
<description>《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 2 章《Spring Boot 配置》Demo</description>
<groupId>demo.springboot</groupId>
<artifactId>chapter-2-spring-boot-config</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</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>
<!-- Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 自定义 swagger2 Starter 组件依赖 -->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>spring-boot-starter-swagger</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Spring Boot Maven 插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.1.RELEASE</version>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>

View File

@@ -0,0 +1,18 @@
package demo.springboot;
import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Spring Boot 应用启动类
*
* Created by bysocket on 26/09/2017.
*/
@EnableSwagger2Doc // 开启 Swagger
@SpringBootApplication
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}

View File

@@ -0,0 +1,46 @@
package demo.springboot.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* 书属性
*
*/
@Component
@ConfigurationProperties(prefix = "demo.book")
@Validated
public class BookComponent {
/**
* 书名
*/
@NotEmpty
private String name;
/**
* 作者
*/
@NotNull
private String writer;
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;
}
}

View File

@@ -0,0 +1,41 @@
package demo.springboot.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 书属性
*
* Created by bysocket on 27/09/2017.
*/
@Component
public class BookProperties {
/**
* 书名
*/
@Value("${demo.book.name}")
private String name;
/**
* 作者
*/
@Value("${demo.book.writer}")
private String writer;
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;
}
}

View File

@@ -0,0 +1,24 @@
package demo.springboot.web;
import demo.springboot.config.BookProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Spring Boot Hello案例
*
* Created by bysocket on 26/09/2017.
*/
@RestController
public class HelloBookController {
@Autowired
BookProperties bookProperties;
@GetMapping("/book/hello")
public String sayHello() {
return "Hello " + bookProperties.getWriter() + " is writing "
+ bookProperties.getName() + " ";
}
}

View File

@@ -0,0 +1,3 @@
## \u4E66\u4FE1\u606F
demo.book.name=[Spring Boot 2.x Core Action] From Dev
demo.book.writer=BYSocket

View File

@@ -0,0 +1,3 @@
## \u4E66\u4FE1\u606F
demo.book.name=[Spring Boot 2.x Core Action] From Prod
demo.book.writer=BYSocket

View File

@@ -0,0 +1,5 @@
## \u4E66\u4FE1\u606F
demo.book.name=[Spring Boot 2.x Core Action]
demo.book.writer=BYSocket
demo.book.description=${demo.book.writer}'s${demo.book.name}

View File

@@ -0,0 +1,5 @@
## 书信息
demo:
book:
name: 《Spring Boot 2.x 核心技术实战 - 上 基础篇》
writer: 泥瓦匠BYSocket

View File

@@ -0,0 +1,33 @@
package demo.springboot;
import demo.springboot.config.BookComponent;
import demo.springboot.config.BookProperties;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ConfigApplicationTests {
@Autowired
BookProperties bookProperties;
@Autowired
BookComponent bookComponent;
@Test
public void testBookProperties() {
Assert.assertEquals(bookProperties.getName(),"[Spring Boot 2.x Core Action]");
Assert.assertEquals(bookProperties.getWriter(),"BYSocket");
}
@Test
public void testBookComponent() {
Assert.assertEquals(bookComponent.getName(),"[Spring Boot 2.x Core Action]");
Assert.assertEquals(bookComponent.getWriter(),"BYSocket");
}
}

View File

@@ -0,0 +1,65 @@
<?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>
<name>chapter-3-spring-boot-web</name>
<description>《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 3 章《Web 开发》Demo</description>
<groupId>demo.springboot</groupId>
<artifactId>chapter-3-spring-boot-web</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</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>
<!-- Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Spring Boot Maven 插件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
-->
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>

View File

@@ -0,0 +1,16 @@
package demo.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Spring Boot 应用启动类
*
* Created by bysocket on 26/09/2017.
*/
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}

View File

@@ -0,0 +1,63 @@
package demo.springboot.domain;
import java.io.Serializable;
/**
* Book 实体类
*
* Created by bysocket on 27/09/2017.
*/
public class Book implements Serializable {
/**
* 编号
*/
private Long id;
/**
* 书名
*/
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;
}
}

View File

@@ -0,0 +1,45 @@
package demo.springboot.service;
import demo.springboot.domain.Book;
import java.util.List;
/**
* Book 业务接口层
*
* Created by bysocket on 27/09/2017.
*/
public interface BookService {
/**
* 获取所有 Book
*/
List<Book> findAll();
/**
* 新增 Book
*
* @param book {@link Book}
*/
Book insertByBook(Book book);
/**
* 更新 Book
*
* @param book {@link Book}
*/
Book update(Book book);
/**
* 删除 Book
*
* @param id 编号
*/
Book delete(Long id);
/**
* 获取 Book
*
* @param id 编号
*/
Book findById(Long id);
}

View File

@@ -0,0 +1,51 @@
package demo.springboot.service.impl;
import demo.springboot.domain.Book;
import demo.springboot.service.BookService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Book 业务层实现
*
* Created by bysocket on 27/09/2017.
*/
@Service
public class BookServiceImpl implements BookService {
// 模拟数据库,存储 Book 信息
// 第五章《数据存储》会替换成 MySQL 存储
private static Map<Long, Book> BOOK_DB = new HashMap<>();
@Override
public List<Book> findAll() {
return new ArrayList<>(BOOK_DB.values());
}
@Override
public Book insertByBook(Book book) {
book.setId(BOOK_DB.size() + 1L);
BOOK_DB.put(book.getId(), book);
return book;
}
@Override
public Book update(Book book) {
BOOK_DB.put(book.getId(), book);
return book;
}
@Override
public Book delete(Long id) {
return BOOK_DB.remove(id);
}
@Override
public Book findById(Long id) {
return BOOK_DB.get(id);
}
}

View File

@@ -0,0 +1,68 @@
package demo.springboot.web;
import demo.springboot.domain.Book;
import demo.springboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* Book 控制层
*
* Created by bysocket on 27/09/2017.
*/
@RestController
@RequestMapping(value = "/book")
public class BookController {
@Autowired
BookService bookService;
/**
* 获取 Book 列表
* 处理 "/book" 的 GET 请求,用来获取 Book 列表
*/
@RequestMapping(method = RequestMethod.GET)
public List<Book> getBookList() {
return bookService.findAll();
}
/**
* 获取 Book
* 处理 "/book/{id}" 的 GET 请求,用来获取 Book 信息
*/
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Book getBook(@PathVariable Long id) {
return bookService.findById(id);
}
/**
* 创建 Book
* 处理 "/book/create" 的 POST 请求,用来新建 Book 信息
* 通过 @RequestBody 绑定实体参数,也通过 @RequestParam 传递参数
*/
@RequestMapping(value = "/create", method = RequestMethod.POST)
public Book postBook(@RequestBody Book book) {
return bookService.insertByBook(book);
}
/**
* 更新 Book
* 处理 "/update" 的 PUT 请求,用来更新 Book 信息
*/
@RequestMapping(value = "/update", method = RequestMethod.PUT)
public Book putBook(@RequestBody Book book) {
return bookService.update(book);
}
/**
* 删除 Book
* 处理 "/book/{id}" 的 GET 请求,用来删除 Book 信息
*/
@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
public Book deleteBook(@PathVariable Long id) {
return bookService.delete(id);
}
}

View File

@@ -0,0 +1,16 @@
package demo.springboot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class WebApplicationTests {
@Test
public void contextLoads() {
}
}

View File

@@ -0,0 +1,71 @@
<?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>
<name>chapter-4-spring-boot-web-thymeleaf</name>
<description>《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 4 章《模板引擎》Demo</description>
<groupId>demo.springboot</groupId>
<artifactId>chapter-4-spring-boot-web-thymeleaf</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</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>
<!-- Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 模板引擎 Thymeleaf 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Spring Boot Maven 插件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
-->
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>

View File

@@ -0,0 +1,16 @@
package demo.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Spring Boot 应用启动类
*
* Created by bysocket on 30/09/2017.
*/
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}

View File

@@ -0,0 +1,63 @@
package demo.springboot.domain;
import java.io.Serializable;
/**
* Book 实体类
*
* Created by bysocket on 30/09/2017.
*/
public class Book implements Serializable {
/**
* 编号
*/
private Long id;
/**
* 书名
*/
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;
}
}

View File

@@ -0,0 +1,45 @@
package demo.springboot.service;
import demo.springboot.domain.Book;
import java.util.List;
/**
* Book 业务接口层
*
* Created by bysocket on 30/09/2017.
*/
public interface BookService {
/**
* 获取所有 Book
*/
List<Book> findAll();
/**
* 新增 Book
*
* @param book {@link Book}
*/
Book insertByBook(Book book);
/**
* 更新 Book
*
* @param book {@link Book}
*/
Book update(Book book);
/**
* 删除 Book
*
* @param id 编号
*/
Book delete(Long id);
/**
* 获取 Book
*
* @param id 编号
*/
Book findById(Long id);
}

View File

@@ -0,0 +1,51 @@
package demo.springboot.service.impl;
import demo.springboot.domain.Book;
import demo.springboot.service.BookService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Book 业务层实现
*
* Created by bysocket on 30/09/2017.
*/
@Service
public class BookServiceImpl implements BookService {
// 模拟数据库,存储 Book 信息
// 第五章《数据存储》会替换成 H2 数据源存储
private static Map<Long, Book> BOOK_DB = new HashMap<>();
@Override
public List<Book> findAll() {
return new ArrayList<>(BOOK_DB.values());
}
@Override
public Book insertByBook(Book book) {
book.setId(BOOK_DB.size() + 1L);
BOOK_DB.put(book.getId(), book);
return book;
}
@Override
public Book update(Book book) {
BOOK_DB.put(book.getId(), book);
return book;
}
@Override
public Book delete(Long id) {
return BOOK_DB.remove(id);
}
@Override
public Book findById(Long id) {
return BOOK_DB.get(id);
}
}

View File

@@ -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.ModelMap;
import org.springframework.web.bind.annotation.*;
/**
* Book 控制层
*
* Created by bysocket on 30/09/2017.
*/
@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(ModelMap map) {
map.addAttribute("bookList",bookService.findAll());
return BOOK_LIST_PATH_NAME;
}
/**
* 获取创建 Book 表单
*/
@RequestMapping(value = "/create", method = RequestMethod.GET)
public String createBookForm(ModelMap map) {
map.addAttribute("book", new Book());
map.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, ModelMap map) {
map.addAttribute("book", bookService.findById(id));
map.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;
}
}

View File

@@ -0,0 +1,2 @@
/* contentDiv */
.contentDiv {padding:20px 60px;}

Binary file not shown.

After

Width:  |  Height:  |  Size: 946 B

View File

@@ -0,0 +1,58 @@
<!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">
<h5>《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 4 章《模板引擎》Demo </h5>
<legend>
<strong>书籍管理</strong>
</legend>
<form th:action="@{/book/{action}(action=${action})}" method="post" class="form-horizontal">
<input type="hidden" name="id" th:value="${book.id}"/>
<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="提交"/>&nbsp;&nbsp;
<input class="btn" type="button" value="返回" onclick="history.back()"/>
</div>
</div>
</form>
</div>
</body>
</html>

View File

@@ -0,0 +1,46 @@
<!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">
<h5> 《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 4 章《模板引擎》Demo </h5>
<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>

View File

@@ -0,0 +1,16 @@
package demo.springboot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class WebApplicationTests {
@Test
public void contextLoads() {
}
}

View File

@@ -0,0 +1,84 @@
<?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>
<name>chapter-5-spring-boot-data-jpa</name>
<description>《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 5 章《数据存储》Demo</description>
<groupId>demo.springboot</groupId>
<artifactId>chapter-5-spring-boot-data-jpa</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</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>
<!-- Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 模板引擎 Thymeleaf 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Spring Data JPA 依赖 :: 数据持久层框架 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- h2 数据源依赖 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Spring Boot Maven 插件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
-->
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>

View File

@@ -0,0 +1,16 @@
package demo.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Spring Boot 应用启动类
*
* Created by bysocket on 30/09/2017.
*/
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}

View File

@@ -0,0 +1,69 @@
package demo.springboot.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable;
/**
* Book 实体类
*
* Created by bysocket on 30/09/2017.
*/
@Entity
public class Book implements Serializable {
/**
* 编号
*/
@Id
@GeneratedValue
private Long id;
/**
* 书名
*/
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;
}
}

View File

@@ -0,0 +1,11 @@
package demo.springboot.domain;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Book 数据持久层操作接口
*
* Created by bysocket on 09/10/2017.
*/
public interface BookRepository extends JpaRepository<Book, Long> {
}

View File

@@ -0,0 +1,45 @@
package demo.springboot.service;
import demo.springboot.domain.Book;
import java.util.List;
/**
* Book 业务接口层
*
* Created by bysocket on 30/09/2017.
*/
public interface BookService {
/**
* 获取所有 Book
*/
List<Book> findAll();
/**
* 新增 Book
*
* @param book {@link Book}
*/
Book insertByBook(Book book);
/**
* 更新 Book
*
* @param book {@link Book}
*/
Book update(Book book);
/**
* 删除 Book
*
* @param id 编号
*/
Book delete(Long id);
/**
* 获取 Book
*
* @param id 编号
*/
Book findById(Long id);
}

View File

@@ -0,0 +1,48 @@
package demo.springboot.service.impl;
import demo.springboot.domain.Book;
import demo.springboot.domain.BookRepository;
import demo.springboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Book 业务层实现
*
* Created by bysocket on 30/09/2017.
*/
@Service
public class BookServiceImpl implements BookService {
@Autowired
BookRepository bookRepository;
@Override
public List<Book> findAll() {
return bookRepository.findAll();
}
@Override
public Book insertByBook(Book book) {
return bookRepository.save(book);
}
@Override
public Book update(Book book) {
return bookRepository.save(book);
}
@Override
public Book delete(Long id) {
Book book = bookRepository.findById(id).get();
bookRepository.delete(book);
return book;
}
@Override
public Book findById(Long id) {
return bookRepository.findById(id).get();
}
}

View File

@@ -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.ModelMap;
import org.springframework.web.bind.annotation.*;
/**
* Book 控制层
*
* Created by bysocket on 30/09/2017.
*/
@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(ModelMap map) {
map.addAttribute("bookList",bookService.findAll());
return BOOK_LIST_PATH_NAME;
}
/**
* 获取创建 Book 表单
*/
@RequestMapping(value = "/create", method = RequestMethod.GET)
public String createBookForm(ModelMap map) {
map.addAttribute("book", new Book());
map.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, ModelMap map) {
map.addAttribute("book", bookService.findById(id));
map.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;
}
}

View File

@@ -0,0 +1,2 @@
## 是否启动日志 SQL 语句
spring.jpa.show-sql=true

View File

@@ -0,0 +1,2 @@
/* contentDiv */
.contentDiv {padding:20px 60px;}

Binary file not shown.

After

Width:  |  Height:  |  Size: 946 B

View File

@@ -0,0 +1,58 @@
<!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">
<h5>《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 5 章《数据存储》Demo </h5>
<legend>
<strong>书籍管理</strong>
</legend>
<form th:action="@{/book/{action}(action=${action})}" method="post" class="form-horizontal">
<input type="hidden" name="id" th:value="${book.id}"/>
<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="提交"/>&nbsp;&nbsp;
<input class="btn" type="button" value="返回" onclick="history.back()"/>
</div>
</div>
</form>
</div>
</body>
</html>

View File

@@ -0,0 +1,46 @@
<!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">
<h5>《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 5 章《数据存储》Demo </h5>
<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>

View File

@@ -0,0 +1,16 @@
package demo.springboot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class WebApplicationTests {
@Test
public void contextLoads() {
}
}

View File

@@ -0,0 +1,96 @@
<?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>
<name>chapter-6-spring-boot-cache-redis</name>
<description>《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 6 章《数据缓存》Demo</description>
<groupId>demo.springboot</groupId>
<artifactId>chapter-6-spring-boot-cache-redis</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</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>
<!-- Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 模板引擎 Thymeleaf 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Spring Data JPA 依赖 :: 数据持久层框架 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- h2 数据源依赖 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Cache 缓存依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Redis 数据源依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Spring Boot Maven 插件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
-->
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>

View File

@@ -0,0 +1,18 @@
package demo.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
/**
* Spring Boot 应用启动类
*
* Created by bysocket on 30/09/2017.
*/
@SpringBootApplication
@EnableCaching
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}

View File

@@ -0,0 +1,69 @@
package demo.springboot.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable;
/**
* Book 实体类
*
* Created by bysocket on 30/09/2017.
*/
@Entity
public class Book implements Serializable {
/**
* 编号
*/
@Id
@GeneratedValue
private Long id;
/**
* 书名
*/
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;
}
}

View File

@@ -0,0 +1,11 @@
package demo.springboot.domain;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Book 数据持久层操作接口
*
* Created by bysocket on 09/10/2017.
*/
public interface BookRepository extends JpaRepository<Book, Long> {
}

View File

@@ -0,0 +1,45 @@
package demo.springboot.service;
import demo.springboot.domain.Book;
import java.util.List;
/**
* Book 业务接口层
*
* Created by bysocket on 30/09/2017.
*/
public interface BookService {
/**
* 获取所有 Book
*/
List<Book> findAll();
/**
* 新增 Book
*
* @param book {@link Book}
*/
Book insertByBook(Book book);
/**
* 更新 Book
*
* @param book {@link Book}
*/
Book update(Book book);
/**
* 删除 Book
*
* @param id 编号
*/
Book delete(Long id);
/**
* 获取 Book
*
* @param id 编号
*/
Book findById(Long id);
}

View File

@@ -0,0 +1,59 @@
package demo.springboot.service.impl;
import demo.springboot.domain.Book;
import demo.springboot.domain.BookRepository;
import demo.springboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Book 业务层实现
*
* Created by bysocket on 30/09/2017.
*/
@Service
@CacheConfig(cacheNames = "books")
public class BookServiceImpl implements BookService {
@Autowired
BookRepository bookRepository;
@Override
public List<Book> findAll() {
return bookRepository.findAll();
}
@Override
public Book insertByBook(Book book) {
return bookRepository.save(book);
}
@CachePut(key = "#p0.id")
@Override
public Book update(Book book) {
System.out.println(" call update method ");
return bookRepository.save(book);
}
@CacheEvict(key = "#p0")
@Override
public Book delete(Long id) {
System.out.println(" call delete method ");
Book book = bookRepository.findById(id).get();
bookRepository.delete(book);
return book;
}
@Cacheable(key = "#p0")
@Override
public Book findById(Long id) {
System.out.println(" call findById method ");
return bookRepository.findById(id).get();
}
}

View File

@@ -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.ModelMap;
import org.springframework.web.bind.annotation.*;
/**
* Book 控制层
*
* Created by bysocket on 30/09/2017.
*/
@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(ModelMap map) {
map.addAttribute("bookList",bookService.findAll());
return BOOK_LIST_PATH_NAME;
}
/**
* 获取创建 Book 表单
*/
@RequestMapping(value = "/create", method = RequestMethod.GET)
public String createBookForm(ModelMap map) {
map.addAttribute("book", new Book());
map.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, ModelMap map) {
map.addAttribute("book", bookService.findById(id));
map.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;
}
}

View File

@@ -0,0 +1,9 @@
## 是否启动日志 SQL 语句
spring.jpa.show-sql=true
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1

View File

@@ -0,0 +1,2 @@
/* contentDiv */
.contentDiv {padding:20px 60px;}

Binary file not shown.

After

Width:  |  Height:  |  Size: 946 B

View File

@@ -0,0 +1,58 @@
<!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">
<h5>《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 5 章《数据存储》Demo </h5>
<legend>
<strong>书籍管理</strong>
</legend>
<form th:action="@{/book/{action}(action=${action})}" method="post" class="form-horizontal">
<input type="hidden" name="id" th:value="${book.id}"/>
<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="提交"/>&nbsp;&nbsp;
<input class="btn" type="button" value="返回" onclick="history.back()"/>
</div>
</div>
</form>
</div>
</body>
</html>

View File

@@ -0,0 +1,46 @@
<!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">
<h5>《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 5 章《数据存储》Demo </h5>
<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>

View File

@@ -0,0 +1,16 @@
package demo.springboot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class WebApplicationTests {
@Test
public void contextLoads() {
}
}

21
pom.xml
View File

@@ -9,6 +9,14 @@
<packaging>pom</packaging>
<modules>
<!-- Spring Boot 2.0 WebFlux-->
<!-- Spring Boot WebFlux 快速入门 -->
<module>springboot-webflux-1-quickstart</module>
<!-- Spring Boot WebFlux 实现 Restful 服务,基于 HTTP / JSON 传输 -->
<module>springboot-webflux-2-restful</module>
<!-- 基础 入门篇 -->
<!-- Spring Boot 之 HelloWorld 详解 -->
<module>springboot-helloworld</module>
@@ -54,6 +62,19 @@
<module>spring-data-elasticsearch-crud</module>
<module>spring-data-elasticsearch-query</module>
<!-- 第 1 章《Spring Boot 入门》 -->
<module>chapter-1-spring-boot-quickstart</module>
<!-- 第 2 章《配置》 -->
<module>chapter-2-spring-boot-config</module>
<!-- 第 3 章《Web 开发》 -->
<module>chapter-3-spring-boot-web</module>
<!-- 第 4 章《模板引擎》 -->
<module>chapter-4-spring-boot-web-thymeleaf</module>
<!-- 第 5 章《数据存储》 -->
<module>chapter-5-spring-boot-data-jpa</module>
<!-- 第 6 章《数据缓存》 -->
<module>chapter-6-spring-boot-cache-redis</module>
<!-- 第四章表单校验案例 -->
<module>chapter-4-spring-boot-validating-form-input</module>
<!-- 第五章数据分页排序案例 -->

View File

@@ -1,59 +0,0 @@
<?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>
<groupId>springboot</groupId>
<artifactId>springboot-webflux</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-webflux :: Spring Boot 实现 WebFlux HTTP Restful 服务</name>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M3</version>
</parent>
<properties>
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
<mysql-connector>5.1.39</mysql-connector>
</properties>
<dependencies>
<!-- Spring Boot Web Flux 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>

View File

@@ -1,20 +0,0 @@
package org.spring.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Spring Boot 应用启动类
*
* Created by bysocket on 09/29/2017.
*/
// Spring Boot 应用的标识
@SpringBootApplication
public class Application {
public static void main(String[] args) {
// 程序启动入口
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication.run(Application.class,args);
}
}

View File

@@ -1,51 +0,0 @@
package org.spring.springboot.controller;
import org.spring.springboot.domain.City;
import org.spring.springboot.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* 城市 Controller 实现 Restful HTTP 服务
* <p>
* Created by bysocket on 09/29/2017.
*/
@RestController
@RequestMapping(value = "/city")
public class CityRestController {
@Autowired
private CityService cityService;
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Mono<City> findOneCity(@PathVariable("id") Long id) {
return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.findCityById(id)));
}
@RequestMapping(method = RequestMethod.GET)
public Flux<City> findAllCity() {
return Flux.create(cityFluxSink -> {
cityService.findAllCity().forEach(city -> {
cityFluxSink.next(city);
});
cityFluxSink.complete();
});
}
@RequestMapping(method = RequestMethod.POST)
public Mono<Long> createCity(@RequestBody City city) {
return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.saveCity(city)));
}
@RequestMapping(method = RequestMethod.PUT)
public Mono<Long> modifyCity(@RequestBody City city) {
return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.updateCity(city)));
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public Mono<Long> modifyCity(@PathVariable("id") Long id) {
return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.deleteCity(id)));
}
}

View File

@@ -1,61 +0,0 @@
package org.spring.springboot.domain;
/**
* 城市实体类
*
* Created by bysocket on 09/29/2017.
*/
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;
}
}

View File

@@ -1,52 +0,0 @@
package org.spring.springboot.service;
import org.spring.springboot.domain.City;
import java.util.List;
/**
* 城市业务逻辑接口类
*
* Created by bysocket on 09/29/2017.
*/
public interface CityService {
/**
* 获取城市信息列表
*
* @return
*/
List<City> findAllCity();
/**
* 根据城市 ID,查询城市信息
*
* @param id
* @return
*/
City findCityById(Long id);
/**
* 新增城市信息
*
* @param city
* @return
*/
Long saveCity(City city);
/**
* 更新城市信息
*
* @param city
* @return
*/
Long updateCity(City city);
/**
* 根据城市 ID,删除城市信息
*
* @param id
* @return
*/
Long deleteCity(Long id);
}

View File

@@ -1,50 +0,0 @@
package org.spring.springboot.service.impl;
import org.spring.springboot.domain.City;
import org.spring.springboot.service.CityService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 城市业务逻辑实现类
*
* Created by bysocket on 09/29/2017.
*/
@Service
public class CityServiceImpl implements CityService {
// 模拟数据库,存储 City 信息
private static Map<Long, City> CITY_DB = new HashMap<>();
public List<City> findAllCity() {
return new ArrayList<>(CITY_DB.values());
}
public City findCityById(Long id) {
return CITY_DB.get(id);
}
@Override
public Long saveCity(City city) {
city.setId(CITY_DB.size() + 1L);
CITY_DB.put(city.getId(), city);
return city.getId();
}
@Override
public Long updateCity(City city) {
CITY_DB.put(city.getId(), city);
return city.getId();
}
@Override
public Long deleteCity(Long id) {
CITY_DB.remove(id);
return id;
}
}