From fc5200546c170d9a9c12ae79bfbe06503831c7f3 Mon Sep 17 00:00:00 2001 From: liqiangqiang Date: Sun, 8 Apr 2018 14:46:30 +0800 Subject: [PATCH] =?UTF-8?q?Spring=20Boot=20=E5=85=A5=E9=97=A8=E3=80=81Spri?= =?UTF-8?q?ng=20Boot=20=E9=85=8D=E7=BD=AE=E3=80=81Web=20=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E3=80=81=E6=A8=A1=E6=9D=BF=E5=BC=95=E6=93=8E=E3=80=81=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=AD=98=E5=82=A8=E3=80=81=E6=95=B0=E6=8D=AE=E7=BC=93?= =?UTF-8?q?=E5=AD=98=20=E6=A1=88=E4=BE=8B=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++ chapter-1-spring-boot-quickstart/pom.xml | 64 ++++++++++++ .../springboot/QuickStartApplication.java | 16 +++ .../springboot/web/HelloBookController.java | 19 ++++ .../demo/springboot/web/HelloController.java | 21 ++++ .../src/main/resources/application.properties | 0 .../QuickStartApplicationTests.java | 16 +++ chapter-2-spring-boot-config/pom.xml | 73 +++++++++++++ .../demo/springboot/ConfigApplication.java | 18 ++++ .../demo/springboot/config/BookComponent.java | 46 +++++++++ .../springboot/config/BookProperties.java | 41 ++++++++ .../springboot/web/HelloBookController.java | 24 +++++ .../main/resources/application-dev.properties | 3 + .../resources/application-prod.properties | 3 + .../src/main/resources/application.properties | 5 + .../src/main/resources/application.yml | 5 + .../springboot/ConfigApplicationTests.java | 33 ++++++ chapter-3-spring-boot-web/pom.xml | 65 ++++++++++++ .../java/demo/springboot/WebApplication.java | 16 +++ .../java/demo/springboot/domain/Book.java | 63 ++++++++++++ .../demo/springboot/service/BookService.java | 45 ++++++++ .../service/impl/BookServiceImpl.java | 51 ++++++++++ .../demo/springboot/web/BookController.java | 68 +++++++++++++ .../src/main/resources/application.properties | 0 .../demo/springboot/WebApplicationTests.java | 16 +++ chapter-4-spring-boot-web-thymeleaf/pom.xml | 71 +++++++++++++ .../java/demo/springboot/WebApplication.java | 16 +++ .../java/demo/springboot/domain/Book.java | 63 ++++++++++++ .../demo/springboot/service/BookService.java | 45 ++++++++ .../service/impl/BookServiceImpl.java | 51 ++++++++++ .../demo/springboot/web/BookController.java | 89 ++++++++++++++++ .../src/main/resources/application.properties | 0 .../src/main/resources/static/css/default.css | 2 + .../main/resources/static/images/favicon.ico | Bin 0 -> 946 bytes .../main/resources/templates/bookForm.html | 58 +++++++++++ .../main/resources/templates/bookList.html | 46 +++++++++ .../demo/springboot/WebApplicationTests.java | 16 +++ chapter-5-spring-boot-data-jpa/pom.xml | 84 +++++++++++++++ .../java/demo/springboot/WebApplication.java | 16 +++ .../java/demo/springboot/domain/Book.java | 69 +++++++++++++ .../springboot/domain/BookRepository.java | 11 ++ .../demo/springboot/service/BookService.java | 45 ++++++++ .../service/impl/BookServiceImpl.java | 48 +++++++++ .../demo/springboot/web/BookController.java | 89 ++++++++++++++++ .../src/main/resources/application.properties | 2 + .../src/main/resources/static/css/default.css | 2 + .../main/resources/static/images/favicon.ico | Bin 0 -> 946 bytes .../main/resources/templates/bookForm.html | 58 +++++++++++ .../main/resources/templates/bookList.html | 46 +++++++++ .../demo/springboot/WebApplicationTests.java | 16 +++ chapter-6-spring-boot-cache-redis/pom.xml | 96 ++++++++++++++++++ .../java/demo/springboot/WebApplication.java | 18 ++++ .../java/demo/springboot/domain/Book.java | 69 +++++++++++++ .../springboot/domain/BookRepository.java | 11 ++ .../demo/springboot/service/BookService.java | 45 ++++++++ .../service/impl/BookServiceImpl.java | 59 +++++++++++ .../demo/springboot/web/BookController.java | 89 ++++++++++++++++ .../src/main/resources/application.properties | 9 ++ .../src/main/resources/static/css/default.css | 2 + .../main/resources/static/images/favicon.ico | Bin 0 -> 946 bytes .../main/resources/templates/bookForm.html | 58 +++++++++++ .../main/resources/templates/bookList.html | 46 +++++++++ .../demo/springboot/WebApplicationTests.java | 16 +++ pom.xml | 21 ++++ springboot-webflux/pom.xml | 59 ----------- .../org/spring/springboot/Application.java | 20 ---- .../controller/CityRestController.java | 51 ---------- .../org/spring/springboot/domain/City.java | 61 ----------- .../springboot/service/CityService.java | 52 ---------- .../service/impl/CityServiceImpl.java | 50 --------- 70 files changed, 2201 insertions(+), 293 deletions(-) create mode 100644 chapter-1-spring-boot-quickstart/pom.xml create mode 100644 chapter-1-spring-boot-quickstart/src/main/java/demo/springboot/QuickStartApplication.java create mode 100644 chapter-1-spring-boot-quickstart/src/main/java/demo/springboot/web/HelloBookController.java create mode 100644 chapter-1-spring-boot-quickstart/src/main/java/demo/springboot/web/HelloController.java rename {springboot-webflux => chapter-1-spring-boot-quickstart}/src/main/resources/application.properties (100%) create mode 100644 chapter-1-spring-boot-quickstart/src/test/java/demo/springboot/QuickStartApplicationTests.java create mode 100644 chapter-2-spring-boot-config/pom.xml create mode 100644 chapter-2-spring-boot-config/src/main/java/demo/springboot/ConfigApplication.java create mode 100644 chapter-2-spring-boot-config/src/main/java/demo/springboot/config/BookComponent.java create mode 100644 chapter-2-spring-boot-config/src/main/java/demo/springboot/config/BookProperties.java create mode 100644 chapter-2-spring-boot-config/src/main/java/demo/springboot/web/HelloBookController.java create mode 100644 chapter-2-spring-boot-config/src/main/resources/application-dev.properties create mode 100644 chapter-2-spring-boot-config/src/main/resources/application-prod.properties create mode 100644 chapter-2-spring-boot-config/src/main/resources/application.properties create mode 100644 chapter-2-spring-boot-config/src/main/resources/application.yml create mode 100644 chapter-2-spring-boot-config/src/test/java/demo/springboot/ConfigApplicationTests.java create mode 100644 chapter-3-spring-boot-web/pom.xml create mode 100644 chapter-3-spring-boot-web/src/main/java/demo/springboot/WebApplication.java create mode 100644 chapter-3-spring-boot-web/src/main/java/demo/springboot/domain/Book.java create mode 100644 chapter-3-spring-boot-web/src/main/java/demo/springboot/service/BookService.java create mode 100644 chapter-3-spring-boot-web/src/main/java/demo/springboot/service/impl/BookServiceImpl.java create mode 100644 chapter-3-spring-boot-web/src/main/java/demo/springboot/web/BookController.java create mode 100644 chapter-3-spring-boot-web/src/main/resources/application.properties create mode 100644 chapter-3-spring-boot-web/src/test/java/demo/springboot/WebApplicationTests.java create mode 100644 chapter-4-spring-boot-web-thymeleaf/pom.xml create mode 100644 chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/WebApplication.java create mode 100644 chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/domain/Book.java create mode 100644 chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/service/BookService.java create mode 100644 chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/service/impl/BookServiceImpl.java create mode 100644 chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/web/BookController.java create mode 100644 chapter-4-spring-boot-web-thymeleaf/src/main/resources/application.properties create mode 100755 chapter-4-spring-boot-web-thymeleaf/src/main/resources/static/css/default.css create mode 100755 chapter-4-spring-boot-web-thymeleaf/src/main/resources/static/images/favicon.ico create mode 100644 chapter-4-spring-boot-web-thymeleaf/src/main/resources/templates/bookForm.html create mode 100644 chapter-4-spring-boot-web-thymeleaf/src/main/resources/templates/bookList.html create mode 100644 chapter-4-spring-boot-web-thymeleaf/src/test/java/demo/springboot/WebApplicationTests.java create mode 100644 chapter-5-spring-boot-data-jpa/pom.xml create mode 100644 chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/WebApplication.java create mode 100644 chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/domain/Book.java create mode 100644 chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/domain/BookRepository.java create mode 100644 chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/service/BookService.java create mode 100644 chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/service/impl/BookServiceImpl.java create mode 100644 chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/web/BookController.java create mode 100644 chapter-5-spring-boot-data-jpa/src/main/resources/application.properties create mode 100755 chapter-5-spring-boot-data-jpa/src/main/resources/static/css/default.css create mode 100755 chapter-5-spring-boot-data-jpa/src/main/resources/static/images/favicon.ico create mode 100644 chapter-5-spring-boot-data-jpa/src/main/resources/templates/bookForm.html create mode 100644 chapter-5-spring-boot-data-jpa/src/main/resources/templates/bookList.html create mode 100644 chapter-5-spring-boot-data-jpa/src/test/java/demo/springboot/WebApplicationTests.java create mode 100644 chapter-6-spring-boot-cache-redis/pom.xml create mode 100644 chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/WebApplication.java create mode 100644 chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/domain/Book.java create mode 100644 chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/domain/BookRepository.java create mode 100644 chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/service/BookService.java create mode 100644 chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/service/impl/BookServiceImpl.java create mode 100644 chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/web/BookController.java create mode 100644 chapter-6-spring-boot-cache-redis/src/main/resources/application.properties create mode 100755 chapter-6-spring-boot-cache-redis/src/main/resources/static/css/default.css create mode 100755 chapter-6-spring-boot-cache-redis/src/main/resources/static/images/favicon.ico create mode 100644 chapter-6-spring-boot-cache-redis/src/main/resources/templates/bookForm.html create mode 100644 chapter-6-spring-boot-cache-redis/src/main/resources/templates/bookList.html create mode 100644 chapter-6-spring-boot-cache-redis/src/test/java/demo/springboot/WebApplicationTests.java delete mode 100755 springboot-webflux/pom.xml delete mode 100644 springboot-webflux/src/main/java/org/spring/springboot/Application.java delete mode 100644 springboot-webflux/src/main/java/org/spring/springboot/controller/CityRestController.java delete mode 100644 springboot-webflux/src/main/java/org/spring/springboot/domain/City.java delete mode 100644 springboot-webflux/src/main/java/org/spring/springboot/service/CityService.java delete mode 100644 springboot-webflux/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java diff --git a/README.md b/README.md index 9cccbab..cc17360 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,14 @@ Web Flux 努力进行中,尽情期待。唯一文章入口:[GitChat文章地 ## 一、项目结构 「Spring Boot 那些事」:[传送门](http://www.bysocket.com/?page_id=1639 "Spring Boot 那些事")
+### 『 WebFlux 篇 』 + +WebFlux 系类文章入口:[GitChat文章地址](http://gitbook.cn/gitchat/author/58968d35f2b669527d7a7c57 "gitchat") + +- springboot-webflux
+Spring Boot WebFlux 实现 Restful 服务 + + #### a. 『 基础 - 入门篇 』 - springboot-helloworld
[《Spring Boot 之 HelloWorld 详解》](http://www.bysocket.com/?p=1124 "Spring Boot 之 HelloWorld详解")
diff --git a/chapter-1-spring-boot-quickstart/pom.xml b/chapter-1-spring-boot-quickstart/pom.xml new file mode 100644 index 0000000..b709729 --- /dev/null +++ b/chapter-1-spring-boot-quickstart/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + chapter-1-spring-boot-quickstart + 《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 1 章《Spring Boot 入门》Demo + + demo.springboot + chapter-1-spring-boot-quickstart + 1.0 + jar + + + org.springframework.boot + spring-boot-starter-parent + 1.5.1.RELEASE + + + + UTF-8 + UTF-8 + 1.8 + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/libs-snapshot + + true + + + + + diff --git a/chapter-1-spring-boot-quickstart/src/main/java/demo/springboot/QuickStartApplication.java b/chapter-1-spring-boot-quickstart/src/main/java/demo/springboot/QuickStartApplication.java new file mode 100644 index 0000000..c479172 --- /dev/null +++ b/chapter-1-spring-boot-quickstart/src/main/java/demo/springboot/QuickStartApplication.java @@ -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); + } +} diff --git a/chapter-1-spring-boot-quickstart/src/main/java/demo/springboot/web/HelloBookController.java b/chapter-1-spring-boot-quickstart/src/main/java/demo/springboot/web/HelloBookController.java new file mode 100644 index 0000000..403533e --- /dev/null +++ b/chapter-1-spring-boot-quickstart/src/main/java/demo/springboot/web/HelloBookController.java @@ -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 核心技术实战 - 上 基础篇》!"; + } +} diff --git a/chapter-1-spring-boot-quickstart/src/main/java/demo/springboot/web/HelloController.java b/chapter-1-spring-boot-quickstart/src/main/java/demo/springboot/web/HelloController.java new file mode 100644 index 0000000..21bb3c6 --- /dev/null +++ b/chapter-1-spring-boot-quickstart/src/main/java/demo/springboot/web/HelloController.java @@ -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 "Hello,Spring Boot!"; + } +} diff --git a/springboot-webflux/src/main/resources/application.properties b/chapter-1-spring-boot-quickstart/src/main/resources/application.properties similarity index 100% rename from springboot-webflux/src/main/resources/application.properties rename to chapter-1-spring-boot-quickstart/src/main/resources/application.properties diff --git a/chapter-1-spring-boot-quickstart/src/test/java/demo/springboot/QuickStartApplicationTests.java b/chapter-1-spring-boot-quickstart/src/test/java/demo/springboot/QuickStartApplicationTests.java new file mode 100644 index 0000000..5220ed5 --- /dev/null +++ b/chapter-1-spring-boot-quickstart/src/test/java/demo/springboot/QuickStartApplicationTests.java @@ -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() { + } + +} diff --git a/chapter-2-spring-boot-config/pom.xml b/chapter-2-spring-boot-config/pom.xml new file mode 100644 index 0000000..cc088fc --- /dev/null +++ b/chapter-2-spring-boot-config/pom.xml @@ -0,0 +1,73 @@ + + + 4.0.0 + chapter-2-spring-boot-config + 《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 2 章《Spring Boot 配置》Demo + + demo.springboot + chapter-2-spring-boot-config + 1.0 + jar + + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.BUILD-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + com.spring4all + spring-boot-starter-swagger + 1.5.1.RELEASE + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + 1.5.1.RELEASE + + + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/libs-snapshot + + true + + + + + diff --git a/chapter-2-spring-boot-config/src/main/java/demo/springboot/ConfigApplication.java b/chapter-2-spring-boot-config/src/main/java/demo/springboot/ConfigApplication.java new file mode 100644 index 0000000..2c0633c --- /dev/null +++ b/chapter-2-spring-boot-config/src/main/java/demo/springboot/ConfigApplication.java @@ -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); + } +} diff --git a/chapter-2-spring-boot-config/src/main/java/demo/springboot/config/BookComponent.java b/chapter-2-spring-boot-config/src/main/java/demo/springboot/config/BookComponent.java new file mode 100644 index 0000000..6a1a5f8 --- /dev/null +++ b/chapter-2-spring-boot-config/src/main/java/demo/springboot/config/BookComponent.java @@ -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; + } +} diff --git a/chapter-2-spring-boot-config/src/main/java/demo/springboot/config/BookProperties.java b/chapter-2-spring-boot-config/src/main/java/demo/springboot/config/BookProperties.java new file mode 100644 index 0000000..42dd0c3 --- /dev/null +++ b/chapter-2-spring-boot-config/src/main/java/demo/springboot/config/BookProperties.java @@ -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; + } +} diff --git a/chapter-2-spring-boot-config/src/main/java/demo/springboot/web/HelloBookController.java b/chapter-2-spring-boot-config/src/main/java/demo/springboot/web/HelloBookController.java new file mode 100644 index 0000000..106fe6e --- /dev/null +++ b/chapter-2-spring-boot-config/src/main/java/demo/springboot/web/HelloBookController.java @@ -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() + " !"; + } +} diff --git a/chapter-2-spring-boot-config/src/main/resources/application-dev.properties b/chapter-2-spring-boot-config/src/main/resources/application-dev.properties new file mode 100644 index 0000000..a296c07 --- /dev/null +++ b/chapter-2-spring-boot-config/src/main/resources/application-dev.properties @@ -0,0 +1,3 @@ +## \u4E66\u4FE1\u606F +demo.book.name=[Spring Boot 2.x Core Action] From Dev +demo.book.writer=BYSocket diff --git a/chapter-2-spring-boot-config/src/main/resources/application-prod.properties b/chapter-2-spring-boot-config/src/main/resources/application-prod.properties new file mode 100644 index 0000000..df01f61 --- /dev/null +++ b/chapter-2-spring-boot-config/src/main/resources/application-prod.properties @@ -0,0 +1,3 @@ +## \u4E66\u4FE1\u606F +demo.book.name=[Spring Boot 2.x Core Action] From Prod +demo.book.writer=BYSocket diff --git a/chapter-2-spring-boot-config/src/main/resources/application.properties b/chapter-2-spring-boot-config/src/main/resources/application.properties new file mode 100644 index 0000000..0315d1f --- /dev/null +++ b/chapter-2-spring-boot-config/src/main/resources/application.properties @@ -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} + diff --git a/chapter-2-spring-boot-config/src/main/resources/application.yml b/chapter-2-spring-boot-config/src/main/resources/application.yml new file mode 100644 index 0000000..4551e78 --- /dev/null +++ b/chapter-2-spring-boot-config/src/main/resources/application.yml @@ -0,0 +1,5 @@ +## 书信息 +demo: + book: + name: 《Spring Boot 2.x 核心技术实战 - 上 基础篇》 + writer: 泥瓦匠BYSocket \ No newline at end of file diff --git a/chapter-2-spring-boot-config/src/test/java/demo/springboot/ConfigApplicationTests.java b/chapter-2-spring-boot-config/src/test/java/demo/springboot/ConfigApplicationTests.java new file mode 100644 index 0000000..1b6055b --- /dev/null +++ b/chapter-2-spring-boot-config/src/test/java/demo/springboot/ConfigApplicationTests.java @@ -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"); + } +} diff --git a/chapter-3-spring-boot-web/pom.xml b/chapter-3-spring-boot-web/pom.xml new file mode 100644 index 0000000..b40d56f --- /dev/null +++ b/chapter-3-spring-boot-web/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + chapter-3-spring-boot-web + 《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 3 章《Web 开发》Demo + + demo.springboot + chapter-3-spring-boot-web + 1.0 + jar + + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.BUILD-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/libs-snapshot + + true + + + + + diff --git a/chapter-3-spring-boot-web/src/main/java/demo/springboot/WebApplication.java b/chapter-3-spring-boot-web/src/main/java/demo/springboot/WebApplication.java new file mode 100644 index 0000000..ffd616f --- /dev/null +++ b/chapter-3-spring-boot-web/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 on 26/09/2017. + */ +@SpringBootApplication +public class WebApplication { + public static void main(String[] args) { + SpringApplication.run(WebApplication.class, args); + } +} diff --git a/chapter-3-spring-boot-web/src/main/java/demo/springboot/domain/Book.java b/chapter-3-spring-boot-web/src/main/java/demo/springboot/domain/Book.java new file mode 100644 index 0000000..29dda95 --- /dev/null +++ b/chapter-3-spring-boot-web/src/main/java/demo/springboot/domain/Book.java @@ -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; + } +} diff --git a/chapter-3-spring-boot-web/src/main/java/demo/springboot/service/BookService.java b/chapter-3-spring-boot-web/src/main/java/demo/springboot/service/BookService.java new file mode 100644 index 0000000..8fb63d1 --- /dev/null +++ b/chapter-3-spring-boot-web/src/main/java/demo/springboot/service/BookService.java @@ -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 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); +} diff --git a/chapter-3-spring-boot-web/src/main/java/demo/springboot/service/impl/BookServiceImpl.java b/chapter-3-spring-boot-web/src/main/java/demo/springboot/service/impl/BookServiceImpl.java new file mode 100644 index 0000000..8d63aaf --- /dev/null +++ b/chapter-3-spring-boot-web/src/main/java/demo/springboot/service/impl/BookServiceImpl.java @@ -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 BOOK_DB = new HashMap<>(); + + @Override + public List 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); + } +} diff --git a/chapter-3-spring-boot-web/src/main/java/demo/springboot/web/BookController.java b/chapter-3-spring-boot-web/src/main/java/demo/springboot/web/BookController.java new file mode 100644 index 0000000..3f85249 --- /dev/null +++ b/chapter-3-spring-boot-web/src/main/java/demo/springboot/web/BookController.java @@ -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 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); + } + +} diff --git a/chapter-3-spring-boot-web/src/main/resources/application.properties b/chapter-3-spring-boot-web/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/chapter-3-spring-boot-web/src/test/java/demo/springboot/WebApplicationTests.java b/chapter-3-spring-boot-web/src/test/java/demo/springboot/WebApplicationTests.java new file mode 100644 index 0000000..ebd0f53 --- /dev/null +++ b/chapter-3-spring-boot-web/src/test/java/demo/springboot/WebApplicationTests.java @@ -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() { + } + +} diff --git a/chapter-4-spring-boot-web-thymeleaf/pom.xml b/chapter-4-spring-boot-web-thymeleaf/pom.xml new file mode 100644 index 0000000..2d3bfff --- /dev/null +++ b/chapter-4-spring-boot-web-thymeleaf/pom.xml @@ -0,0 +1,71 @@ + + + 4.0.0 + chapter-4-spring-boot-web-thymeleaf + 《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 4 章《模板引擎》Demo + + demo.springboot + chapter-4-spring-boot-web-thymeleaf + 1.0 + jar + + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.BUILD-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/libs-snapshot + + true + + + + + diff --git a/chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/WebApplication.java b/chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/WebApplication.java new file mode 100644 index 0000000..0dbc1f2 --- /dev/null +++ b/chapter-4-spring-boot-web-thymeleaf/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 on 30/09/2017. + */ +@SpringBootApplication +public class WebApplication { + public static void main(String[] args) { + SpringApplication.run(WebApplication.class, args); + } +} diff --git a/chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/domain/Book.java b/chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/domain/Book.java new file mode 100644 index 0000000..46a747a --- /dev/null +++ b/chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/domain/Book.java @@ -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; + } +} diff --git a/chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/service/BookService.java b/chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/service/BookService.java new file mode 100644 index 0000000..d2323b9 --- /dev/null +++ b/chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/service/BookService.java @@ -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 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); +} diff --git a/chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/service/impl/BookServiceImpl.java b/chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/service/impl/BookServiceImpl.java new file mode 100644 index 0000000..35df408 --- /dev/null +++ b/chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/service/impl/BookServiceImpl.java @@ -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 BOOK_DB = new HashMap<>(); + + @Override + public List 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); + } +} diff --git a/chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/web/BookController.java b/chapter-4-spring-boot-web-thymeleaf/src/main/java/demo/springboot/web/BookController.java new file mode 100644 index 0000000..3c6ac11 --- /dev/null +++ b/chapter-4-spring-boot-web-thymeleaf/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.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; + } + +} diff --git a/chapter-4-spring-boot-web-thymeleaf/src/main/resources/application.properties b/chapter-4-spring-boot-web-thymeleaf/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/chapter-4-spring-boot-web-thymeleaf/src/main/resources/static/css/default.css b/chapter-4-spring-boot-web-thymeleaf/src/main/resources/static/css/default.css new file mode 100755 index 0000000..4da3051 --- /dev/null +++ b/chapter-4-spring-boot-web-thymeleaf/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/chapter-4-spring-boot-web-thymeleaf/src/main/resources/static/images/favicon.ico b/chapter-4-spring-boot-web-thymeleaf/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/chapter-4-spring-boot-web-thymeleaf/src/main/resources/templates/bookForm.html b/chapter-4-spring-boot-web-thymeleaf/src/main/resources/templates/bookForm.html new file mode 100644 index 0000000..27d7efe --- /dev/null +++ b/chapter-4-spring-boot-web-thymeleaf/src/main/resources/templates/bookForm.html @@ -0,0 +1,58 @@ + + + + + + + + + 书籍管理 + + + +
+ +
《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 4 章《模板引擎》Demo
+ + + 书籍管理 + + +
+ + + +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+    + +
+
+
+
+ + \ No newline at end of file diff --git a/chapter-4-spring-boot-web-thymeleaf/src/main/resources/templates/bookList.html b/chapter-4-spring-boot-web-thymeleaf/src/main/resources/templates/bookList.html new file mode 100644 index 0000000..a0091df --- /dev/null +++ b/chapter-4-spring-boot-web-thymeleaf/src/main/resources/templates/bookList.html @@ -0,0 +1,46 @@ + + + + + + + + + 书籍列表 + + + + +
+ +
《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 4 章《模板引擎》Demo
+ + + + 书籍列表 + + + + + + + + + + + + + + + + + + + +
书籍编号书名作者简介管理
删除
+ + +
+ + + \ No newline at end of file diff --git a/chapter-4-spring-boot-web-thymeleaf/src/test/java/demo/springboot/WebApplicationTests.java b/chapter-4-spring-boot-web-thymeleaf/src/test/java/demo/springboot/WebApplicationTests.java new file mode 100644 index 0000000..ebd0f53 --- /dev/null +++ b/chapter-4-spring-boot-web-thymeleaf/src/test/java/demo/springboot/WebApplicationTests.java @@ -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() { + } + +} diff --git a/chapter-5-spring-boot-data-jpa/pom.xml b/chapter-5-spring-boot-data-jpa/pom.xml new file mode 100644 index 0000000..fd1d2eb --- /dev/null +++ b/chapter-5-spring-boot-data-jpa/pom.xml @@ -0,0 +1,84 @@ + + + 4.0.0 + chapter-5-spring-boot-data-jpa + 《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 5 章《数据存储》Demo + + demo.springboot + chapter-5-spring-boot-data-jpa + 1.0 + jar + + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.BUILD-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + + com.h2database + h2 + runtime + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/libs-snapshot + + true + + + + + diff --git a/chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/WebApplication.java b/chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/WebApplication.java new file mode 100644 index 0000000..0dbc1f2 --- /dev/null +++ b/chapter-5-spring-boot-data-jpa/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 on 30/09/2017. + */ +@SpringBootApplication +public class WebApplication { + public static void main(String[] args) { + SpringApplication.run(WebApplication.class, args); + } +} diff --git a/chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/domain/Book.java b/chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/domain/Book.java new file mode 100644 index 0000000..5ce99aa --- /dev/null +++ b/chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/domain/Book.java @@ -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; + } +} diff --git a/chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/domain/BookRepository.java b/chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/domain/BookRepository.java new file mode 100644 index 0000000..e0796e0 --- /dev/null +++ b/chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/domain/BookRepository.java @@ -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 { +} diff --git a/chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/service/BookService.java b/chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/service/BookService.java new file mode 100644 index 0000000..d2323b9 --- /dev/null +++ b/chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/service/BookService.java @@ -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 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); +} diff --git a/chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/service/impl/BookServiceImpl.java b/chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/service/impl/BookServiceImpl.java new file mode 100644 index 0000000..62c59be --- /dev/null +++ b/chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/service/impl/BookServiceImpl.java @@ -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 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(); + } +} diff --git a/chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/web/BookController.java b/chapter-5-spring-boot-data-jpa/src/main/java/demo/springboot/web/BookController.java new file mode 100644 index 0000000..3c6ac11 --- /dev/null +++ b/chapter-5-spring-boot-data-jpa/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.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; + } + +} diff --git a/chapter-5-spring-boot-data-jpa/src/main/resources/application.properties b/chapter-5-spring-boot-data-jpa/src/main/resources/application.properties new file mode 100644 index 0000000..b9919f8 --- /dev/null +++ b/chapter-5-spring-boot-data-jpa/src/main/resources/application.properties @@ -0,0 +1,2 @@ +## 是否启动日志 SQL 语句 +spring.jpa.show-sql=true \ No newline at end of file diff --git a/chapter-5-spring-boot-data-jpa/src/main/resources/static/css/default.css b/chapter-5-spring-boot-data-jpa/src/main/resources/static/css/default.css new file mode 100755 index 0000000..4da3051 --- /dev/null +++ b/chapter-5-spring-boot-data-jpa/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/chapter-5-spring-boot-data-jpa/src/main/resources/static/images/favicon.ico b/chapter-5-spring-boot-data-jpa/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/chapter-5-spring-boot-data-jpa/src/main/resources/templates/bookForm.html b/chapter-5-spring-boot-data-jpa/src/main/resources/templates/bookForm.html new file mode 100644 index 0000000..43118b6 --- /dev/null +++ b/chapter-5-spring-boot-data-jpa/src/main/resources/templates/bookForm.html @@ -0,0 +1,58 @@ + + + + + + + + + 书籍管理 + + + +
+ +
《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 5 章《数据存储》Demo
+ + + 书籍管理 + + +
+ + + +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+    + +
+
+
+
+ + \ No newline at end of file diff --git a/chapter-5-spring-boot-data-jpa/src/main/resources/templates/bookList.html b/chapter-5-spring-boot-data-jpa/src/main/resources/templates/bookList.html new file mode 100644 index 0000000..2ab11c6 --- /dev/null +++ b/chapter-5-spring-boot-data-jpa/src/main/resources/templates/bookList.html @@ -0,0 +1,46 @@ + + + + + + + + + 书籍列表 + + + + +
+ +
《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 5 章《数据存储》Demo
+ + + + 书籍列表 + + + + + + + + + + + + + + + + + + + +
书籍编号书名作者简介管理
删除
+ + +
+ + + \ No newline at end of file diff --git a/chapter-5-spring-boot-data-jpa/src/test/java/demo/springboot/WebApplicationTests.java b/chapter-5-spring-boot-data-jpa/src/test/java/demo/springboot/WebApplicationTests.java new file mode 100644 index 0000000..ebd0f53 --- /dev/null +++ b/chapter-5-spring-boot-data-jpa/src/test/java/demo/springboot/WebApplicationTests.java @@ -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() { + } + +} diff --git a/chapter-6-spring-boot-cache-redis/pom.xml b/chapter-6-spring-boot-cache-redis/pom.xml new file mode 100644 index 0000000..690eb60 --- /dev/null +++ b/chapter-6-spring-boot-cache-redis/pom.xml @@ -0,0 +1,96 @@ + + + 4.0.0 + chapter-6-spring-boot-cache-redis + 《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 6 章《数据缓存》Demo + + demo.springboot + chapter-6-spring-boot-cache-redis + 1.0 + jar + + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.BUILD-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + + com.h2database + h2 + runtime + + + + + org.springframework.boot + spring-boot-starter-cache + + + + + org.springframework.boot + spring-boot-starter-redis + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/libs-snapshot + + true + + + + + diff --git a/chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/WebApplication.java b/chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/WebApplication.java new file mode 100644 index 0000000..7b7d220 --- /dev/null +++ b/chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/WebApplication.java @@ -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); + } +} diff --git a/chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/domain/Book.java b/chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/domain/Book.java new file mode 100644 index 0000000..5ce99aa --- /dev/null +++ b/chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/domain/Book.java @@ -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; + } +} diff --git a/chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/domain/BookRepository.java b/chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/domain/BookRepository.java new file mode 100644 index 0000000..e0796e0 --- /dev/null +++ b/chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/domain/BookRepository.java @@ -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 { +} diff --git a/chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/service/BookService.java b/chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/service/BookService.java new file mode 100644 index 0000000..d2323b9 --- /dev/null +++ b/chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/service/BookService.java @@ -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 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); +} diff --git a/chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/service/impl/BookServiceImpl.java b/chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/service/impl/BookServiceImpl.java new file mode 100644 index 0000000..d2b957e --- /dev/null +++ b/chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/service/impl/BookServiceImpl.java @@ -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 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(); + } +} diff --git a/chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/web/BookController.java b/chapter-6-spring-boot-cache-redis/src/main/java/demo/springboot/web/BookController.java new file mode 100644 index 0000000..3c6ac11 --- /dev/null +++ b/chapter-6-spring-boot-cache-redis/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.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; + } + +} diff --git a/chapter-6-spring-boot-cache-redis/src/main/resources/application.properties b/chapter-6-spring-boot-cache-redis/src/main/resources/application.properties new file mode 100644 index 0000000..b0d364a --- /dev/null +++ b/chapter-6-spring-boot-cache-redis/src/main/resources/application.properties @@ -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 \ No newline at end of file diff --git a/chapter-6-spring-boot-cache-redis/src/main/resources/static/css/default.css b/chapter-6-spring-boot-cache-redis/src/main/resources/static/css/default.css new file mode 100755 index 0000000..4da3051 --- /dev/null +++ b/chapter-6-spring-boot-cache-redis/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/chapter-6-spring-boot-cache-redis/src/main/resources/static/images/favicon.ico b/chapter-6-spring-boot-cache-redis/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/chapter-6-spring-boot-cache-redis/src/main/resources/templates/bookForm.html b/chapter-6-spring-boot-cache-redis/src/main/resources/templates/bookForm.html new file mode 100644 index 0000000..43118b6 --- /dev/null +++ b/chapter-6-spring-boot-cache-redis/src/main/resources/templates/bookForm.html @@ -0,0 +1,58 @@ + + + + + + + + + 书籍管理 + + + +
+ +
《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 5 章《数据存储》Demo
+ + + 书籍管理 + + +
+ + + +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+    + +
+
+
+
+ + \ No newline at end of file diff --git a/chapter-6-spring-boot-cache-redis/src/main/resources/templates/bookList.html b/chapter-6-spring-boot-cache-redis/src/main/resources/templates/bookList.html new file mode 100644 index 0000000..2ab11c6 --- /dev/null +++ b/chapter-6-spring-boot-cache-redis/src/main/resources/templates/bookList.html @@ -0,0 +1,46 @@ + + + + + + + + + 书籍列表 + + + + +
+ +
《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 5 章《数据存储》Demo
+ + + + 书籍列表 + + + + + + + + + + + + + + + + + + + +
书籍编号书名作者简介管理
删除
+ + +
+ + + \ No newline at end of file diff --git a/chapter-6-spring-boot-cache-redis/src/test/java/demo/springboot/WebApplicationTests.java b/chapter-6-spring-boot-cache-redis/src/test/java/demo/springboot/WebApplicationTests.java new file mode 100644 index 0000000..ebd0f53 --- /dev/null +++ b/chapter-6-spring-boot-cache-redis/src/test/java/demo/springboot/WebApplicationTests.java @@ -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() { + } + +} diff --git a/pom.xml b/pom.xml index b39290c..01b6e17 100644 --- a/pom.xml +++ b/pom.xml @@ -9,6 +9,14 @@ pom + + + + springboot-webflux-1-quickstart + + + springboot-webflux-2-restful + springboot-helloworld @@ -54,6 +62,19 @@ spring-data-elasticsearch-crud spring-data-elasticsearch-query + + chapter-1-spring-boot-quickstart + + chapter-2-spring-boot-config + + chapter-3-spring-boot-web + + chapter-4-spring-boot-web-thymeleaf + + chapter-5-spring-boot-data-jpa + + chapter-6-spring-boot-cache-redis + chapter-4-spring-boot-validating-form-input diff --git a/springboot-webflux/pom.xml b/springboot-webflux/pom.xml deleted file mode 100755 index ed47c0e..0000000 --- a/springboot-webflux/pom.xml +++ /dev/null @@ -1,59 +0,0 @@ - - - 4.0.0 - - springboot - springboot-webflux - 0.0.1-SNAPSHOT - springboot-webflux :: Spring Boot 实现 WebFlux HTTP Restful 服务 - - - - org.springframework.boot - spring-boot-starter-parent - 2.0.0.M3 - - - - 1.2.0 - 5.1.39 - - - - - - - org.springframework.boot - spring-boot-starter-webflux - - - - - org.springframework.boot - spring-boot-starter-test - test - - - - - junit - junit - 4.12 - - - - - - - - spring-milestones - Spring Milestones - https://repo.spring.io/libs-milestone - - false - - - - - diff --git a/springboot-webflux/src/main/java/org/spring/springboot/Application.java b/springboot-webflux/src/main/java/org/spring/springboot/Application.java deleted file mode 100644 index d648b6b..0000000 --- a/springboot-webflux/src/main/java/org/spring/springboot/Application.java +++ /dev/null @@ -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); - } -} diff --git a/springboot-webflux/src/main/java/org/spring/springboot/controller/CityRestController.java b/springboot-webflux/src/main/java/org/spring/springboot/controller/CityRestController.java deleted file mode 100644 index ff495b6..0000000 --- a/springboot-webflux/src/main/java/org/spring/springboot/controller/CityRestController.java +++ /dev/null @@ -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 服务 - *

- * 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 findOneCity(@PathVariable("id") Long id) { - return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.findCityById(id))); - } - - @RequestMapping(method = RequestMethod.GET) - public Flux findAllCity() { - return Flux.create(cityFluxSink -> { - cityService.findAllCity().forEach(city -> { - cityFluxSink.next(city); - }); - cityFluxSink.complete(); - }); - } - - @RequestMapping(method = RequestMethod.POST) - public Mono createCity(@RequestBody City city) { - return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.saveCity(city))); - } - - @RequestMapping(method = RequestMethod.PUT) - public Mono modifyCity(@RequestBody City city) { - return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.updateCity(city))); - } - - @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) - public Mono modifyCity(@PathVariable("id") Long id) { - return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.deleteCity(id))); - } -} diff --git a/springboot-webflux/src/main/java/org/spring/springboot/domain/City.java b/springboot-webflux/src/main/java/org/spring/springboot/domain/City.java deleted file mode 100644 index 598af6d..0000000 --- a/springboot-webflux/src/main/java/org/spring/springboot/domain/City.java +++ /dev/null @@ -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; - } -} diff --git a/springboot-webflux/src/main/java/org/spring/springboot/service/CityService.java b/springboot-webflux/src/main/java/org/spring/springboot/service/CityService.java deleted file mode 100644 index 441ffbb..0000000 --- a/springboot-webflux/src/main/java/org/spring/springboot/service/CityService.java +++ /dev/null @@ -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 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); -} diff --git a/springboot-webflux/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java b/springboot-webflux/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java deleted file mode 100644 index 1a628a6..0000000 --- a/springboot-webflux/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java +++ /dev/null @@ -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 CITY_DB = new HashMap<>(); - - public List 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; - } - -}