├── .gitignore
├── README.md
├── book-source-code
└── xiandafu-Spring-Boot-2.0-Samples-master.zip
├── ch01-hello
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── zccoder
│ └── boot2
│ └── ch1
│ └── hello
│ ├── HelloStart.java
│ ├── controller
│ └── HelloController.java
│ └── rest
│ └── UserCreditRestController.java
├── ch02-aop
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── zccoder
│ └── boot2
│ └── ch2
│ └── aop
│ ├── AopStart.java
│ ├── config
│ └── AopConfig.java
│ └── controller
│ └── HelloController.java
├── ch03-mvc
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── zccoder
│ │ └── boot2
│ │ └── ch3
│ │ └── mvc
│ │ ├── MvcStart.java
│ │ ├── conf
│ │ ├── ErrorController.java
│ │ ├── JacksonConfig.java
│ │ └── MvcConfigurer.java
│ │ ├── controller
│ │ ├── BeetlController.java
│ │ ├── DataBindingController.java
│ │ ├── FileUploadController.java
│ │ ├── FreemakerController.java
│ │ ├── JacksonSampleController.java
│ │ ├── JavaBeanController.java
│ │ ├── JsonController.java
│ │ ├── ModelAndViewController.java
│ │ ├── ModelAttributeController.java
│ │ ├── Sample34Controller.java
│ │ ├── UrlMapperController.java
│ │ ├── form
│ │ │ ├── OrderPostForm.java
│ │ │ └── WorkInfoForm.java
│ │ └── validate
│ │ │ ├── WorkOverTime.java
│ │ │ └── WorkOverTimeValidator.java
│ │ ├── entity
│ │ ├── Order.java
│ │ ├── OrderDetail.java
│ │ └── User.java
│ │ └── service
│ │ ├── UserService.java
│ │ └── impl
│ │ └── UserServiceImpl.java
│ └── resources
│ ├── application.properties
│ ├── static
│ ├── addOrder.html
│ └── css
│ │ └── ztree.css
│ └── templates
│ ├── error.btl
│ ├── index.btl
│ ├── userInfo.btl
│ └── userInfo.ftl
├── ch04-view
├── ch04-view-beetl
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── zccoder
│ │ │ └── boot2
│ │ │ └── ch4
│ │ │ └── view
│ │ │ └── beetl
│ │ │ ├── ViewBeetlStart.java
│ │ │ ├── conf
│ │ │ └── BeetlExtConfig.java
│ │ │ ├── controller
│ │ │ ├── IndexController.java
│ │ │ └── SimulateController.java
│ │ │ └── util
│ │ │ ├── SimpleFunction.java
│ │ │ └── SimpleTag.java
│ │ └── resources
│ │ ├── application.properties
│ │ ├── beetl.properties
│ │ ├── static
│ │ ├── css
│ │ │ ├── bootstrap-theme.css
│ │ │ ├── bootstrap-theme.css.map
│ │ │ ├── bootstrap-theme.min.css
│ │ │ ├── bootstrap-theme.min.css.map
│ │ │ ├── bootstrap.css
│ │ │ ├── bootstrap.css.map
│ │ │ ├── bootstrap.min.css
│ │ │ └── bootstrap.min.css.map
│ │ ├── fonts
│ │ │ ├── glyphicons-halflings-regular.eot
│ │ │ ├── glyphicons-halflings-regular.svg
│ │ │ ├── glyphicons-halflings-regular.ttf
│ │ │ ├── glyphicons-halflings-regular.woff
│ │ │ └── glyphicons-halflings-regular.woff2
│ │ └── js
│ │ │ ├── bootstrap.js
│ │ │ ├── bootstrap.min.js
│ │ │ └── npm.js
│ │ └── templates
│ │ ├── common
│ │ └── layout.btl
│ │ ├── functions
│ │ └── test.html
│ │ ├── index.btl
│ │ ├── test.html
│ │ ├── user
│ │ └── userList.btl
│ │ └── values
│ │ ├── api
│ │ └── user
│ │ │ └── 1.json.var
│ │ └── user
│ │ └── userlist.html.var
├── ch04-view-json
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── java
│ │ └── com
│ │ └── zccoder
│ │ └── boot2
│ │ └── ch4
│ │ └── view
│ │ └── json
│ │ ├── ViewJsonStart.java
│ │ ├── conf
│ │ └── JacksonConf.java
│ │ ├── controller
│ │ ├── DataBindController.java
│ │ ├── JacksonStreamController.java
│ │ └── TreeModelController.java
│ │ └── entity
│ │ └── User.java
└── pom.xml
├── ch05-data
├── ch05-data-beetlsql
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── zccoder
│ │ │ └── boot2
│ │ │ └── ch5
│ │ │ └── data
│ │ │ └── beetlsql
│ │ │ ├── BeetlSqlTest.java
│ │ │ ├── DataBeetlsqlStart.java
│ │ │ ├── conf
│ │ │ └── DataSourceConfig.java
│ │ │ ├── controller
│ │ │ └── UserController.java
│ │ │ ├── dao
│ │ │ └── UserDao.java
│ │ │ ├── entity
│ │ │ └── User.java
│ │ │ └── service
│ │ │ ├── UserService.java
│ │ │ └── impl
│ │ │ └── UserServiceImpl.java
│ │ └── resources
│ │ ├── application.properties
│ │ ├── db.sql
│ │ └── sql
│ │ └── user.md
├── ch05-data-template
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── zccoder
│ │ │ └── boot2
│ │ │ └── ch5
│ │ │ └── data
│ │ │ └── template
│ │ │ ├── DataTemplateStart.java
│ │ │ ├── config
│ │ │ └── DataSourceConfig.java
│ │ │ ├── controller
│ │ │ └── UserController.java
│ │ │ ├── dao
│ │ │ └── UserDao.java
│ │ │ ├── entity
│ │ │ └── User.java
│ │ │ └── service
│ │ │ ├── UserService.java
│ │ │ └── impl
│ │ │ └── UserServiceImpl.java
│ │ └── resources
│ │ ├── application.properties
│ │ └── db.sql
└── pom.xml
├── ch06-jpa
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── zccoder
│ │ └── boot2
│ │ └── ch6
│ │ └── jpa
│ │ ├── JpaStart.java
│ │ ├── config
│ │ └── DataSourceConfig.java
│ │ ├── controller
│ │ └── UserController.java
│ │ ├── entity
│ │ ├── Department.java
│ │ └── User.java
│ │ ├── repository
│ │ └── UserRepository.java
│ │ └── service
│ │ ├── UserService.java
│ │ └── impl
│ │ └── UserServiceImpl.java
│ └── resources
│ ├── application.properties
│ └── db.sql
├── ch07-config
├── .gitignore
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── zccoder
│ │ └── boot2
│ │ └── ch7
│ │ └── config
│ │ ├── ConfigStart.java
│ │ ├── config
│ │ ├── EncryptCondition.java
│ │ ├── EnvConfig.java
│ │ ├── MobileEncryptBean.java
│ │ ├── MobileEncryptCondition.java
│ │ ├── MyConfiguration.java
│ │ ├── ServerConfig.java
│ │ └── UrlTestBean.java
│ │ └── controller
│ │ └── PropertyController.java
│ └── resources
│ ├── application.properties
│ ├── banner.txt
│ └── static
│ ├── images
│ └── apple.ico
│ └── index.html
├── ch08-deploy
├── .gitignore
├── env
│ ├── application-test.properties
│ └── application.properties
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── zccoder
│ │ └── boot2
│ │ └── ch8
│ │ └── deploy
│ │ ├── DeployStart.java
│ │ ├── conf
│ │ └── DataSourceConf.java
│ │ └── controller
│ │ └── HelloWorldController.java
│ └── resources
│ ├── application-prod.properties
│ └── application.properties
├── ch09-test
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── zccoder
│ │ │ └── boot2
│ │ │ └── ch9
│ │ │ └── test
│ │ │ ├── TestApplication.java
│ │ │ ├── config
│ │ │ └── DataSourceConfig.java
│ │ │ ├── controller
│ │ │ └── UserController.java
│ │ │ ├── dao
│ │ │ └── UserDao.java
│ │ │ ├── entity
│ │ │ └── User.java
│ │ │ └── service
│ │ │ ├── CreditSystemService.java
│ │ │ ├── UserService.java
│ │ │ └── impl
│ │ │ ├── CreditSystemServiceImpl.java
│ │ │ └── UserServiceImpl.java
│ └── resources
│ │ ├── application.properties
│ │ └── banner.txt
│ └── test
│ ├── java
│ └── com
│ │ └── zccoder
│ │ └── boot2
│ │ └── ch9
│ │ └── test
│ │ ├── controller
│ │ └── UserControllerTest.java
│ │ ├── db
│ │ ├── UserDbTest.java
│ │ └── user.sql
│ │ ├── mock
│ │ └── CreditServiceMockTest.java
│ │ └── service
│ │ └── UserServiceTest.java
│ └── resources
│ └── application-test.properties
├── ch10-rest
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── zccoder
│ │ └── boot2
│ │ └── ch10
│ │ └── rest
│ │ ├── RestApplication.java
│ │ ├── conf
│ │ └── RestConf.java
│ │ ├── controller
│ │ ├── OrderApi2Controller.java
│ │ ├── OrderApiController.java
│ │ └── RestClientTestController.java
│ │ └── entity
│ │ └── Order.java
│ └── resources
│ ├── application.properties
│ ├── static
│ └── swagger3
│ │ ├── favicon-16x16.png
│ │ ├── favicon-32x32.png
│ │ ├── index.html
│ │ ├── oauth2-redirect.html
│ │ ├── sample.json
│ │ ├── sample2.json
│ │ ├── swagger-ui-bundle.js
│ │ ├── swagger-ui-bundle.js.map
│ │ ├── swagger-ui-standalone-preset.js
│ │ ├── swagger-ui-standalone-preset.js.map
│ │ ├── swagger-ui.css
│ │ ├── swagger-ui.css.map
│ │ ├── swagger-ui.js
│ │ └── swagger-ui.js.map
│ └── templates
│ └── values
│ └── api
│ └── v2
│ └── order
│ └── $$.var
├── ch11-mongodb
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── zccoder
│ │ └── boot2
│ │ └── ch11
│ │ └── mongodb
│ │ ├── MongodbApplication.java
│ │ ├── controller
│ │ ├── DriverTestController.java
│ │ └── MongoController.java
│ │ └── entity
│ │ ├── Baike.java
│ │ └── Comment.java
│ └── resources
│ ├── application.properties
│ └── xxx.btl
├── ch12-redis
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── zccoder
│ │ └── boot2
│ │ └── ch12
│ │ └── redis
│ │ ├── RedisApplication.java
│ │ ├── conf
│ │ ├── RedisChannelListenerConf.java
│ │ └── RedisConfig.java
│ │ └── controller
│ │ ├── RedisTemplateController.java
│ │ ├── StrKeyRedisTemplateController.java
│ │ └── StringRedisTemplateController.java
│ └── resources
│ └── application.properties
├── ch13-elasticsearch
├── README.md
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── zccoder
│ │ └── boot2
│ │ └── ch13
│ │ └── elasticsearch
│ │ ├── ElasticsearchApplication.java
│ │ ├── dao
│ │ └── BookDao.java
│ │ ├── entity
│ │ ├── Book.java
│ │ └── BookEntity.java
│ │ └── rest
│ │ ├── ElasticDataController.java
│ │ └── RestClientController.java
│ └── resources
│ └── application.properties
├── ch14-cache
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── zccoder
│ │ └── boot2
│ │ └── ch14
│ │ └── cache
│ │ ├── CacheApplication.java
│ │ ├── config
│ │ ├── CacheConfig.java
│ │ └── RedisCacheManagerCustomizer.java
│ │ ├── controller
│ │ └── AdminController.java
│ │ ├── entity
│ │ └── Menu.java
│ │ ├── pojo
│ │ └── MenuNode.java
│ │ └── service
│ │ ├── MenuService.java
│ │ └── impl
│ │ └── MenuServiceImpl.java
│ └── resources
│ └── application.properties
├── ch15-session
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── zccoder
│ │ └── boot2
│ │ └── ch15
│ │ └── session
│ │ ├── SessionApplication.java
│ │ └── controller
│ │ └── SpringSessionController.java
│ └── resources
│ └── application.properties
├── ch16-zookeeper
├── pom.xml
├── readme.md
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── zccoder
│ │ └── boot2
│ │ └── ch16
│ │ └── zookeeper
│ │ ├── ZookeeperApplication.java
│ │ ├── config
│ │ └── ZookeeperConfig.java
│ │ ├── controller
│ │ └── ZookeeperTestController.java
│ │ └── service
│ │ ├── OrderService.java
│ │ └── impl
│ │ └── OrderServiceImpl.java
│ └── resources
│ └── application.properties
├── ch17-monitor
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── zccoder
│ │ └── boot2
│ │ └── ch17
│ │ └── monitor
│ │ ├── MonitorApplication.java
│ │ ├── config
│ │ ├── AcutatorExtConfig.java
│ │ ├── DataSourceConfig.java
│ │ ├── HikariCpEndpoint.java
│ │ └── MessageCenterHealthIndicator.java
│ │ └── controller
│ │ ├── OutOfMemoryController.java
│ │ └── TestController.java
│ └── resources
│ └── application.properties
└── pom.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | # Created by .ignore support plugin (hsz.mobi)
2 | ### Example user template template
3 | ### Example user template
4 |
5 | # IntelliJ project files
6 | .idea
7 | *.iml
8 | out
9 | gen
10 |
11 | # Java compile files
12 | target
13 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 简介
2 |
3 | 《Spring Boot 2精髓:从构建小系统到架构分布式大系统》学习项目源码
4 |
5 | # 目录
6 |
7 | ### 2018-2018030801:01-ch01-hello
8 | 第一章《Java EE 简介》学习源码
9 |
10 | Hello Spring Boot 示例、使用热部署、添加REST支持
11 |
12 |
13 | ### 2018-2018030802:02-ch02-aop
14 | 第二章《Spring Boot 基础》学习源码
15 |
16 | 在 Spring Boot 中使用 AOP
17 |
18 |
19 | ### 2018-2018030901:03-ch03-mvc
20 | 第三章《MVC 框架》学习源码
21 |
22 | URL 映射、文件上传、JSON 解析、JSR303 验证框架、跨域访问、通用错误处理
23 |
24 |
25 | ### 2018-2018031401:04-ch04-view
26 | 第四章《视图技术》学习源码
27 |
28 | Beetl 模版引擎、JSON 技术
29 |
30 |
31 | ### 2018-2018062501:05-ch05-data
32 | 第五章《数据库访问》学习源码
33 |
34 | Spring JDBC Template、Beetl SQL
35 |
36 |
37 | ### 2018-2018062601:06-ch06-jpa
38 | 第六章《Spring Data JPA》学习源码
39 |
40 | Spring Data JPA
41 |
42 |
43 | ### 2018-2018092101:07-ch07-config
44 | 第七章《Spring Boot 配置》学习源码
45 |
46 | Spring Boot 自动配置
47 |
48 |
49 | ### 2018-2018092102:08-ch08-deploy
50 | 第八章《部署 Spring Boot 应用》学习源码
51 |
52 | jar包运行、war包部署、多环境部署、@Profile注解
53 |
54 |
55 | ### 2018-2018092501:09-ch09-test
56 | 第九章《Testing 单元测试》学习源码
57 |
58 | Spring Boot 单元测试、Mockito、面向数据库应用的单元测试
59 |
60 |
61 | ### 2018-2018092601:10-ch10-rest
62 | 第十章《Rest》学习源码
63 |
64 | Spring Boot 集成 Rest
65 |
66 |
67 | ### 2018-2018092801:11-ch11-mongodb
68 | 第十一章《MongoDB》学习源码
69 |
70 | Spring Boot 集成 MongoDB
71 |
72 |
73 | ### 2018-2018092802:12-ch12-redis
74 | 第十二章《Redis》学习源码
75 |
76 | Spring Boot 集成 Redis
77 |
78 |
79 | ### 2018-2018092803:13-ch13-elasticsearch
80 | 第十三章《Elasticsearch》学习源码
81 |
82 | Spring Boot 集成 Elasticsearch
83 |
84 |
85 | ### 2018-2018092901:14-ch14-cache
86 | 第十四章《Cache》学习源码
87 |
88 | 使用Cache、使用Redis Cache
89 |
90 |
91 | ### 2018-2018092902:15-ch15-session
92 | 第十五章《Spring Session》学习源码
93 |
94 | 使用Spring Session
95 |
96 |
97 | ### 2018-2018092903:16-ch16-zookeeper
98 | 第十六章《Spring Boot 与 Zookeeper》学习源码
99 |
100 | 实现分布式锁、服务注册与服务发现
101 |
102 |
103 | ### 2018-2018093001:17-ch17-monitor
104 | 第十七章《监控Spring Boot 应用》学习源码
105 |
106 | 使用 Acutator、自定义监控
107 |
108 |
--------------------------------------------------------------------------------
/book-source-code/xiandafu-Spring-Boot-2.0-Samples-master.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zccodere/study-boot2/2118ce215c52abc1991aa72747f907206d319970/book-source-code/xiandafu-Spring-Boot-2.0-Samples-master.zip
--------------------------------------------------------------------------------
/ch01-hello/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 |
7 | study-boot2
8 | com.zccoder
9 | 1.0.0-SNAPSHOT
10 |
11 |
12 | ch01-hello
13 |
14 |
15 | 2.0.0.RELEASE
16 |
17 |
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-parent
22 | ${spring.boot.version}
23 | pom
24 | import
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-web
29 | ${spring.boot.version}
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-devtools
34 | ${spring.boot.version}
35 | true
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/ch01-hello/src/main/java/com/zccoder/boot2/ch1/hello/HelloStart.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch1.hello;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | *
8 | * 标题: 启动类
9 | * 描述: Hello Spring Boot 示例
10 | *
11 | * @author zc
12 | * @date 2018/03/08
13 | */
14 | @SpringBootApplication
15 | public class HelloStart {
16 |
17 | public static void main(String[] args) {
18 | SpringApplication.run(HelloStart.class, args);
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/ch01-hello/src/main/java/com/zccoder/boot2/ch1/hello/controller/HelloController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch1.hello.controller;
2 |
3 | import org.springframework.stereotype.Controller;
4 | import org.springframework.web.bind.annotation.RequestMapping;
5 | import org.springframework.web.bind.annotation.ResponseBody;
6 |
7 | /**
8 | *
9 | * 标题: Hello控制器
10 | * 描述: 简单演示
11 | *
12 | * @author zc
13 | * @date 2018/03/08
14 | */
15 | @Controller
16 | public class HelloController {
17 |
18 | @RequestMapping("/say.html")
19 | @ResponseBody
20 | public String say() {
21 | return "Hello Spring Boot";
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/ch01-hello/src/main/java/com/zccoder/boot2/ch1/hello/rest/UserCreditRestController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch1.hello.rest;
2 |
3 | import org.springframework.web.bind.annotation.PathVariable;
4 | import org.springframework.web.bind.annotation.RequestMapping;
5 | import org.springframework.web.bind.annotation.RestController;
6 |
7 | /**
8 | *
9 | * 标题: RESTFul服务
10 | * 描述: 用户信用等级服务
11 | *
12 | * @author zc
13 | * @date 2018/03/08
14 | **/
15 | @RestController
16 | public class UserCreditRestController {
17 |
18 | private static final int TEST_NUM = 3;
19 |
20 | @RequestMapping("/usercredit/{id}")
21 | public Integer getCreditLevel(@PathVariable Integer id) {
22 | // 模拟id用户的信用等级
23 | return id + TEST_NUM;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/ch02-aop/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 |
7 | study-boot2
8 | com.zccoder
9 | 1.0.0-SNAPSHOT
10 |
11 |
12 | ch02-aop
13 |
14 |
15 | 2.0.0.RELEASE
16 |
17 |
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-parent
22 | ${spring.boot.version}
23 | pom
24 | import
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-web
29 | ${spring.boot.version}
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-starter-aop
34 | ${spring.boot.version}
35 |
36 |
37 | org.springframework.boot
38 | spring-boot-devtools
39 | ${spring.boot.version}
40 | true
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/ch02-aop/src/main/java/com/zccoder/boot2/ch2/aop/AopStart.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch2.aop;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | *
8 | * 标题: 启动类
9 | * 描述: 在 Spring Boot 中使用 AOP
10 | *
11 | * @author zc
12 | * @date 2018/03/08
13 | */
14 | @SpringBootApplication
15 | public class AopStart {
16 |
17 | public static void main(String[] args) {
18 | SpringApplication.run(AopStart.class, args);
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/ch02-aop/src/main/java/com/zccoder/boot2/ch2/aop/config/AopConfig.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch2.aop.config;
2 |
3 | import org.aspectj.lang.ProceedingJoinPoint;
4 | import org.aspectj.lang.annotation.Around;
5 | import org.aspectj.lang.annotation.Aspect;
6 | import org.springframework.context.annotation.Configuration;
7 |
8 | import java.util.Arrays;
9 |
10 | /**
11 | *
12 | * 标题: AOP 切面类
13 | * 描述: 在调用目标方法前后输出日志
14 | *
15 | * @author zc
16 | * @date 2018/03/08
17 | */
18 | @Configuration
19 | @Aspect
20 | public class AopConfig {
21 |
22 | @Around("@within(org.springframework.stereotype.Controller) ")
23 | public Object around(ProceedingJoinPoint pjp) throws Throwable {
24 |
25 | try {
26 | // 前置通知
27 | Object[] args = pjp.getArgs();
28 | System.out.println("args: " + Arrays.asList(args));
29 |
30 | // 调用目标方法
31 | Object obj = pjp.proceed();
32 |
33 | // 后置通知
34 | System.out.println("retuen: " + obj);
35 |
36 | return obj;
37 | } catch (Throwable e) {
38 | // 异常通知
39 | throw e;
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/ch02-aop/src/main/java/com/zccoder/boot2/ch2/aop/controller/HelloController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch2.aop.controller;
2 |
3 | import org.springframework.stereotype.Controller;
4 | import org.springframework.web.bind.annotation.PathVariable;
5 | import org.springframework.web.bind.annotation.RequestMapping;
6 | import org.springframework.web.bind.annotation.ResponseBody;
7 |
8 | /**
9 | *
10 | * 标题: Hello控制器
11 | * 描述: 简单演示
12 | *
13 | * @author zc
14 | * @date 2018/03/08
15 | **/
16 | @Controller
17 | public class HelloController {
18 |
19 | @RequestMapping("/say.html")
20 | @ResponseBody
21 | public String say(String name) {
22 | return "Hello Spring Boot";
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/ch03-mvc/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 |
7 | study-boot2
8 | com.zccoder
9 | 1.0.0-SNAPSHOT
10 |
11 |
12 | ch03-mvc
13 |
14 |
15 | 2.0.0.RELEASE
16 |
17 |
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-parent
22 | ${spring.boot.version}
23 | pom
24 | import
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-web
29 | ${spring.boot.version}
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-starter-freemarker
34 | ${spring.boot.version}
35 |
36 |
37 | com.ibeetl
38 | beetl-framework-starter
39 | 1.1.15.RELEASE
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/MvcStart.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | *
8 | * 标题: 启动类
9 | * 描述: MVC 框架
10 | *
11 | * @author zc
12 | * @date 2018/03/09
13 | **/
14 | @SpringBootApplication
15 | public class MvcStart {
16 | public static void main(String[] args) {
17 | SpringApplication.run(MvcStart.class, args);
18 |
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/conf/JacksonConfig.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.conf;
2 |
3 | import java.text.SimpleDateFormat;
4 |
5 | import org.springframework.context.annotation.Bean;
6 | import org.springframework.context.annotation.Configuration;
7 | import org.springframework.context.annotation.Primary;
8 |
9 | import com.fasterxml.jackson.databind.ObjectMapper;
10 |
11 | /**
12 | *
13 | * 标题: Jackson配置
14 | * 描述: 默认使用Jackson实现JSON的序列化和反序列化
15 | *
16 | * @author zc
17 | * @date 2018/03/09
18 | **/
19 | @Configuration
20 | public class JacksonConfig {
21 |
22 | @Bean
23 | public ObjectMapper getObjectMapper() {
24 | ObjectMapper objectMapper = new ObjectMapper();
25 | objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
26 | return objectMapper;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/controller/BeetlController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.controller;
2 |
3 | import com.zccoder.boot2.ch3.mvc.entity.User;
4 | import com.zccoder.boot2.ch3.mvc.service.UserService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Controller;
7 | import org.springframework.ui.Model;
8 | import org.springframework.web.bind.annotation.GetMapping;
9 | import org.springframework.web.bind.annotation.RequestMapping;
10 | import org.springframework.web.servlet.ModelAndView;
11 |
12 | /**
13 | *
14 | * 标题: Beetl模版引擎
15 | * 描述: 测试使用模板引擎Beetl
16 | *
17 | * @author zc
18 | * @date 2018/03/09
19 | **/
20 | @Controller
21 | @RequestMapping("/beetl")
22 | public class BeetlController {
23 | @Autowired
24 | private UserService userService;
25 |
26 | @RequestMapping("/index.html")
27 | public String say(Model model) {
28 | model.addAttribute("name", "hello,world");
29 | return "/index.btl";
30 | }
31 |
32 | @GetMapping("/showuser.html")
33 | public ModelAndView showUserInfo(Long id) {
34 | ModelAndView view = new ModelAndView();
35 | User user = userService.getUserById(id);
36 | view.addObject("user", user);
37 | view.setViewName("/userInfo.btl");
38 | return view;
39 | }
40 |
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/controller/FileUploadController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.controller;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 |
6 | import org.springframework.stereotype.Controller;
7 | import org.springframework.web.bind.annotation.PostMapping;
8 | import org.springframework.web.bind.annotation.RequestMapping;
9 | import org.springframework.web.bind.annotation.RequestParam;
10 | import org.springframework.web.bind.annotation.ResponseBody;
11 | import org.springframework.web.multipart.MultipartFile;
12 |
13 | /**
14 | *
15 | * 标题: 文件上传控制器
16 | * 描述: 处理文件上传
17 | *
18 | * @author zc
19 | * @date 2018/03/09
20 | **/
21 | @Controller
22 | @RequestMapping("/upload")
23 | public class FileUploadController {
24 |
25 | @PostMapping("/form")
26 | @ResponseBody
27 | public String handleFormUpload(@RequestParam("name") String name,
28 | @RequestParam("file") MultipartFile file) throws IOException {
29 | if (!file.isEmpty()) {
30 | String fileName = file.getOriginalFilename();
31 | InputStream ins = file.getInputStream();
32 |
33 | // 处理上传内容
34 | return "success";
35 | }
36 | return "failure";
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/controller/FreemakerController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.controller;
2 |
3 | import com.zccoder.boot2.ch3.mvc.entity.User;
4 | import com.zccoder.boot2.ch3.mvc.service.UserService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Controller;
7 | import org.springframework.web.bind.annotation.GetMapping;
8 | import org.springframework.web.bind.annotation.RequestMapping;
9 | import org.springframework.web.servlet.ModelAndView;
10 |
11 | /**
12 | *
13 | * 标题: Freemaker控制器
14 | * 描述: 使用Freemaker
15 | *
16 | * @author zc
17 | * @date 2018/03/09
18 | **/
19 | @Controller
20 | @RequestMapping("/freemarker")
21 | public class FreemakerController {
22 |
23 | @Autowired
24 | private UserService userService;
25 |
26 | /***
27 | * http://127.0.0.1:8080/freemarker//showuser.html?id=1
28 | * @param id
29 | * @return
30 | */
31 | @GetMapping("/showuser.html")
32 | public ModelAndView showUserInfo(Long id) {
33 | ModelAndView view = new ModelAndView();
34 | User user = userService.getUserById(id);
35 | view.addObject("user", user);
36 | view.setViewName("/userInfo");
37 | return view;
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/controller/JavaBeanController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.controller;
2 |
3 | import com.zccoder.boot2.ch3.mvc.controller.form.OrderPostForm;
4 | import com.zccoder.boot2.ch3.mvc.entity.User;
5 | import org.springframework.stereotype.Controller;
6 | import org.springframework.web.bind.annotation.GetMapping;
7 | import org.springframework.web.bind.annotation.PostMapping;
8 | import org.springframework.web.bind.annotation.RequestBody;
9 | import org.springframework.web.bind.annotation.RequestMapping;
10 | import org.springframework.web.bind.annotation.RequestParam;
11 | import org.springframework.web.bind.annotation.ResponseBody;
12 |
13 | /**
14 | *
15 | * 标题: JavaBean接受HTTP参数
16 | * 描述: 通过form提交的各种映射举例
17 | *
18 | * @author zc
19 | * @date 2018/03/09
20 | **/
21 | @Controller
22 | @RequestMapping("/javabean")
23 | public class JavaBeanController {
24 |
25 | @GetMapping(path = "/update.json")
26 | @ResponseBody
27 | public String updateUser(User user) {
28 | System.out.println(user.getName());
29 | System.out.println(user.getId());
30 | return "success";
31 | }
32 |
33 | @GetMapping(path = "/update2.json")
34 | @ResponseBody
35 | public String updateUser2(Integer id, String name) {
36 | System.out.println(id);
37 | System.out.println(name);
38 | return "success";
39 | }
40 |
41 |
42 | @GetMapping(path = "/update3.json")
43 | @ResponseBody
44 | public String updateUser3(@RequestParam(name = "id", required = true) Integer id, String name) {
45 | System.out.println(id);
46 | System.out.println(name);
47 | return "success";
48 | }
49 |
50 | @PostMapping(path = "/saveOrder.json")
51 | @ResponseBody
52 | public String saveOrder(OrderPostForm form) {
53 | return "success";
54 | }
55 |
56 | @PostMapping(path = "/savejsonorder.json")
57 | @ResponseBody
58 | public String saveOrderByJson(@RequestBody User user) {
59 | return user.getName();
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/controller/JsonController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.controller;
2 |
3 | import java.util.Date;
4 | import java.util.HashMap;
5 | import java.util.Map;
6 |
7 | import com.zccoder.boot2.ch3.mvc.entity.User;
8 | import com.zccoder.boot2.ch3.mvc.service.UserService;
9 | import org.springframework.beans.factory.annotation.Autowired;
10 | import org.springframework.stereotype.Controller;
11 | import org.springframework.web.bind.annotation.GetMapping;
12 | import org.springframework.web.bind.annotation.PathVariable;
13 | import org.springframework.web.bind.annotation.RequestMapping;
14 | import org.springframework.web.bind.annotation.ResponseBody;
15 |
16 | import com.fasterxml.jackson.databind.ObjectMapper;
17 |
18 | /**
19 | *
20 | * 标题: JSON控制器
21 | * 描述: 日期转化需要参考JacksonConfig类
22 | *
23 | * @author zc
24 | * @date 2018/03/09
25 | **/
26 | @Controller
27 | @RequestMapping("/json")
28 | public class JsonController {
29 | @Autowired
30 | UserService userService;
31 |
32 | @GetMapping("/user/{id}.json")
33 | @ResponseBody
34 | public User showUserInfo(@PathVariable Long id) {
35 | User user = userService.getUserById(id);
36 | return user;
37 | }
38 |
39 | @GetMapping("/now.json")
40 | @ResponseBody
41 | public Map datetime() {
42 | //JacksonConfig 配置了序列化日期
43 | Map map = new HashMap(16);
44 | map.put("time", new Date());
45 | return map;
46 | }
47 |
48 | /**
49 | * 比如:json/date.json?date=2017-09-20 21:30:15
50 | *
51 | * @param date
52 | * @return
53 | */
54 | @GetMapping("/date.json")
55 | @ResponseBody
56 | public Map datetime(Date date) {
57 | //MvcConfigurer 配置了处理查询参数到日期类型的映射
58 | Map map = new HashMap(16);
59 | map.put("time", date);
60 | return map;
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/controller/ModelAndViewController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.controller;
2 |
3 | import com.zccoder.boot2.ch3.mvc.entity.User;
4 | import com.zccoder.boot2.ch3.mvc.service.UserService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Controller;
7 | import org.springframework.ui.Model;
8 | import org.springframework.web.bind.annotation.GetMapping;
9 | import org.springframework.web.bind.annotation.PathVariable;
10 | import org.springframework.web.bind.annotation.RequestMapping;
11 | import org.springframework.web.servlet.ModelAndView;
12 |
13 | /**
14 | *
15 | * 标题: ModelAndView 控制器
16 | * 描述: 模型视图
17 | *
18 | * @author zc
19 | * @date 2018/03/09
20 | **/
21 | @Controller
22 | @RequestMapping("/model")
23 | public class ModelAndViewController {
24 |
25 | @Autowired
26 | private UserService userService;
27 |
28 | /**
29 | * 一个beetl模板测试。因为视图扩展名字是btl
30 | *
31 | * @param userId
32 | * @param model
33 | * @return
34 | */
35 | @GetMapping(path = "/{userId}/get.html")
36 | public String getUser(@PathVariable Long userId, Model model) {
37 | User userInfo = userService.getUserById(userId);
38 | //model.addAttribute(userInfo); 与下面一行作用一样,但这会有潜在问题
39 | model.addAttribute("user", userInfo);
40 | return "/userInfo.btl";
41 | }
42 |
43 | /**
44 | * 使用freemaker模板测试,freemaker会寻找/userInfo.ftl 模板
45 | *
46 | * @param userId
47 | * @param view
48 | * @return
49 | */
50 | @GetMapping(path = "/{userId}/get2.html")
51 | public ModelAndView getUser2(@PathVariable Long userId, ModelAndView view) {
52 | User userInfo = userService.getUserById(userId);
53 | view.addObject("user", userInfo);
54 | view.setViewName("/userInfo");
55 | return view;
56 | }
57 |
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/controller/ModelAttributeController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.controller;
2 |
3 | import com.zccoder.boot2.ch3.mvc.service.UserService;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.stereotype.Controller;
6 | import org.springframework.ui.Model;
7 | import org.springframework.web.bind.annotation.GetMapping;
8 | import org.springframework.web.bind.annotation.ModelAttribute;
9 | import org.springframework.web.bind.annotation.PathVariable;
10 | import org.springframework.web.bind.annotation.RequestMapping;
11 | import org.springframework.web.bind.annotation.ResponseBody;
12 |
13 | /**
14 | *
15 | * 标题: // @ModelAttribute 注解
16 | * 描述: 调用方法前会先调用此注解修饰的方法
17 | *
18 | * @author zc
19 | * @date 2018/03/09
20 | **/
21 | @Controller
22 | @RequestMapping("/modelattribute")
23 | public class ModelAttributeController {
24 |
25 | @Autowired
26 | private UserService userService;
27 |
28 | /**
29 | * Controller方法中的公共方法,调用方法前先调用此方法。
30 | *
31 | * @param id
32 | * @param model
33 | */
34 | @ModelAttribute
35 | public void findUserById(@PathVariable Long id, Model model) {
36 | model.addAttribute("user", userService.getUserById(id));
37 | }
38 |
39 | @GetMapping(path = "/{id}/get.json")
40 | @ResponseBody
41 | public String getUser(Model model) {
42 | System.out.println(model.containsAttribute("user"));
43 | return "success";
44 | }
45 |
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/controller/Sample34Controller.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.controller;
2 |
3 | import com.zccoder.boot2.ch3.mvc.entity.User;
4 | import com.zccoder.boot2.ch3.mvc.service.UserService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.http.MediaType;
7 | import org.springframework.stereotype.Controller;
8 | import org.springframework.ui.Model;
9 | import org.springframework.web.bind.annotation.GetMapping;
10 | import org.springframework.web.bind.annotation.PathVariable;
11 | import org.springframework.web.bind.annotation.RequestMapping;
12 | import org.springframework.web.bind.annotation.ResponseBody;
13 |
14 | /**
15 | *
16 | * 标题: url映射到方法
17 | * 描述: url映射到方法
18 | *
19 | * @author zc
20 | * @date 2018/03/09
21 | **/
22 | @Controller
23 | @RequestMapping("/user4")
24 | public class Sample34Controller {
25 |
26 | @Autowired
27 | private UserService userService;
28 |
29 | @GetMapping("/")
30 | @ResponseBody
31 | public String index() {
32 | return "hell";
33 | }
34 |
35 | /**
36 | * 客户端请求必须包含application/json 才会处理
37 | *
38 | * @return
39 | */
40 | @GetMapping(value = "/all1.json", consumes = "application/json")
41 | @ResponseBody
42 | public User forJson() {
43 | return userService.getUserById(1L);
44 | }
45 |
46 | @GetMapping(path = "/user/{userId}.json", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
47 | @ResponseBody
48 | public User getUser(@PathVariable Long userId, Model model) {
49 | return userService.getUserById(userId);
50 | }
51 |
52 |
53 | @GetMapping(path = "/update.json", params = "action=save")
54 | @ResponseBody
55 | public void saveUser() {
56 | System.out.println("call save");
57 | }
58 |
59 | @GetMapping(path = "/update.json", params = "action=update")
60 | @ResponseBody
61 | public void updateUser() {
62 | System.out.println("call update");
63 | }
64 |
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/controller/UrlMapperController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.controller;
2 |
3 | import java.util.List;
4 |
5 | import com.zccoder.boot2.ch3.mvc.entity.User;
6 | import com.zccoder.boot2.ch3.mvc.service.UserService;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.stereotype.Controller;
9 | import org.springframework.web.bind.annotation.GetMapping;
10 | import org.springframework.web.bind.annotation.PathVariable;
11 | import org.springframework.web.bind.annotation.RequestMapping;
12 | import org.springframework.web.bind.annotation.RequestMethod;
13 | import org.springframework.web.bind.annotation.ResponseBody;
14 |
15 | /**
16 | *
17 | * 标题: URL映射
18 | * 描述: mvc url映射测试
19 | *
20 | * @author zc
21 | * @date 2018/03/09
22 | **/
23 | @Controller
24 | @RequestMapping("/urlmapper")
25 | public class UrlMapperController {
26 |
27 | @Autowired
28 | UserService userService;
29 |
30 |
31 | @RequestMapping(path = "/user/all/*.json", method = RequestMethod.GET)
32 | @ResponseBody
33 | public List allUser() {
34 | return userService.allUser();
35 | }
36 |
37 |
38 | @RequestMapping(path = "/user/{id}.json", method = RequestMethod.GET)
39 | @ResponseBody
40 | public User getById(@PathVariable Long id) {
41 | return userService.getUserById(id);
42 | }
43 |
44 | @GetMapping(path = "/{userId}.json", produces = "application/json")
45 | @ResponseBody
46 | public User getUserById(@PathVariable Long userId) {
47 | return userService.getUserById(userId);
48 | }
49 |
50 | @GetMapping(value = "/consumes/test.json", consumes = "application/json")
51 | @ResponseBody
52 | public User forJson() {
53 | return userService.getUserById(1L);
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/controller/form/OrderPostForm.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.controller.form;
2 |
3 | import com.zccoder.boot2.ch3.mvc.entity.Order;
4 | import com.zccoder.boot2.ch3.mvc.entity.OrderDetail;
5 |
6 | import java.util.List;
7 |
8 | /**
9 | *
10 | * 标题: 订单信息类
11 | * 描述: 用户提交订单信息,包含多个订单明细
12 | *
13 | * @author zc
14 | * @date 2018/03/09
15 | **/
16 | public class OrderPostForm {
17 |
18 | /**
19 | * 订单
20 | */
21 | public Order order;
22 |
23 | /**
24 | * 订单明细
25 | */
26 | public List details;
27 |
28 | public Order getOrder() {
29 | return order;
30 | }
31 |
32 | public void setOrder(Order order) {
33 | this.order = order;
34 | }
35 |
36 | public List getDetails() {
37 | return details;
38 | }
39 |
40 | public void setDetails(List details) {
41 | this.details = details;
42 | }
43 |
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/controller/form/WorkInfoForm.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.controller.form;
2 |
3 | import javax.validation.constraints.Email;
4 | import javax.validation.constraints.NotNull;
5 | import javax.validation.constraints.Null;
6 | import javax.validation.constraints.Size;
7 |
8 | import com.zccoder.boot2.ch3.mvc.controller.validate.WorkOverTime;
9 |
10 | /**
11 | *
12 | * 标题: JSR303
13 | * 描述: 包含了验证注解的JavaBean
14 | *
15 | * @author zc
16 | * @date 2018/03/09
17 | **/
18 | public class WorkInfoForm {
19 |
20 | /**
21 | * 定义一个类,更新时校验组
22 | */
23 | public interface Update {
24 | }
25 |
26 | /**
27 | * 定义一个类,添加时校验组
28 | */
29 | public interface Add {
30 | }
31 |
32 | @NotNull(groups = {Update.class})
33 | @Null(groups = {Update.class})
34 | Long id;
35 | @Size(min = 3, max = 20)
36 | String name;
37 | @Email
38 | String email;
39 | @WorkOverTime(max = 5)
40 | Integer workTime;
41 |
42 | public Long getId() {
43 | return id;
44 | }
45 |
46 | public void setId(Long id) {
47 | this.id = id;
48 | }
49 |
50 | public String getName() {
51 | return name;
52 | }
53 |
54 | public void setName(String name) {
55 | this.name = name;
56 | }
57 |
58 | public String getEmail() {
59 | return email;
60 | }
61 |
62 | public void setEmail(String email) {
63 | this.email = email;
64 | }
65 |
66 | public Integer getWorkTime() {
67 | return workTime;
68 | }
69 |
70 | public void setWorkTime(Integer workTime) {
71 | this.workTime = workTime;
72 | }
73 |
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/controller/validate/WorkOverTime.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.controller.validate;
2 |
3 | import java.lang.annotation.Documented;
4 | import java.lang.annotation.ElementType;
5 | import java.lang.annotation.Retention;
6 | import java.lang.annotation.RetentionPolicy;
7 | import java.lang.annotation.Target;
8 |
9 | import javax.validation.Constraint;
10 | import javax.validation.Payload;
11 |
12 | /**
13 | *
14 | * 标题: 自定义校验注解
15 | * 描述: 加班时间校验
16 | *
17 | * @author zc
18 | * @date 2018/03/09
19 | **/
20 | @Constraint(validatedBy = {WorkOverTimeValidator.class})
21 | @Documented
22 | @Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.FIELD})
23 | @Retention(RetentionPolicy.RUNTIME)
24 | public @interface WorkOverTime {
25 |
26 | /**
27 | * 用于创建错误信息
28 | */
29 | String message() default "加班时间过长,不能超过{max}";
30 |
31 | /**
32 | * 加班时间
33 | *
34 | * @return
35 | */
36 | int max() default 4;
37 |
38 | /**
39 | * 验证规则分组
40 | *
41 | * @return
42 | */
43 | Class>[] groups() default {};
44 |
45 | /**
46 | * 验证的有效负荷
47 | *
48 | * @return
49 | */
50 | Class extends Payload>[] payload() default {};
51 | }
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/controller/validate/WorkOverTimeValidator.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.controller.validate;
2 |
3 | import javax.validation.ConstraintValidator;
4 | import javax.validation.ConstraintValidatorContext;
5 |
6 | /**
7 | *
8 | * 标题: 进行验证
9 | * 描述: 实现 ConstraintValidator 接口
10 | *
11 | * @author zc
12 | * @date 2018/03/09
13 | **/
14 | public class WorkOverTimeValidator implements ConstraintValidator {
15 |
16 | WorkOverTime work;
17 |
18 | int max;
19 |
20 | @Override
21 | public void initialize(WorkOverTime work) {
22 | // 获取注解的定义
23 | this.work = work;
24 | max = work.max();
25 | }
26 |
27 | @Override
28 | public boolean isValid(Integer value, ConstraintValidatorContext context) {
29 | // 校验逻辑
30 | if (value == null) {
31 | return true;
32 | }
33 | return value < max;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/entity/Order.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.entity;
2 |
3 | /**
4 | *
5 | * 标题: 订单类
6 | * 描述: 订单信息
7 | *
8 | * @author zc
9 | * @date 2018/03/09
10 | **/
11 | public class Order {
12 |
13 | Long id;
14 |
15 | String name;
16 |
17 | public Long getId() {
18 | return id;
19 | }
20 |
21 | public void setId(Long id) {
22 | this.id = id;
23 | }
24 |
25 | public String getName() {
26 | return name;
27 | }
28 |
29 | public void setName(String name) {
30 | this.name = name;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/entity/OrderDetail.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.entity;
2 |
3 | /**
4 | *
5 | * 标题: 订单明细
6 | * 描述: 订单详情
7 | *
8 | * @author zc
9 | * @date 2018/03/09
10 | **/
11 | public class OrderDetail {
12 |
13 | Long id;
14 |
15 | String name;
16 |
17 | public Long getId() {
18 | return id;
19 | }
20 |
21 | public void setId(Long id) {
22 | this.id = id;
23 | }
24 |
25 | public String getName() {
26 | return name;
27 | }
28 |
29 | public void setName(String name) {
30 | this.name = name;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/entity/User.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.entity;
2 |
3 | /**
4 | *
5 | * 标题: 用户类
6 | * 描述: 用户信息
7 | *
8 | * @author zc
9 | * @date 2018/03/09
10 | **/
11 | public class User {
12 |
13 | Long id;
14 |
15 | String name;
16 |
17 | public Long getId() {
18 | return id;
19 | }
20 |
21 | public void setId(Long id) {
22 | this.id = id;
23 | }
24 |
25 | public String getName() {
26 | return name;
27 | }
28 |
29 | public void setName(String name) {
30 | this.name = name;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/service/UserService.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.service;
2 |
3 | import com.zccoder.boot2.ch3.mvc.entity.User;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | *
9 | * 标题: 用户服务
10 | * 描述: 用户服务类
11 | *
12 | * @author zc
13 | * @date 2018/03/09
14 | **/
15 | public interface UserService {
16 |
17 | /**
18 | * 获取所有用户
19 | *
20 | * @return
21 | */
22 | List allUser();
23 |
24 | /**
25 | * 根据ID查询用户
26 | *
27 | * @param id
28 | * @return
29 | */
30 | User getUserById(Long id);
31 |
32 | /**
33 | * 更新用户状态
34 | *
35 | * @param id
36 | * @param type
37 | */
38 | void updateUser(Long id, Integer type);
39 | }
40 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/java/com/zccoder/boot2/ch3/mvc/service/impl/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch3.mvc.service.impl;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import com.zccoder.boot2.ch3.mvc.entity.User;
7 | import com.zccoder.boot2.ch3.mvc.service.UserService;
8 | import org.springframework.stereotype.Service;
9 |
10 | /**
11 | *
12 | * 标题: 用户服务实现
13 | * 描述: 用户服务类
14 | * //@Transactional 事务配置
15 | *
16 | * @author zc
17 | * @date 2018/03/09
18 | **/
19 | @Service
20 | public class UserServiceImpl implements UserService {
21 |
22 | @Override
23 | public List allUser() {
24 | return sampleUser(5);
25 | }
26 |
27 | @Override
28 | public User getUserById(Long id) {
29 | User user = sampleUser(1).get(0);
30 | user.setId(id);
31 | return user;
32 | }
33 |
34 | private List sampleUser(int num) {
35 | List list = new ArrayList(num);
36 | for (int i = 0; i < num; i++) {
37 | User user = new User();
38 | user.setId((long) i);
39 | user.setName("mame" + i);
40 | list.add(user);
41 | }
42 | return list;
43 | }
44 |
45 | @Override
46 | public void updateUser(Long id, Integer type) {
47 |
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/resources/application.properties:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zccodere/study-boot2/2118ce215c52abc1991aa72747f907206d319970/ch03-mvc/src/main/resources/application.properties
--------------------------------------------------------------------------------
/ch03-mvc/src/main/resources/static/addOrder.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 添加订单
6 |
7 |
8 |
20 |
21 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/resources/static/css/ztree.css:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zccodere/study-boot2/2118ce215c52abc1991aa72747f907206d319970/ch03-mvc/src/main/resources/static/css/ztree.css
--------------------------------------------------------------------------------
/ch03-mvc/src/main/resources/templates/error.btl:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 错误
5 |
6 |
7 | <%
8 | %>
9 | ErrorCode:${status}
10 | Message:${errorMessage}
11 | Message:${message}
12 |
13 | 此错误信息来自 error.btl文件,通过ErrorController.java统一处理
14 |
15 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/resources/templates/index.btl:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Hello,Beetl
6 |
7 |
8 | ${name}
9 |
10 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/resources/templates/userInfo.btl:
--------------------------------------------------------------------------------
1 |
2 |
3 | ${user.id}
4 |
5 | ${user.name}
6 |
7 |
8 |
--------------------------------------------------------------------------------
/ch03-mvc/src/main/resources/templates/userInfo.ftl:
--------------------------------------------------------------------------------
1 |
2 |
3 | ${user.id}
4 |
5 | ${user.name}
6 |
7 |
8 |
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 |
8 | ch04-view
9 | com.zccoder
10 | 1.0.0-SNAPSHOT
11 |
12 |
13 | ch04-view-beetl
14 | jar
15 |
16 |
17 | 2.0.0.RELEASE
18 |
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-parent
25 | ${spring.boot.version}
26 | pom
27 | import
28 |
29 |
30 |
31 |
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-starter-web
36 |
37 |
38 | com.ibeetl
39 | beetl-framework-starter
40 | 1.1.40.RELEASE
41 |
42 |
43 |
44 |
45 |
46 |
47 | org.springframework.boot
48 | spring-boot-maven-plugin
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/java/com/zccoder/boot2/ch4/view/beetl/ViewBeetlStart.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch4.view.beetl;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | *
8 | * 标题: 启动类
9 | * 描述: Beetl 模版引擎
10 | *
11 | * @author zc
12 | * @date 2018/03/14
13 | */
14 | @SpringBootApplication
15 | public class ViewBeetlStart {
16 |
17 | public static void main(String[] args) {
18 | SpringApplication.run(ViewBeetlStart.class, args);
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/java/com/zccoder/boot2/ch4/view/beetl/conf/BeetlExtConfig.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch4.view.beetl.conf;
2 |
3 | import java.net.URL;
4 | import java.util.HashMap;
5 | import java.util.Map;
6 |
7 | import javax.annotation.PostConstruct;
8 |
9 | import org.beetl.core.GroupTemplate;
10 | import org.beetl.core.Tag;
11 | import org.beetl.core.TagFactory;
12 | import org.springframework.beans.factory.annotation.Autowired;
13 | import org.springframework.context.ApplicationContext;
14 | import org.springframework.context.annotation.Configuration;
15 |
16 | import com.zccoder.boot2.ch4.view.beetl.util.SimpleFunction;
17 | import com.zccoder.boot2.ch4.view.beetl.util.SimpleTag;
18 |
19 | /**
20 | *
21 | * 标题: 配置类
22 | * 描述: 配置 GroupTemplate
23 | *
24 | * @author zc
25 | * @date 2018/03/14
26 | */
27 | @Configuration
28 | public class BeetlExtConfig {
29 |
30 | @Autowired
31 | private GroupTemplate groupTemplate;
32 | @Autowired
33 | private ApplicationContext applicationContext;
34 |
35 | @PostConstruct
36 | public void config() {
37 | Map shared = new HashMap(16);
38 | shared.put("jsVersion", System.currentTimeMillis());
39 | groupTemplate.registerFunction("hi", applicationContext.getBean(SimpleFunction.class));
40 | groupTemplate.registerTagFactory("myTag", new TagFactory() {
41 | @Override
42 | public Tag createTag() {
43 | return applicationContext.getBean(SimpleTag.class);
44 | }
45 | });
46 | URL url = BeetlExtConfig.class.getResource("/templates/functions");
47 | System.out.print("==================" + url);
48 | }
49 |
50 | // @Bean /*另外一种配置方法,实现BeetlTemplateCustomize*/
51 | // public BeetlTemplateCustomize beetlTemplateCustomize(){
52 | // return new BeetlTemplateCustomize(){
53 | // public void customize(GroupTemplate groupTemplate){
54 | //
55 | // }
56 | // };
57 | // }
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/java/com/zccoder/boot2/ch4/view/beetl/controller/IndexController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch4.view.beetl.controller;
2 |
3 | import org.springframework.stereotype.Controller;
4 | import org.springframework.web.bind.annotation.RequestMapping;
5 | import org.springframework.web.servlet.ModelAndView;
6 |
7 | /**
8 | *
9 | * 标题: 控制器
10 | * 描述: 配置视图跳转
11 | * 学习beetl语法,可以参考在线体验:http://ibeetl.com/beetlonline/,学习大部分语法
12 | *
13 | * @author zc
14 | * @date 2018/03/14
15 | */
16 | @Controller
17 | public class IndexController {
18 |
19 | @RequestMapping("/index.do")
20 | public ModelAndView index() {
21 | ModelAndView view = new ModelAndView("/index.btl");
22 | view.addObject("name", "lijz");
23 | return view;
24 | }
25 |
26 | @RequestMapping("/test.do")
27 | public ModelAndView test() {
28 | ModelAndView view = new ModelAndView("/test.html");
29 | return view;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/java/com/zccoder/boot2/ch4/view/beetl/controller/SimulateController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch4.view.beetl.controller;
2 |
3 | import javax.servlet.http.HttpServletRequest;
4 | import javax.servlet.http.HttpServletResponse;
5 |
6 | import org.beetl.ext.simulate.WebSimulate;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.stereotype.Controller;
9 | import org.springframework.web.bind.annotation.RequestMapping;
10 |
11 | /**
12 | *
13 | * 标题: 控制器
14 | * 描述: 前后端分离,使用 WebSimulate 模拟后端服务
15 | *
16 | * @author zc
17 | * @date 2018/03/14
18 | */
19 | @Controller
20 | public class SimulateController {
21 |
22 | @Autowired
23 | private WebSimulate webSimulate;
24 |
25 | @RequestMapping("/api/**")
26 | public void simluateJson(HttpServletRequest request, HttpServletResponse response) {
27 | webSimulate.execute(request, response);
28 | }
29 |
30 | @RequestMapping("/**/*.html")
31 | public void simluateView(HttpServletRequest request, HttpServletResponse response) {
32 | webSimulate.execute(request, response);
33 | }
34 | }
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/java/com/zccoder/boot2/ch4/view/beetl/util/SimpleFunction.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch4.view.beetl.util;
2 |
3 | import org.beetl.core.Context;
4 | import org.beetl.core.Function;
5 | import org.springframework.stereotype.Component;
6 |
7 | /**
8 | *
9 | * 标题: 自定义函数
10 | * 描述: 对模版进行扩展
11 | *
12 | * @author zc
13 | * @date 2018/03/14
14 | */
15 | @Component
16 | public class SimpleFunction implements Function {
17 |
18 | @Override
19 | public Object call(Object[] paras, Context ctx) {
20 | return "hi";
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/java/com/zccoder/boot2/ch4/view/beetl/util/SimpleTag.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch4.view.beetl.util;
2 |
3 | import java.io.IOException;
4 |
5 | import org.beetl.core.Tag;
6 | import org.springframework.context.annotation.Scope;
7 | import org.springframework.stereotype.Component;
8 |
9 | /**
10 | *
11 | * 标题: 自定义标签函数
12 | * 描述: 对模版进行扩展
13 | *
14 | * @author zc
15 | * @date 2018/03/14
16 | */
17 | @Component
18 | @Scope("prototype")
19 | public class SimpleTag extends Tag {
20 |
21 | @Override
22 | public void render() {
23 | System.out.println(this);
24 | try {
25 | ctx.byteWriter.writeString("被删除了,付费可以看");
26 | } catch (IOException e) {
27 | System.err.println(e.getMessage());
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/resources/application.properties:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zccodere/study-boot2/2118ce215c52abc1991aa72747f907206d319970/ch04-view/ch04-view-beetl/src/main/resources/application.properties
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/resources/beetl.properties:
--------------------------------------------------------------------------------
1 | DELIMITER_PLACEHOLDER_START=${
2 | DELIMITER_PLACEHOLDER_END=}
3 | DELIMITER_STATEMENT_START=@
4 | DELIMITER_STATEMENT_END=
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/resources/static/fonts/glyphicons-halflings-regular.eot:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zccodere/study-boot2/2118ce215c52abc1991aa72747f907206d319970/ch04-view/ch04-view-beetl/src/main/resources/static/fonts/glyphicons-halflings-regular.eot
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/resources/static/fonts/glyphicons-halflings-regular.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zccodere/study-boot2/2118ce215c52abc1991aa72747f907206d319970/ch04-view/ch04-view-beetl/src/main/resources/static/fonts/glyphicons-halflings-regular.ttf
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/resources/static/fonts/glyphicons-halflings-regular.woff:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zccodere/study-boot2/2118ce215c52abc1991aa72747f907206d319970/ch04-view/ch04-view-beetl/src/main/resources/static/fonts/glyphicons-halflings-regular.woff
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/resources/static/fonts/glyphicons-halflings-regular.woff2:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zccodere/study-boot2/2118ce215c52abc1991aa72747f907206d319970/ch04-view/ch04-view-beetl/src/main/resources/static/fonts/glyphicons-halflings-regular.woff2
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/resources/static/js/npm.js:
--------------------------------------------------------------------------------
1 | // This file is autogenerated via the `commonjs` Grunt task. You can require() this file in a CommonJS environment.
2 | require('../../js/transition.js')
3 | require('../../js/alert.js')
4 | require('../../js/button.js')
5 | require('../../js/carousel.js')
6 | require('../../js/collapse.js')
7 | require('../../js/dropdown.js')
8 | require('../../js/modal.js')
9 | require('../../js/tooltip.js')
10 | require('../../js/popover.js')
11 | require('../../js/scrollspy.js')
12 | require('../../js/tab.js')
13 | require('../../js/affix.js')
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/resources/templates/common/layout.btl:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Bootstrap 101 Template
9 |
10 |
11 |
12 |
13 |
14 |
15 |
19 |
20 |
21 | ${layoutContent}
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/resources/templates/functions/test.html:
--------------------------------------------------------------------------------
1 | hello,fn
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/resources/templates/index.btl:
--------------------------------------------------------------------------------
1 | @layout("/common/layout.btl"){
2 |
3 | 欢迎使用:${name}! ${hi()}
4 |
5 | @myTag(){
6 | 警告信息一
7 | @}
8 | @}
9 | ${test()}
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/resources/templates/test.html:
--------------------------------------------------------------------------------
1 | @a =1;
2 |
3 | hello go ${a}
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/resources/templates/user/userList.btl:
--------------------------------------------------------------------------------
1 | @ for(user in users){
2 | ${user.name}
3 | @}
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/resources/templates/values/api/user/1.json.var:
--------------------------------------------------------------------------------
1 | var json = {name:"lijz"};
--------------------------------------------------------------------------------
/ch04-view/ch04-view-beetl/src/main/resources/templates/values/user/userlist.html.var:
--------------------------------------------------------------------------------
1 | var users = [{name:"xiandafu"},{name:"lucy"}];
2 | var view = "/user/userList.btl";
--------------------------------------------------------------------------------
/ch04-view/ch04-view-json/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 |
8 | ch04-view
9 | com.zccoder
10 | 1.0.0-SNAPSHOT
11 |
12 |
13 | ch04-view-json
14 | jar
15 |
16 |
17 | 2.0.0.RELEASE
18 |
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-parent
25 | ${spring.boot.version}
26 | pom
27 | import
28 |
29 |
30 |
31 |
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-starter-web
36 |
37 |
38 |
39 |
40 |
41 |
42 | org.springframework.boot
43 | spring-boot-maven-plugin
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/ch04-view/ch04-view-json/src/main/java/com/zccoder/boot2/ch4/view/json/ViewJsonStart.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch4.view.json;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | *
8 | * 标题: 启动类
9 | * 描述: JSON 技术
10 | *
11 | * @author zc
12 | * @date 2018/03/14
13 | */
14 | @SpringBootApplication
15 | public class ViewJsonStart {
16 |
17 | public static void main(String[] args) {
18 | SpringApplication.run(ViewJsonStart.class, args);
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/ch04-view/ch04-view-json/src/main/java/com/zccoder/boot2/ch4/view/json/conf/JacksonConf.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch4.view.json.conf;
2 |
3 | import java.text.SimpleDateFormat;
4 |
5 | import org.springframework.context.annotation.Bean;
6 | import org.springframework.context.annotation.Configuration;
7 | import org.springframework.context.annotation.Primary;
8 |
9 | import com.fasterxml.jackson.databind.ObjectMapper;
10 |
11 | /**
12 | *
13 | * 标题: 配置类
14 | * 描述: Jackson 配置
15 | *
16 | * @author zc
17 | * @date 2018/03/14
18 | */
19 | @Configuration
20 | public class JacksonConf {
21 |
22 | @Bean
23 | @Primary
24 | public ObjectMapper getObjectMapper() {
25 | ObjectMapper objectMapper = new ObjectMapper();
26 | objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
27 | return objectMapper;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/ch04-view/ch04-view-json/src/main/java/com/zccoder/boot2/ch4/view/json/controller/JacksonStreamController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch4.view.json.controller;
2 |
3 | import java.io.IOException;
4 | import java.io.StringWriter;
5 |
6 | import org.apache.commons.logging.Log;
7 | import org.apache.commons.logging.LogFactory;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.stereotype.Controller;
10 | import org.springframework.web.bind.annotation.RequestMapping;
11 | import org.springframework.web.bind.annotation.ResponseBody;
12 |
13 | import com.fasterxml.jackson.core.JsonFactory;
14 | import com.fasterxml.jackson.core.JsonGenerator;
15 | import com.fasterxml.jackson.core.JsonParseException;
16 | import com.fasterxml.jackson.core.JsonParser;
17 | import com.fasterxml.jackson.core.JsonToken;
18 | import com.fasterxml.jackson.databind.ObjectMapper;
19 |
20 | /**
21 | *
22 | * 标题: 控制器
23 | * 描述: 直接输出 JSON 字符串
24 | *
25 | * @author zc
26 | * @date 2018/03/14
27 | */
28 | @Controller
29 | @RequestMapping("/stream")
30 | public class JacksonStreamController {
31 |
32 | @Autowired
33 | private ObjectMapper mapper;
34 |
35 | @RequestMapping("/parser.html")
36 | @ResponseBody
37 | public String parser() throws JsonParseException, IOException {
38 | String json = "{\"name\":\"lijz\",\"id\":10}";
39 | JsonFactory f = mapper.getFactory();
40 | String key = null, value = null;
41 | JsonParser parser = f.createParser(json);
42 | // {
43 | JsonToken token = parser.nextToken();
44 | //"name"
45 | token = parser.nextToken();
46 | if (token == JsonToken.FIELD_NAME) {
47 | key = parser.getCurrentName();
48 |
49 | }
50 |
51 | token = parser.nextToken();
52 | //"lijz"
53 | value = parser.getValueAsString();
54 | parser.close();
55 | return key + "," + value;
56 |
57 | }
58 |
59 | @RequestMapping("/generator.html")
60 | @ResponseBody
61 | public String generator() throws JsonParseException, IOException {
62 | JsonFactory f = mapper.getFactory();
63 | //输出到stringWriter
64 | StringWriter sw = new StringWriter();
65 | JsonGenerator g = f.createGenerator(sw);
66 | // {
67 | g.writeStartObject();
68 |
69 | // "message", "Hello world!"
70 | g.writeStringField("name", "lijiazhi");
71 | // }
72 | g.writeEndObject();
73 | g.close();
74 | return sw.toString();
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/ch04-view/ch04-view-json/src/main/java/com/zccoder/boot2/ch4/view/json/controller/TreeModelController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch4.view.json.controller;
2 |
3 | import org.springframework.stereotype.Controller;
4 | import org.springframework.web.bind.annotation.RequestMapping;
5 | import org.springframework.web.bind.annotation.ResponseBody;
6 |
7 | /**
8 | *
9 | * 标题: 控制器
10 | * 描述: 直接返回字符串
11 | *
12 | * @author zc
13 | * @date 2018/03/14
14 | */
15 | @Controller
16 | public class TreeModelController {
17 | @RequestMapping("/sayhello.html")
18 | @ResponseBody
19 | public String say() {
20 | return "hello world";
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/ch04-view/ch04-view-json/src/main/java/com/zccoder/boot2/ch4/view/json/entity/User.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch4.view.json.entity;
2 |
3 | import java.math.BigDecimal;
4 |
5 | import com.fasterxml.jackson.annotation.JsonIgnore;
6 | import com.fasterxml.jackson.annotation.JsonView;
7 |
8 | /**
9 | *
10 | * 标题: POJO类
11 | * 描述: 用户实体
12 | *
13 | * @author zc
14 | * @date 2018/03/14
15 | */
16 | public class User {
17 |
18 | public interface IdView {
19 | };
20 |
21 | public interface IdNameView extends IdView {
22 | };
23 |
24 | @JsonView(IdView.class)
25 | private Integer id;
26 | @JsonView(IdNameView.class)
27 | private String name;
28 | @JsonIgnore
29 | BigDecimal salary;
30 |
31 | public Integer getId() {
32 | return id;
33 | }
34 |
35 | public void setId(Integer id) {
36 | this.id = id;
37 | }
38 |
39 | public String getName() {
40 | return name;
41 | }
42 |
43 | public void setName(String name) {
44 | this.name = name;
45 | }
46 |
47 | public BigDecimal getSalary() {
48 | return salary;
49 | }
50 |
51 | public void setSalary(BigDecimal salary) {
52 | this.salary = salary;
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/ch04-view/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 |
8 | study-boot2
9 | com.zccoder
10 | 1.0.0-SNAPSHOT
11 |
12 |
13 | ch04-view
14 | pom
15 |
16 |
17 | ch04-view-beetl
18 | ch04-view-json
19 |
20 |
21 |
--------------------------------------------------------------------------------
/ch05-data/ch05-data-beetlsql/src/main/java/com/zccoder/boot2/ch5/data/beetlsql/DataBeetlsqlStart.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch5.data.beetlsql;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | *
8 | * 标题: 启动类
9 | * 描述: BeetlSQL
10 | * 时间: 2018/06/25
11 | *
12 | * @author zc
13 | */
14 | @SpringBootApplication
15 | public class DataBeetlsqlStart {
16 |
17 | public static void main(String[] args) {
18 | SpringApplication.run(DataBeetlsqlStart.class, args);
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/ch05-data/ch05-data-beetlsql/src/main/java/com/zccoder/boot2/ch5/data/beetlsql/conf/DataSourceConfig.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch5.data.beetlsql.conf;
2 |
3 | import javax.sql.DataSource;
4 |
5 | import org.beetl.sql.core.db.MySqlStyle;
6 | import org.beetl.sql.ext.spring4.BeetlSqlDataSource;
7 | import org.beetl.sql.ext.spring4.SqlManagerFactoryBean;
8 | import org.springframework.beans.factory.annotation.Qualifier;
9 | import org.springframework.context.annotation.Bean;
10 | import org.springframework.context.annotation.Configuration;
11 | import org.springframework.core.env.Environment;
12 |
13 | import com.ibeetl.starter.BeetlSqlCustomize;
14 | import com.zaxxer.hikari.HikariDataSource;
15 |
16 | /**
17 | *
18 | * 标题: 配置数据源
19 | * 描述: 配置数据源
20 | * 时间: 2018/06/25
21 | *
22 | * @author zc
23 | */
24 | @Configuration
25 | public class DataSourceConfig {
26 |
27 | @Bean(name = "dataSource")
28 | public DataSource datasource(Environment env) {
29 | HikariDataSource ds = new HikariDataSource();
30 | ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
31 | ds.setJdbcUrl(env.getProperty("spring.datasource.url"));
32 | ds.setUsername(env.getProperty("spring.datasource.username"));
33 | ds.setPassword(env.getProperty("spring.datasource.password"));
34 | return ds;
35 | }
36 |
37 | @Bean
38 | public BeetlSqlDataSource beetlSqlDataSource(Environment env){
39 | BeetlSqlDataSource source = new BeetlSqlDataSource();
40 | source.setMasterSource(datasource(env));
41 | return source;
42 | }
43 |
44 | // @Bean
45 | // public BeetlSqlCustomize beetlSqlCustomize() {
46 | // return new BeetlSqlCustomize() {
47 | // @Override
48 | // public void customize(SqlManagerFactoryBean sqlManager) {
49 | // sqlManager.setDbStyle(new MySqlStyle());
50 | //
51 | // }
52 | //
53 | // };
54 | // }
55 | }
--------------------------------------------------------------------------------
/ch05-data/ch05-data-beetlsql/src/main/java/com/zccoder/boot2/ch5/data/beetlsql/controller/UserController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch5.data.beetlsql.controller;
2 |
3 |
4 | import com.zccoder.boot2.ch5.data.beetlsql.entity.User;
5 | import com.zccoder.boot2.ch5.data.beetlsql.service.UserService;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.stereotype.Controller;
8 | import org.springframework.web.bind.annotation.PathVariable;
9 | import org.springframework.web.bind.annotation.RequestMapping;
10 | import org.springframework.web.bind.annotation.ResponseBody;
11 |
12 | import java.util.List;
13 |
14 | /**
15 | *
16 | * 标题: 用户Controller
17 | * 描述: 用户Controller
18 | * 时间: 2018/06/25
19 | *
20 | * @author zc
21 | */
22 | @Controller
23 | public class UserController {
24 |
25 | @Autowired
26 | private UserService userService;
27 |
28 | @RequestMapping("/user/{id}")
29 | @ResponseBody
30 | public User say(@PathVariable Integer id) {
31 | return userService.getUserById(id);
32 | }
33 |
34 | @RequestMapping("/user/query/{name}")
35 | @ResponseBody
36 | public List say(@PathVariable String name) {
37 | return userService.select(name);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/ch05-data/ch05-data-beetlsql/src/main/java/com/zccoder/boot2/ch5/data/beetlsql/dao/UserDao.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch5.data.beetlsql.dao;
2 |
3 |
4 | import com.zccoder.boot2.ch5.data.beetlsql.entity.User;
5 | import org.beetl.sql.core.annotatoin.SqlResource;
6 | import org.beetl.sql.core.mapper.BaseMapper;
7 |
8 | import java.util.List;
9 |
10 | /**
11 | *
12 | * 标题: 用户 DAO
13 | * 描述: 用户 DAO
14 | * 时间: 2018/06/25
15 | *
16 | * @author zc
17 | */
18 | @SqlResource("user")
19 | public interface UserDao extends BaseMapper {
20 |
21 | /**
22 | * 根据用户名查询用户
23 | * @param user 用户名
24 | * @return 用户列表
25 | */
26 | List selectSample(User user);
27 | }
--------------------------------------------------------------------------------
/ch05-data/ch05-data-beetlsql/src/main/java/com/zccoder/boot2/ch5/data/beetlsql/entity/User.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch5.data.beetlsql.entity;
2 |
3 | import java.io.Serializable;
4 | import java.util.Date;
5 |
6 | /**
7 | *
8 | * 标题: 用户实体类
9 | * 描述: 用户实体类
10 | * 时间: 2018/06/25
11 | *
12 | * @author zc
13 | */
14 | public class User implements Serializable {
15 |
16 | private static final long serialVersionUID = -6015847192273728434L;
17 | /**
18 | * 主键ID
19 | */
20 | private Integer id;
21 | /**
22 | * 部门ID
23 | */
24 | private Integer departmentId;
25 | /**
26 | * 名称
27 | */
28 | private String name;
29 | /**
30 | * 创建时间
31 | */
32 | private Date createTime;
33 |
34 | @Override
35 | public String toString() {
36 | return "User{" +
37 | "id=" + id +
38 | ", departmentId=" + departmentId +
39 | ", name='" + name + '\'' +
40 | ", createTime=" + createTime +
41 | '}';
42 | }
43 |
44 | public Integer getId() {
45 | return id;
46 | }
47 |
48 | public void setId(Integer id) {
49 | this.id = id;
50 | }
51 |
52 | public Integer getDepartmentId() {
53 | return departmentId;
54 | }
55 |
56 | public void setDepartmentId(Integer departmentId) {
57 | this.departmentId = departmentId;
58 | }
59 |
60 | public String getName() {
61 | return name;
62 | }
63 |
64 | public void setName(String name) {
65 | this.name = name;
66 | }
67 |
68 | public Date getCreateTime() {
69 | return createTime;
70 | }
71 |
72 | public void setCreateTime(Date createTime) {
73 | this.createTime = createTime;
74 | }
75 |
76 | }
77 |
--------------------------------------------------------------------------------
/ch05-data/ch05-data-beetlsql/src/main/java/com/zccoder/boot2/ch5/data/beetlsql/service/UserService.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch5.data.beetlsql.service;
2 |
3 | import com.zccoder.boot2.ch5.data.beetlsql.entity.User;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | *
9 | * 标题: 用户服务
10 | * 描述: 用户服务
11 | * 时间: 2018/06/25
12 | *
13 | * @author zc
14 | */
15 | public interface UserService {
16 | /**
17 | * 根据用户ID查询用户信息
18 | *
19 | * @param id 用户ID
20 | * @return 用户信息
21 | */
22 | User getUserById(Integer id);
23 |
24 | /**
25 | * 根据用户名称查询用户列表
26 | *
27 | * @param name 名称
28 | * @return 用户列表
29 | */
30 | List select(String name);
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/ch05-data/ch05-data-beetlsql/src/main/java/com/zccoder/boot2/ch5/data/beetlsql/service/impl/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch5.data.beetlsql.service.impl;
2 |
3 |
4 | import com.zccoder.boot2.ch5.data.beetlsql.dao.UserDao;
5 | import com.zccoder.boot2.ch5.data.beetlsql.entity.User;
6 | import com.zccoder.boot2.ch5.data.beetlsql.service.UserService;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.stereotype.Service;
9 |
10 | import java.util.List;
11 |
12 | /**
13 | *
14 | * 标题: 用户服务实现
15 | * 描述: 用户服务实现
16 | * 时间: 2018/06/25
17 | *
18 | * @author zc
19 | */
20 | @Service
21 | public class UserServiceImpl implements UserService {
22 |
23 | @Autowired
24 | private UserDao userDao;
25 |
26 | @Override
27 | public User getUserById(Integer id) {
28 | return userDao.unique(id);
29 | }
30 |
31 | @Override
32 | public List select(String name) {
33 | User paras = new User();
34 | paras.setName(name);
35 | return userDao.selectSample(paras);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/ch05-data/ch05-data-beetlsql/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false
2 | spring.datasource.username=root
3 | spring.datasource.password=root
4 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
5 | beetlsql.basePackage=com.zccoder.boot2.ch5.data.beetlsql.dao
6 | beetl-beetlsql.dev=true
7 |
--------------------------------------------------------------------------------
/ch05-data/ch05-data-beetlsql/src/main/resources/db.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE `user` (
2 | `id` int(11) NOT NULL AUTO_INCREMENT,
3 | `name` varchar(45) COLLATE utf8_bin DEFAULT NULL COMMENT '名称',
4 | `department_id` int(11) DEFAULT NULL,
5 | `create_time` date DEFAULT NULL COMMENT '创建时间',
6 | PRIMARY KEY (`id`)
7 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
8 |
9 | CREATE TABLE `department` (
10 | `id` int(11) NOT NULL AUTO_INCREMENT,
11 | `name` varchar(45) COLLATE utf8_bin DEFAULT NULL,
12 | PRIMARY KEY (`id`)
13 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
14 |
15 |
16 | INSERT INTO `user` VALUES (1, 'helo', 1, '2017-4-21 11:52:41');
17 | INSERT INTO `department` VALUES ('1', '研发部门');
18 |
--------------------------------------------------------------------------------
/ch05-data/ch05-data-beetlsql/src/main/resources/sql/user.md:
--------------------------------------------------------------------------------
1 | selectSample
2 | ===
3 |
4 | * 一个简单的查询例子
5 | * 根据用户名查询用户
6 | select * from user where 1=1
7 | @if(!isEmpty(name)){
8 | and name = #name#
9 | @}
10 |
--------------------------------------------------------------------------------
/ch05-data/ch05-data-template/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | ch05-data
7 | com.zccoder
8 | 1.0.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | ch05-data-template
13 | jar
14 |
15 |
16 | 2.0.0.RELEASE
17 |
18 |
19 |
20 |
21 |
22 | org.springframework.boot
23 | spring-boot-parent
24 | ${spring.boot.version}
25 | pom
26 | import
27 |
28 |
29 |
30 |
31 |
32 |
33 | org.springframework.boot
34 | spring-boot-starter-web
35 |
36 |
37 | org.springframework.boot
38 | spring-boot-devtools
39 | true
40 |
41 |
42 | org.springframework.boot
43 | spring-boot-starter-jdbc
44 |
45 |
46 | com.zaxxer
47 | HikariCP
48 |
49 |
50 | mysql
51 | mysql-connector-java
52 | 6.0.5
53 |
54 |
55 |
56 | org.springframework.boot
57 | spring-boot-starter-test
58 | test
59 |
60 |
61 |
62 |
63 |
64 |
65 | org.springframework.boot
66 | spring-boot-maven-plugin
67 |
68 |
69 |
70 |
71 |
--------------------------------------------------------------------------------
/ch05-data/ch05-data-template/src/main/java/com/zccoder/boot2/ch5/data/template/DataTemplateStart.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch5.data.template;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | *
8 | * 标题: 启动类
9 | * 描述: Spring JDBC Template
10 | * 时间: 2018/06/25
11 | *
12 | * @author zc
13 | */
14 | @SpringBootApplication
15 | public class DataTemplateStart {
16 |
17 | public static void main(String[] args) {
18 | SpringApplication.run(DataTemplateStart.class, args);
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/ch05-data/ch05-data-template/src/main/java/com/zccoder/boot2/ch5/data/template/config/DataSourceConfig.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch5.data.template.config;
2 |
3 | import com.zaxxer.hikari.HikariDataSource;
4 | import org.springframework.context.annotation.Bean;
5 | import org.springframework.context.annotation.Configuration;
6 | import org.springframework.core.env.Environment;
7 |
8 | import javax.sql.DataSource;
9 |
10 | /**
11 | *
12 | * 标题: 配置数据源
13 | * 描述: 配置数据源
14 | * 时间: 2018/06/25
15 | *
16 | * @author zc
17 | */
18 | @Configuration
19 | public class DataSourceConfig {
20 |
21 | @Bean(name = "dataSource")
22 | public DataSource dataSource(Environment environment){
23 | HikariDataSource dataSource = new HikariDataSource();
24 | dataSource.setJdbcUrl(environment.getProperty("spring.datasource.url"));
25 | dataSource.setUsername(environment.getProperty("spring.datasource.username"));
26 | dataSource.setPassword(environment.getProperty("spring.datasource.password"));
27 | dataSource.setDriverClassName(environment.getProperty("spring.datasource.driver-class-name"));
28 | return dataSource;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/ch05-data/ch05-data-template/src/main/java/com/zccoder/boot2/ch5/data/template/controller/UserController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch5.data.template.controller;
2 |
3 | import com.zccoder.boot2.ch5.data.template.entity.User;
4 | import com.zccoder.boot2.ch5.data.template.service.UserService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Controller;
7 | import org.springframework.web.bind.annotation.PathVariable;
8 | import org.springframework.web.bind.annotation.RequestMapping;
9 | import org.springframework.web.bind.annotation.ResponseBody;
10 |
11 | /**
12 | *
13 | * 标题: 用户controller
14 | * 描述: 用户controller
15 | * 时间: 2018/06/25
16 | *
17 | * @author zc
18 | */
19 | @Controller
20 | public class UserController {
21 |
22 | @Autowired
23 | private UserService userService;
24 |
25 | @RequestMapping("/user/{id}")
26 | @ResponseBody
27 | public User say(@PathVariable Long id){
28 | return userService.geUserById(id);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/ch05-data/ch05-data-template/src/main/java/com/zccoder/boot2/ch5/data/template/entity/User.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch5.data.template.entity;
2 |
3 | import java.io.Serializable;
4 | import java.util.Date;
5 |
6 | /**
7 | *
8 | * 标题: 用户实体类
9 | * 描述: 用户实体类
10 | * 时间: 2018/06/25
11 | *
12 | * @author zc
13 | */
14 | public class User implements Serializable {
15 |
16 | private static final long serialVersionUID = -6015847192273728434L;
17 | /**
18 | * 主键ID
19 | */
20 | private Integer id;
21 | /**
22 | * 部门ID
23 | */
24 | private Integer departmentId;
25 | /**
26 | * 名称
27 | */
28 | private String name;
29 | /**
30 | * 创建时间
31 | */
32 | private Date createTime;
33 |
34 | @Override
35 | public String toString() {
36 | return "User{" +
37 | "id=" + id +
38 | ", departmentId=" + departmentId +
39 | ", name='" + name + '\'' +
40 | ", createTime=" + createTime +
41 | '}';
42 | }
43 |
44 | public Integer getId() {
45 | return id;
46 | }
47 |
48 | public void setId(Integer id) {
49 | this.id = id;
50 | }
51 |
52 | public Integer getDepartmentId() {
53 | return departmentId;
54 | }
55 |
56 | public void setDepartmentId(Integer departmentId) {
57 | this.departmentId = departmentId;
58 | }
59 |
60 | public String getName() {
61 | return name;
62 | }
63 |
64 | public void setName(String name) {
65 | this.name = name;
66 | }
67 |
68 | public Date getCreateTime() {
69 | return createTime;
70 | }
71 |
72 | public void setCreateTime(Date createTime) {
73 | this.createTime = createTime;
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/ch05-data/ch05-data-template/src/main/java/com/zccoder/boot2/ch5/data/template/service/UserService.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch5.data.template.service;
2 |
3 | import com.zccoder.boot2.ch5.data.template.entity.User;
4 |
5 | /**
6 | *
7 | * 标题: 用户服务
8 | * 描述: 用户服务
9 | * 时间: 2018/06/25
10 | *
11 | * @author zc
12 | */
13 | public interface UserService {
14 |
15 | /**
16 | * 通过用户ID查询用户信息
17 | * @param id 用户ID
18 | * @return 用户信息
19 | */
20 | User geUserById(Long id) ;
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/ch05-data/ch05-data-template/src/main/java/com/zccoder/boot2/ch5/data/template/service/impl/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch5.data.template.service.impl;
2 |
3 |
4 | import com.zccoder.boot2.ch5.data.template.dao.UserDao;
5 | import com.zccoder.boot2.ch5.data.template.entity.User;
6 | import com.zccoder.boot2.ch5.data.template.service.UserService;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.stereotype.Service;
9 |
10 | /**
11 | *
12 | * 标题: 用户服务实现
13 | * 描述: 用户服务实现
14 | * 时间: 2018/06/25
15 | *
16 | * @author zc
17 | */
18 | @Service
19 | public class UserServiceImpl implements UserService {
20 |
21 | @Autowired
22 | private UserDao userDao;
23 |
24 | @Override
25 | public User geUserById(Long id) {
26 | User user = userDao.findUserById(id);
27 | return user;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/ch05-data/ch05-data-template/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false
2 | spring.datasource.username=root
3 | spring.datasource.password=root
4 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
5 |
--------------------------------------------------------------------------------
/ch05-data/ch05-data-template/src/main/resources/db.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE `user` (
2 | `id` int(11) NOT NULL AUTO_INCREMENT,
3 | `name` varchar(45) COLLATE utf8_bin DEFAULT NULL COMMENT '名称',
4 | `department_id` int(11) DEFAULT NULL,
5 | `create_time` date DEFAULT NULL COMMENT '创建时间',
6 | PRIMARY KEY (`id`)
7 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
8 |
9 | CREATE TABLE `department` (
10 | `id` int(11) NOT NULL AUTO_INCREMENT,
11 | `name` varchar(45) COLLATE utf8_bin DEFAULT NULL,
12 | PRIMARY KEY (`id`)
13 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
14 |
15 |
16 | INSERT INTO `user` VALUES (1, 'helo', 1, '2017-4-21 11:52:41');
17 | INSERT INTO `department` VALUES ('1', '研发部门');
18 |
--------------------------------------------------------------------------------
/ch05-data/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | study-boot2
7 | com.zccoder
8 | 1.0.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | ch05-data
13 | pom
14 |
15 | ch05-data-template
16 | ch05-data-beetlsql
17 |
18 |
19 |
--------------------------------------------------------------------------------
/ch06-jpa/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | study-boot2
7 | com.zccoder
8 | 1.0.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | ch06-jpa
13 | jar
14 |
15 |
16 | 2.0.0.RELEASE
17 |
18 |
19 |
20 |
21 |
22 | org.springframework.boot
23 | spring-boot-parent
24 | ${spring.boot.version}
25 | pom
26 | import
27 |
28 |
29 |
30 |
31 |
32 |
33 | org.springframework.boot
34 | spring-boot-starter-web
35 |
36 |
37 | org.springframework.boot
38 | spring-boot-devtools
39 | true
40 |
41 |
42 | org.springframework.boot
43 | spring-boot-starter-data-jpa
44 |
45 |
46 | com.zaxxer
47 | HikariCP
48 |
49 |
50 | mysql
51 | mysql-connector-java
52 | 6.0.5
53 |
54 |
55 |
56 | org.springframework.boot
57 | spring-boot-starter-test
58 | test
59 |
60 |
61 |
62 |
63 |
64 |
65 | org.springframework.boot
66 | spring-boot-maven-plugin
67 |
68 |
69 |
70 |
71 |
--------------------------------------------------------------------------------
/ch06-jpa/src/main/java/com/zccoder/boot2/ch6/jpa/JpaStart.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch6.jpa;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | *
8 | * 标题: 启动类
9 | * 描述: Spring Data JPA
10 | * 时间: 2018/06/26
11 | *
12 | * @author zc
13 | */
14 | @SpringBootApplication
15 | public class JpaStart {
16 |
17 | public static void main(String[] args) {
18 | SpringApplication.run(JpaStart.class, args);
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/ch06-jpa/src/main/java/com/zccoder/boot2/ch6/jpa/config/DataSourceConfig.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch6.jpa.config;
2 |
3 | import com.zaxxer.hikari.HikariDataSource;
4 | import org.springframework.context.annotation.Bean;
5 | import org.springframework.context.annotation.Configuration;
6 | import org.springframework.core.env.Environment;
7 |
8 | import javax.sql.DataSource;
9 |
10 | /**
11 | *
12 | * 标题: 配置数据源
13 | * 描述: 配置数据源
14 | * 时间: 2018/06/26
15 | *
16 | * @author zc
17 | */
18 | @Configuration
19 | public class DataSourceConfig {
20 |
21 | @Bean(name = "dataSource")
22 | public DataSource dataSource(Environment environment){
23 | HikariDataSource dataSource = new HikariDataSource();
24 | dataSource.setJdbcUrl(environment.getProperty("spring.datasource.url"));
25 | dataSource.setDriverClassName(environment.getProperty("spring.datasource.driver-class-name"));
26 | dataSource.setUsername(environment.getProperty("spring.datasource.username"));
27 | dataSource.setPassword(environment.getProperty("spring.datasource.password"));
28 | return dataSource;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/ch06-jpa/src/main/java/com/zccoder/boot2/ch6/jpa/entity/Department.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch6.jpa.entity;
2 |
3 | import javax.persistence.*;
4 | import java.util.HashSet;
5 | import java.util.Set;
6 |
7 | /**
8 | *
9 | * 标题: 部门实体
10 | * 描述: 部门实体
11 | * 时间: 2018/06/26
12 | *
13 | * @author zc
14 | */
15 | @Entity
16 | public class Department {
17 |
18 | /**
19 | * 主键ID
20 | */
21 | @Id
22 | @GeneratedValue(strategy = GenerationType.IDENTITY)
23 | private Integer id;
24 | /**
25 | * 名称
26 | */
27 | @Column
28 | private String name;
29 | /**
30 | * 用户信息
31 | */
32 | @OneToMany(mappedBy = "department")
33 | private Set users = new HashSet<>();
34 |
35 | public Department() {
36 | }
37 |
38 | public Integer getId() {
39 | return id;
40 | }
41 |
42 | public void setId(Integer id) {
43 | this.id = id;
44 | }
45 |
46 | public String getName() {
47 | return name;
48 | }
49 |
50 | public void setName(String name) {
51 | this.name = name;
52 | }
53 |
54 | public Set getUsers() {
55 | return users;
56 | }
57 |
58 | public void setUsers(Set users) {
59 | this.users = users;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/ch06-jpa/src/main/java/com/zccoder/boot2/ch6/jpa/entity/User.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch6.jpa.entity;
2 |
3 | import javax.persistence.*;
4 | import java.util.Date;
5 | import java.util.Objects;
6 |
7 | /**
8 | *
9 | * 标题: 用户实体
10 | * 描述: 用户实体
11 | * 时间: 2018/06/26
12 | *
13 | * @author zc
14 | */
15 | @Entity
16 | public class User {
17 |
18 | /**
19 | * 主键ID
20 | */
21 | @Id
22 | @GeneratedValue(strategy = GenerationType.IDENTITY)
23 | private Integer id;
24 | /**
25 | * 名称
26 | */
27 | @Column
28 | private String name;
29 | /**
30 | * 创建时间
31 | */
32 | @Column(name = "create_time")
33 | private Date createTime;
34 |
35 | /**
36 | * 部门
37 | */
38 | @ManyToOne
39 | @JoinColumn(name = "department_id")
40 | private Department department;
41 |
42 | @Override
43 | public boolean equals(Object o) {
44 | if (this == o) {
45 | return true;
46 | }
47 | if (o == null || getClass() != o.getClass()) {
48 | return false;
49 | }
50 | User user = (User) o;
51 | return Objects.equals(id, user.id);
52 | }
53 |
54 | @Override
55 | public int hashCode() {
56 |
57 | return Objects.hash(id);
58 | }
59 |
60 | public User() {
61 | }
62 |
63 | public Integer getId() {
64 | return id;
65 | }
66 |
67 | public void setId(Integer id) {
68 | this.id = id;
69 | }
70 |
71 | public String getName() {
72 | return name;
73 | }
74 |
75 | public void setName(String name) {
76 | this.name = name;
77 | }
78 |
79 | public Date getCreateTime() {
80 | return createTime;
81 | }
82 |
83 | public void setCreateTime(Date createTime) {
84 | this.createTime = createTime;
85 | }
86 |
87 | public Department getDepartment() {
88 | return department;
89 | }
90 |
91 | public void setDepartment(Department department) {
92 | this.department = department;
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/ch06-jpa/src/main/java/com/zccoder/boot2/ch6/jpa/service/UserService.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch6.jpa.service;
2 |
3 | import java.util.List;
4 |
5 | import com.zccoder.boot2.ch6.jpa.entity.User;
6 | import org.springframework.data.domain.Page;
7 | import org.springframework.data.domain.Pageable;
8 |
9 | /**
10 | *
11 | * 标题: 用户服务
12 | * 描述: 用户服务
13 | * 时间: 2018/06/26
14 | *
15 | * @author zc
16 | */
17 | public interface UserService {
18 | /**
19 | * 查询用户
20 | *
21 | * @param id 用户ID
22 | * @return 用户信息
23 | */
24 | User findUser(Integer id);
25 |
26 | /**
27 | * 新增用户
28 | *
29 | * @param user 用户
30 | * @return 影响行数
31 | */
32 | Integer addUser(User user);
33 |
34 | /**
35 | * 分页查询
36 | *
37 | * @param start 开始
38 | * @param end 结束
39 | * @return 用户集合
40 | */
41 | List getAllUser(int start, int end);
42 |
43 | /**
44 | * 查询用户
45 | *
46 | * @param name 名称
47 | * @return 用户信息
48 | */
49 | User getUser(String name);
50 |
51 | /**
52 | * 查询用户
53 | *
54 | * @param name 名称
55 | * @param departmentId 部门ID
56 | * @return 用户信息
57 | */
58 | User getUser(String name, Integer departmentId);
59 |
60 | /**
61 | * 分页查询用户
62 | *
63 | * @param departmentId 部门ID
64 | * @param page 分页参数
65 | * @return 用户集合
66 | */
67 | Page queryUser(Integer departmentId, Pageable page);
68 |
69 | /**
70 | * 分页查询用户
71 | *
72 | * @param departmentId 部门ID
73 | * @param page 分页参数
74 | * @return 用户集合
75 | */
76 | Page queryUser2(Integer departmentId, Pageable page);
77 |
78 | /**
79 | * 查询用户
80 | *
81 | * @param name 名称
82 | * @return 用户集合
83 | */
84 | List getByExample(String name);
85 |
86 | /**
87 | * 修改用户
88 | */
89 | void updateUser();
90 | }
91 |
--------------------------------------------------------------------------------
/ch06-jpa/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false
2 | spring.datasource.username=root
3 | spring.datasource.password=root
4 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
5 |
6 | spring.jpa.hibernate.ddl-auto=none
7 | spring.jpa.show-sql=true
--------------------------------------------------------------------------------
/ch06-jpa/src/main/resources/db.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE `user` (
2 | `id` int(11) NOT NULL AUTO_INCREMENT,
3 | `name` varchar(45) COLLATE utf8_bin DEFAULT NULL COMMENT '名称',
4 | `department_id` int(11) DEFAULT NULL,
5 | `create_time` date DEFAULT NULL COMMENT '创建时间',
6 | PRIMARY KEY (`id`)
7 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
8 |
9 | CREATE TABLE `department` (
10 | `id` int(11) NOT NULL AUTO_INCREMENT,
11 | `name` varchar(45) COLLATE utf8_bin DEFAULT NULL,
12 | PRIMARY KEY (`id`)
13 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
14 |
15 |
16 | INSERT INTO `user` VALUES (1, 'helo', 1, '2017-4-21 11:52:41');
17 | INSERT INTO `department` VALUES ('1', '研发部门');
18 |
--------------------------------------------------------------------------------
/ch07-config/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 |
--------------------------------------------------------------------------------
/ch07-config/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 |
6 | study-boot2
7 | com.zccoder
8 | 1.0.0-SNAPSHOT
9 |
10 |
11 | ch07-config
12 | jar
13 |
14 |
15 | 2.0.0.RELEASE
16 |
17 |
18 |
19 |
20 |
21 | org.springframework.boot
22 | spring-boot-dependencies
23 | ${spring.boot.version}
24 | pom
25 | import
26 |
27 |
28 |
29 |
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-devtools
34 | true
35 |
36 |
37 | org.springframework.boot
38 | spring-boot-starter-web
39 |
40 |
41 | org.springframework.boot
42 | spring-boot-starter-tomcat
43 |
44 |
45 |
46 |
47 | org.springframework.boot
48 | spring-boot-starter-undertow
49 |
50 |
51 | org.springframework.boot
52 | spring-boot-configuration-processor
53 |
54 |
55 |
--------------------------------------------------------------------------------
/ch07-config/src/main/java/com/zccoder/boot2/ch7/config/ConfigStart.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch7.config;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | *
8 | * 标题: 启动类
9 | * 描述: 启动类
10 | * 时间: 2018/09/21
11 | *
12 | * @author zc
13 | */
14 | @SpringBootApplication
15 | public class ConfigStart {
16 |
17 | public static void main(String[] args) {
18 | SpringApplication.run(ConfigStart.class, args);
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/ch07-config/src/main/java/com/zccoder/boot2/ch7/config/config/EncryptCondition.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch7.config.config;
2 |
3 | import org.springframework.context.annotation.Condition;
4 | import org.springframework.context.annotation.ConditionContext;
5 | import org.springframework.core.env.Environment;
6 | import org.springframework.core.io.Resource;
7 | import org.springframework.core.type.AnnotatedTypeMetadata;
8 |
9 | /**
10 | *
11 | * 标题: 加密条件
12 | * 描述: 加密条件
13 | * 时间: 2018/09/21
14 | *
15 | * @author zc
16 | */
17 | public class EncryptCondition implements Condition {
18 |
19 | @Override
20 | public boolean matches(ConditionContext ctx, AnnotatedTypeMetadata metadata) {
21 | Resource res = ctx.getResourceLoader().getResource("encrypt.txt");
22 | Environment env = ctx.getEnvironment();
23 | // 当 存在encrypt.txt文件 且 mobile.encrypt.enable为true 时,才匹配成功
24 | return res.exists() && env.containsProperty("mobile.encrypt.enable");
25 | }
26 |
27 | }
--------------------------------------------------------------------------------
/ch07-config/src/main/java/com/zccoder/boot2/ch7/config/config/EnvConfig.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch7.config.config;
2 |
3 | import org.springframework.beans.BeansException;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.beans.factory.config.BeanPostProcessor;
6 | import org.springframework.boot.autoconfigure.condition.ConditionalOnJava;
7 | import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.Range;
8 | import org.springframework.boot.system.JavaVersion;
9 | import org.springframework.context.annotation.Configuration;
10 | import org.springframework.core.env.Environment;
11 |
12 | /**
13 | *
14 | * 标题: 条件注解
15 | * 描述: 条件注解
16 | * 时间: 2018/09/21
17 | *
18 | * @author zc
19 | */
20 | @Configuration
21 | @ConditionalOnJava(range = Range.EQUAL_OR_NEWER, value = JavaVersion.EIGHT)
22 | public class EnvConfig implements BeanPostProcessor {
23 |
24 | @Autowired
25 | private Environment env;
26 |
27 | public int getServerPort() {
28 | return env.getProperty("server.port", Integer.class);
29 | }
30 |
31 | @Override
32 | public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
33 | if (bean instanceof UrlTestBean) {
34 | System.out.println("=========== " + beanName);
35 | }
36 | return bean;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/ch07-config/src/main/java/com/zccoder/boot2/ch7/config/config/MobileEncryptBean.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch7.config.config;
2 |
3 | /**
4 | *
5 | * 标题: 手机加密Bean
6 | * 描述: 手机加密Bean
7 | * 时间: 2018/09/21
8 | *
9 | * @author zc
10 | */
11 | public class MobileEncryptBean {
12 |
13 | public String getMobile(String mobile) {
14 | return mobile;
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/ch07-config/src/main/java/com/zccoder/boot2/ch7/config/config/MobileEncryptCondition.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch7.config.config;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Conditional;
5 | import org.springframework.context.annotation.Configuration;
6 |
7 | /**
8 | *
9 | * 标题: 手机加密条件配置
10 | * 描述: 手机加密条件配置
11 | * 时间: 2018/09/21
12 | *
13 | * @author zc
14 | */
15 | @Configuration
16 | @Conditional(EncryptCondition.class)
17 | public class MobileEncryptCondition {
18 |
19 | @Bean
20 | public MobileEncryptBean mobileEncryptBean() {
21 | return new MobileEncryptBean();
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/ch07-config/src/main/java/com/zccoder/boot2/ch7/config/config/MyConfiguration.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch7.config.config;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 |
6 | /**
7 | *
8 | * 标题: 自定义配置
9 | * 描述: 自定义配置
10 | * 时间: 2018/09/21
11 | *
12 | * @author zc
13 | */
14 | @Configuration
15 | public class MyConfiguration {
16 |
17 | @Bean
18 | public UrlTestBean getURLTestBean() {
19 | return new UrlTestBean();
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/ch07-config/src/main/java/com/zccoder/boot2/ch7/config/config/ServerConfig.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch7.config.config;
2 |
3 | import org.springframework.boot.context.properties.ConfigurationProperties;
4 | import org.springframework.context.annotation.Configuration;
5 |
6 | /**
7 | *
8 | * 标题: 使用 @ConfigurationProperties 加载配置参数
9 | * 描述: 使用 @ConfigurationProperties 加载配置参数
10 | * 时间: 2018/09/21
11 | *
12 | * @author zc
13 | */
14 | @ConfigurationProperties("server")
15 | @Configuration
16 | public class ServerConfig {
17 |
18 | private int port;
19 |
20 | private Servlet servlet = new Servlet();
21 |
22 | public int getPort() {
23 | return port;
24 | }
25 |
26 | public void setPort(int port) {
27 | this.port = port;
28 | }
29 |
30 |
31 | public Servlet getServlet() {
32 | return servlet;
33 | }
34 |
35 | public void setServlet(Servlet servlet) {
36 | this.servlet = servlet;
37 | }
38 |
39 |
40 | public static class Servlet {
41 | String path;
42 |
43 | public String getPath() {
44 | return path;
45 | }
46 |
47 | public void setPath(String path) {
48 | this.path = path;
49 | }
50 |
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/ch07-config/src/main/java/com/zccoder/boot2/ch7/config/config/UrlTestBean.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch7.config.config;
2 |
3 | /**
4 | *
5 | * 标题: 用于演示采用java config 配置此类
6 | * 描述: 用于演示采用java config 配置此类
7 | * 时间: 2018/09/21
8 | *
9 | * @author zc
10 | */
11 | public class UrlTestBean {
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/ch07-config/src/main/java/com/zccoder/boot2/ch7/config/controller/PropertyController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch7.config.controller;
2 |
3 | import com.zccoder.boot2.ch7.config.config.EnvConfig;
4 | import com.zccoder.boot2.ch7.config.config.ServerConfig;
5 | import org.apache.commons.logging.Log;
6 | import org.apache.commons.logging.LogFactory;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.beans.factory.annotation.Value;
9 | import org.springframework.stereotype.Controller;
10 | import org.springframework.web.bind.annotation.RequestMapping;
11 | import org.springframework.web.bind.annotation.ResponseBody;
12 |
13 | /**
14 | *
15 | * 标题: Controller
16 | * 描述: Controller
17 | * 时间: 2018/09/21
18 | *
19 | * @author zc
20 | */
21 | @Controller
22 | public class PropertyController {
23 | private Log log = LogFactory.getLog(PropertyController.class);
24 | @Autowired
25 | EnvConfig envConfig;
26 | @Autowired
27 | ServerConfig serverConfig;
28 |
29 | @RequestMapping("/sayhello.html")
30 | public @ResponseBody
31 | String say() {
32 | log.info("acess");
33 | return "hello world";
34 | }
35 |
36 |
37 | @RequestMapping("/showenv.html")
38 | public @ResponseBody
39 | String env() {
40 |
41 | return "port:" + envConfig.getServerPort();
42 | }
43 |
44 | @RequestMapping("/showvalue.html")
45 | public @ResponseBody
46 | String value(@Value("${server.port}") int port) {
47 | return "port:" + port;
48 | }
49 |
50 | @RequestMapping("/showserver.html")
51 | public @ResponseBody
52 | String value() {
53 |
54 | return "port:" + serverConfig.getPort() + " contxtPath:" + serverConfig.getServlet().getPath();
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/ch07-config/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | server.port=9090
2 | server.servlet.context-path=/config
3 | logging.level.root=info
4 | logging.level.org.springframework=info
5 | logging.level.com.zccoder.boot2.ch7.config=debug
6 | #logging.file = my.log
7 | logging.pattern.console=%level %date{HH:mm:ss} %logger{20}.%M %L :%m%n
8 | logging.pattern.file= %level %date{ISO8601} [%thread] %logger{20}.%M %L :%m%n
--------------------------------------------------------------------------------
/ch07-config/src/main/resources/banner.txt:
--------------------------------------------------------------------------------
1 | ////////////////////////////////////////////////////////////////////
2 | // _ooOoo_ //
3 | // o8888888o //
4 | // 88" . "88 //
5 | // (| ^_^ |) //
6 | // O\ = /O //
7 | // ____/`---'\____ //
8 | // .' \\| |// `. //
9 | // / \\||| : |||// \ //
10 | // / _||||| -:- |||||- \ //
11 | // | | \\\ - /// | | //
12 | // | \_| ''\---/'' | | //
13 | // \ .-\__ `-` ___/-. / //
14 | // ___`. .' /--.--\ `. . ___ //
15 | // ."" '< `.___\_<|>_/___.' >'"". //
16 | // | | : `- \`.;`\ _ /`;.`/ - ` : | | //
17 | // \ \ `-. \_ __\ /__ _/ .-` / / //
18 | // ========`-.____`-.___\_____/___.-`____.-'======== //
19 | // `=---=' //
20 | // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
21 | // 佛祖保佑 永不宕机 永无BUG //
22 | ////////////////////////////////////////////////////////////////////
--------------------------------------------------------------------------------
/ch07-config/src/main/resources/static/images/apple.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zccodere/study-boot2/2118ce215c52abc1991aa72747f907206d319970/ch07-config/src/main/resources/static/images/apple.ico
--------------------------------------------------------------------------------
/ch07-config/src/main/resources/static/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Hey
7 |
8 |
9 |
--------------------------------------------------------------------------------
/ch08-deploy/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 |
--------------------------------------------------------------------------------
/ch08-deploy/env/application-test.properties:
--------------------------------------------------------------------------------
1 | server.port=9900
2 | logging.level.org.springframework.web=DEBUG
--------------------------------------------------------------------------------
/ch08-deploy/env/application.properties:
--------------------------------------------------------------------------------
1 | server.port=8080
--------------------------------------------------------------------------------
/ch08-deploy/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 |
6 | study-boot2
7 | com.zccoder
8 | 1.0.0-SNAPSHOT
9 |
10 |
11 | ch08-deploy
12 | jar
13 |
14 |
15 | 2.0.0.RELEASE
16 |
17 |
18 |
19 |
20 |
21 | org.springframework.boot
22 | spring-boot-dependencies
23 | ${spring.boot.version}
24 | pom
25 | import
26 |
27 |
28 |
29 |
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-devtools
34 | true
35 |
36 |
37 |
38 | org.springframework.boot
39 | spring-boot-starter-web
40 |
41 |
42 | org.springframework.boot
43 | spring-boot-starter-tomcat
44 |
45 |
46 |
47 | com.zaxxer
48 | HikariCP
49 |
50 |
51 | mysql
52 | mysql-connector-java
53 |
54 |
55 |
56 |
57 |
58 |
59 | org.springframework.boot
60 | spring-boot-maven-plugin
61 | ${spring.boot.version}
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/ch08-deploy/src/main/java/com/zccoder/boot2/ch8/deploy/DeployStart.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch8.deploy;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.boot.builder.SpringApplicationBuilder;
6 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
7 |
8 | /**
9 | *
10 | * 标题: 启动类
11 | * 描述: 启动类
12 | * 时间: 2018/09/21
13 | *
14 | * @author zc
15 | */
16 | @SpringBootApplication
17 | public class DeployStart extends SpringBootServletInitializer {
18 |
19 | @Override
20 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
21 | return application.sources(DeployStart.class);
22 | }
23 |
24 | public static void main(String[] args) {
25 | SpringApplication.run(DeployStart.class, args);
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/ch08-deploy/src/main/java/com/zccoder/boot2/ch8/deploy/conf/DataSourceConf.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch8.deploy.conf;
2 |
3 | import javax.sql.DataSource;
4 |
5 | import org.springframework.context.annotation.Bean;
6 | import org.springframework.context.annotation.Configuration;
7 | import org.springframework.context.annotation.Profile;
8 | import org.springframework.core.env.Environment;
9 |
10 | import com.zaxxer.hikari.HikariDataSource;
11 |
12 | /**
13 | *
14 | * 标题: 数据源配置
15 | * 描述: 数据源配置
16 | * 时间: 2018/09/21
17 | *
18 | * @author zc
19 | */
20 | @Configuration
21 | public class DataSourceConf {
22 | @Bean(name = "dataSource")
23 | public DataSource devSource(Environment env) {
24 | HikariDataSource prod = getDataSource(env);
25 | prod.setMaximumPoolSize(2);
26 | return prod;
27 |
28 | }
29 |
30 | @Bean(name = "dataSource")
31 | @Profile("test")
32 | public DataSource testDatasource(Environment env) {
33 | HikariDataSource test = getDataSource(env);
34 | test.setMaximumPoolSize(10);
35 | return test;
36 | }
37 |
38 | @Bean(name = "dataSource")
39 | @Profile("prod")
40 | public DataSource prodSource(Environment env) {
41 | HikariDataSource prod = getDataSource(env);
42 |
43 | prod.setMaximumPoolSize(100);
44 | return prod;
45 |
46 | }
47 |
48 | private HikariDataSource getDataSource(Environment env) {
49 | HikariDataSource ds = new HikariDataSource();
50 | ds.setJdbcUrl(env.getProperty("spring.datasource.url"));
51 | ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
52 | ds.setUsername(env.getProperty("spring.datasource.username"));
53 | ds.setPassword(env.getProperty("spring.datasource.password"));
54 | return ds;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/ch08-deploy/src/main/java/com/zccoder/boot2/ch8/deploy/controller/HelloWorldController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch8.deploy.controller;
2 |
3 | import org.springframework.stereotype.Controller;
4 | import org.springframework.web.bind.annotation.RequestMapping;
5 | import org.springframework.web.bind.annotation.ResponseBody;
6 |
7 | /**
8 | *
9 | * 标题: Controller
10 | * 描述: Controller
11 | * 时间: 2018/09/21
12 | *
13 | * @author zc
14 | */
15 | @Controller
16 | public class HelloWorldController {
17 |
18 | @RequestMapping("/sayhello.html")
19 | public @ResponseBody
20 | String say() {
21 | return "hello world";
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/ch08-deploy/src/main/resources/application-prod.properties:
--------------------------------------------------------------------------------
1 | server.port=9000
--------------------------------------------------------------------------------
/ch08-deploy/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.datasource.url=jdbc:mysql://127.0.0.1:3306/orm?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
2 | spring.datasource.username=root
3 | spring.datasource.password=123456
4 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver
5 | server.port=8080
6 |
--------------------------------------------------------------------------------
/ch09-test/src/main/java/com/zccoder/boot2/ch9/test/TestApplication.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch9.test;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | *
8 | * 标题: 启动类
9 | * 描述: 启动类
10 | * 时间: 2018/09/25
11 | *
12 | * @author zc
13 | */
14 | @SpringBootApplication
15 | public class TestApplication {
16 |
17 | public static void main(String[] args) {
18 | SpringApplication.run(TestApplication.class, args);
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/ch09-test/src/main/java/com/zccoder/boot2/ch9/test/config/DataSourceConfig.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch9.test.config;
2 |
3 | import javax.sql.DataSource;
4 |
5 | import org.springframework.beans.factory.annotation.Qualifier;
6 | import org.springframework.context.annotation.Bean;
7 | import org.springframework.context.annotation.Configuration;
8 | import org.springframework.core.env.Environment;
9 |
10 | import com.zaxxer.hikari.HikariDataSource;
11 |
12 | /**
13 | *
14 | * 标题: 数据源配置
15 | * 描述: 数据源配置
16 | * 时间: 2018/09/25
17 | *
18 | * @author zc
19 | */
20 | @Configuration
21 | public class DataSourceConfig {
22 |
23 | @Bean(name = "dataSource")
24 | public DataSource datasource(Environment env) {
25 | HikariDataSource ds = new HikariDataSource();
26 | ds.setJdbcUrl(env.getProperty("spring.datasource.url"));
27 | ds.setUsername(env.getProperty("spring.datasource.username"));
28 | ds.setPassword(env.getProperty("spring.datasource.password"));
29 | ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
30 | return ds;
31 | }
32 |
33 | }
--------------------------------------------------------------------------------
/ch09-test/src/main/java/com/zccoder/boot2/ch9/test/controller/UserController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch9.test.controller;
2 |
3 | import com.zccoder.boot2.ch9.test.entity.User;
4 | import com.zccoder.boot2.ch9.test.service.UserService;
5 | import org.apache.commons.logging.Log;
6 | import org.apache.commons.logging.LogFactory;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.stereotype.Controller;
9 | import org.springframework.web.bind.annotation.PathVariable;
10 | import org.springframework.web.bind.annotation.RequestMapping;
11 | import org.springframework.web.bind.annotation.ResponseBody;
12 |
13 | /**
14 | *
15 | * 标题: 用户 Controller
16 | * 描述: 用户 Controller
17 | * 时间: 2018/09/25
18 | *
19 | * @author zc
20 | */
21 | @Controller
22 | public class UserController {
23 | private Log log = LogFactory.getLog(UserController.class);
24 |
25 | @Autowired
26 | UserService userService;
27 |
28 | @RequestMapping("/user/{id}")
29 | public @ResponseBody
30 | String getUser(@PathVariable Integer id) {
31 | return String.valueOf(userService.getCredit(id));
32 | }
33 |
34 | @RequestMapping("/user/{id}/{name}")
35 | public @ResponseBody
36 | String updateUser(@PathVariable Integer id, @PathVariable String name) {
37 | User user = new User();
38 | user.setId(id);
39 | user.setName(name);
40 | userService.updateUser(user);
41 | return "{\"success\":true}";
42 | }
43 |
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/ch09-test/src/main/java/com/zccoder/boot2/ch9/test/dao/UserDao.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch9.test.dao;
2 |
3 | import org.beetl.sql.core.mapper.BaseMapper;
4 |
5 | import com.zccoder.boot2.ch9.test.entity.User;
6 |
7 | /**
8 | *
9 | * 标题: 用户DAO
10 | * 描述: 用户DAO
11 | * 时间: 2018/09/25
12 | *
13 | * @author zc
14 | */
15 | public interface UserDao extends BaseMapper {
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/ch09-test/src/main/java/com/zccoder/boot2/ch9/test/entity/User.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch9.test.entity;
2 |
3 | /**
4 | *
5 | * 标题: 用户实体
6 | * 描述: 用户实体
7 | * 时间: 2018/09/25
8 | *
9 | * @author zc
10 | */
11 | public class User {
12 |
13 | Integer id;
14 |
15 | String name;
16 |
17 | public Integer getId() {
18 | return id;
19 | }
20 |
21 | public void setId(Integer id) {
22 | this.id = id;
23 | }
24 |
25 | public String getName() {
26 | return name;
27 | }
28 |
29 | public void setName(String name) {
30 | this.name = name;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/ch09-test/src/main/java/com/zccoder/boot2/ch9/test/service/CreditSystemService.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch9.test.service;
2 |
3 | /**
4 | *
5 | * 标题: 积分系统服务
6 | * 描述: 积分系统服务
7 | * 时间: 2018/09/25
8 | *
9 | * @author zc
10 | */
11 | public interface CreditSystemService {
12 | /**
13 | * 获取积分
14 | *
15 | * @param userId ID
16 | * @return 积分
17 | */
18 | int getUserCredit(int userId);
19 |
20 | /**
21 | * 增加积分
22 | *
23 | * @param userId ID
24 | * @param score 分数
25 | * @return 结果
26 | */
27 | boolean addCedit(int userId, int score);
28 | }
29 |
--------------------------------------------------------------------------------
/ch09-test/src/main/java/com/zccoder/boot2/ch9/test/service/UserService.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch9.test.service;
2 |
3 | import com.zccoder.boot2.ch9.test.entity.User;
4 |
5 | /**
6 | *
7 | * 标题: 用户服务
8 | * 描述: 用户服务
9 | * 时间: 2018/09/25
10 | *
11 | * @author zc
12 | */
13 | public interface UserService {
14 | /**
15 | * 获取积分
16 | *
17 | * @param userId ID
18 | * @return 积分
19 | */
20 | int getCredit(int userId);
21 |
22 | /**
23 | * 更新用户信息
24 | *
25 | * @param user 用户
26 | * @return 结果
27 | */
28 | boolean updateUser(User user);
29 | }
30 |
--------------------------------------------------------------------------------
/ch09-test/src/main/java/com/zccoder/boot2/ch9/test/service/impl/CreditSystemServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch9.test.service.impl;
2 |
3 | import org.springframework.stereotype.Service;
4 |
5 | import com.zccoder.boot2.ch9.test.service.CreditSystemService;
6 |
7 | /**
8 | *
9 | * 标题: 积分系统服务实现
10 | * 描述: 积分系统服务实现
11 | * 时间: 2018/09/25
12 | *
13 | * @author zc
14 | */
15 | @Service
16 | public class CreditSystemServiceImpl implements CreditSystemService {
17 |
18 | @Override
19 | public int getUserCredit(int userId) {
20 | throw new UnsupportedOperationException("积分系统未完成,不能调用");
21 | }
22 |
23 | @Override
24 | public boolean addCedit(int userId, int score) {
25 | throw new UnsupportedOperationException("积分系统未完成,不能调用");
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/ch09-test/src/main/java/com/zccoder/boot2/ch9/test/service/impl/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch9.test.service.impl;
2 |
3 | import com.zccoder.boot2.ch9.test.dao.UserDao;
4 | import com.zccoder.boot2.ch9.test.entity.User;
5 | import org.apache.commons.logging.Log;
6 | import org.apache.commons.logging.LogFactory;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.stereotype.Service;
9 | import org.springframework.transaction.annotation.Transactional;
10 |
11 | import com.zccoder.boot2.ch9.test.service.CreditSystemService;
12 | import com.zccoder.boot2.ch9.test.service.UserService;
13 |
14 | /**
15 | *
16 | * 标题: 用户服务实现
17 | * 描述: 用户服务实现
18 | * 时间: 2018/09/25
19 | *
20 | * @author zc
21 | */
22 | @Service
23 | @Transactional(rollbackFor = Exception.class)
24 | public class UserServiceImpl implements UserService {
25 |
26 | Log log = LogFactory.getLog(this.getClass());
27 | @Autowired
28 | CreditSystemService creditSystemService;
29 |
30 | @Autowired
31 | UserDao userDao;
32 |
33 | @Override
34 | public int getCredit(int userId) {
35 | return creditSystemService.getUserCredit(userId);
36 |
37 | }
38 |
39 | @Override
40 | public boolean updateUser(User user) {
41 | int ret = userDao.updateById(user);
42 | return ret == 1;
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/ch09-test/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false
2 | spring.datasource.username=root
3 | spring.datasource.password=root
4 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver
5 |
--------------------------------------------------------------------------------
/ch09-test/src/main/resources/banner.txt:
--------------------------------------------------------------------------------
1 | ////////////////////////////////////////////////////////////////////
2 | // _ooOoo_ //
3 | // o8888888o //
4 | // 88" . "88 //
5 | // (| ^_^ |) //
6 | // O\ = /O //
7 | // ____/`---'\____ //
8 | // .' \\| |// `. //
9 | // / \\||| : |||// \ //
10 | // / _||||| -:- |||||- \ //
11 | // | | \\\ - /// | | //
12 | // | \_| ''\---/'' | | //
13 | // \ .-\__ `-` ___/-. / //
14 | // ___`. .' /--.--\ `. . ___ //
15 | // ."" '< `.___\_<|>_/___.' >'"". //
16 | // | | : `- \`.;`\ _ /`;.`/ - ` : | | //
17 | // \ \ `-. \_ __\ /__ _/ .-` / / //
18 | // ========`-.____`-.___\_____/___.-`____.-'======== //
19 | // `=---=' //
20 | // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
21 | // 佛祖保佑 永不宕机 永无BUG //
22 | ////////////////////////////////////////////////////////////////////
--------------------------------------------------------------------------------
/ch09-test/src/test/java/com/zccoder/boot2/ch9/test/controller/UserControllerTest.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch9.test.controller;
2 |
3 | import static org.mockito.BDDMockito.given;
4 | import static org.mockito.Mockito.*;
5 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
6 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
7 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
8 |
9 | import org.junit.Test;
10 | import org.junit.runner.RunWith;
11 | import org.springframework.beans.factory.annotation.Autowired;
12 | import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
13 | import org.springframework.boot.test.mock.mockito.MockBean;
14 | import org.springframework.test.context.junit4.SpringRunner;
15 | import org.springframework.test.web.servlet.MockMvc;
16 |
17 | import com.zccoder.boot2.ch9.test.controller.UserController;
18 | import com.zccoder.boot2.ch9.test.entity.User;
19 | import com.zccoder.boot2.ch9.test.service.UserService;
20 |
21 | @RunWith(SpringRunner.class)
22 | //需要模拟测试的Controller
23 | @WebMvcTest(UserController.class)
24 | public class UserControllerTest {
25 | @Autowired
26 | private MockMvc mvc;
27 |
28 | @MockBean
29 | UserService userService;
30 |
31 | @Test
32 | public void testMvc() throws Exception {
33 | int userId = 10;
34 | int expectedCredit = 100;
35 | //模拟userService
36 | given(this.userService.getCredit(anyInt())).willReturn(100);
37 | //http 调用
38 | mvc.perform(get("/user/{id}", userId)).andExpect(content().string(String.valueOf(expectedCredit)));
39 | }
40 |
41 | @Test
42 | public void updateUser() throws Exception {
43 | int userId = 1;
44 | String name = "hilijz";
45 | int expectedCredit = 100;
46 | given(this.userService.updateUser(any(User.class))).willReturn(true);
47 | String path = "$.success";
48 | mvc.perform(get("/user/{id}/{name}", userId,name)).andExpect(jsonPath(path).value(true));
49 | }
50 | }
--------------------------------------------------------------------------------
/ch09-test/src/test/java/com/zccoder/boot2/ch9/test/db/UserDbTest.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch9.test.db;
2 |
3 | import static org.junit.Assert.*;
4 |
5 | import com.zccoder.boot2.ch9.test.dao.UserDao;
6 | import com.zccoder.boot2.ch9.test.entity.User;
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 | import org.springframework.beans.factory.annotation.Autowired;
10 | import org.springframework.boot.test.context.SpringBootTest;
11 | import org.springframework.test.context.ActiveProfiles;
12 | import org.springframework.test.context.jdbc.Sql;
13 | import org.springframework.test.context.junit4.SpringRunner;
14 | import org.springframework.transaction.annotation.Transactional;
15 |
16 | import com.zccoder.boot2.ch9.test.service.UserService;
17 |
18 | @RunWith(SpringRunner.class)
19 | @SpringBootTest
20 | @ActiveProfiles("test")
21 | @Transactional
22 | public class UserDbTest {
23 |
24 | @Autowired
25 | UserService userService;
26 |
27 | @Autowired
28 | UserDao userDao;
29 |
30 | @Test
31 | @Sql({"classpath:com/zccoder/boot2/ch9/test/db/user.sql"}) //初始化一条主键为1的用户数据
32 | public void upateNameTest(){
33 |
34 | User user = new User();
35 | user.setId(1);
36 | user.setName("hello123");
37 | boolean success = userService.updateUser(user);
38 | User dbUser = userDao.unique(1);
39 | assertEquals(dbUser.getName(),"hello123");
40 |
41 | }
42 |
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/ch09-test/src/test/java/com/zccoder/boot2/ch9/test/db/user.sql:
--------------------------------------------------------------------------------
1 | INSERT INTO `user` (id,`name`, `department_id`) VALUES (1,'lijz', '1');
--------------------------------------------------------------------------------
/ch09-test/src/test/java/com/zccoder/boot2/ch9/test/mock/CreditServiceMockTest.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch9.test.mock;
2 |
3 | import static org.junit.Assert.assertEquals;
4 | import static org.mockito.AdditionalMatchers.lt;
5 | import static org.mockito.ArgumentMatchers.eq;
6 | import static org.mockito.Mockito.doThrow;
7 | import static org.mockito.Mockito.mock;
8 | import static org.mockito.Mockito.times;
9 | import static org.mockito.Mockito.verify;
10 | import static org.mockito.Mockito.when;
11 |
12 | import java.io.IOException;
13 | import java.util.List;
14 |
15 | import com.zccoder.boot2.ch9.test.service.CreditSystemService;
16 | import org.junit.Assert;
17 | import org.junit.Test;
18 | import org.junit.runner.RunWith;
19 | import org.mockito.junit.MockitoJUnitRunner;
20 |
21 | @RunWith(MockitoJUnitRunner.class)
22 | public class CreditServiceMockTest {
23 | @Test
24 | public void test() throws IOException {
25 |
26 |
27 | int userId = 10;
28 | // 创建mock对象
29 | CreditSystemService creditService = mock(CreditSystemService.class);
30 | // 模拟mock对象调用
31 | when(creditService.getUserCredit(eq(userId))).thenReturn(1000);
32 | // 实际调用
33 | int ret = creditService.getUserCredit(userId);
34 | //注释如下行,单元测试会失败
35 | creditService.getUserCredit(userId);
36 | // 比较期望值和返回值
37 | assertEquals(1000, ret);
38 | verify(creditService, times(2)).getUserCredit(eq(userId));
39 |
40 | }
41 |
42 | @Test
43 | public void test2() {
44 | int userId = 10;
45 | // 创建mock对象
46 | CreditSystemService creditService = mock(CreditSystemService.class);
47 | // 模拟mock对象调用
48 | when(creditService.getUserCredit(eq(userId))).thenReturn(1000);
49 | when(creditService.getUserCredit(lt(0))).thenThrow(new IllegalArgumentException("userId不能小于0"));
50 |
51 | try{
52 | // 实际调用
53 | int ret = creditService.getUserCredit(10);
54 | Assert.assertEquals(1000, ret);
55 | ret = creditService.getUserCredit(-1);
56 | Assert.fail();
57 | }catch(IllegalArgumentException e){
58 | //应该抛出异常
59 | }
60 |
61 |
62 |
63 |
64 |
65 | }
66 |
67 | @Test
68 | public void test3() {
69 |
70 | // 创建mock对象
71 | List list = mock(List.class);
72 | doThrow(new UnsupportedOperationException("不支持clear方法调用")).when(list).clear();;
73 | try {
74 | list.clear();
75 |
76 | }catch(UnsupportedOperationException x) {
77 | return ;
78 | }
79 | Assert.fail();
80 |
81 |
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/ch09-test/src/test/java/com/zccoder/boot2/ch9/test/service/UserServiceTest.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch9.test.service;
2 |
3 | import static org.junit.Assert.assertEquals;
4 | import static org.junit.Assert.assertTrue;
5 | import static org.mockito.ArgumentMatchers.anyInt;
6 | import static org.mockito.BDDMockito.given;
7 |
8 | import com.zccoder.boot2.ch9.test.entity.User;
9 | import com.zccoder.boot2.ch9.test.service.CreditSystemService;
10 | import com.zccoder.boot2.ch9.test.service.UserService;
11 | import org.junit.Test;
12 | import org.junit.runner.RunWith;
13 | import org.springframework.beans.factory.annotation.Autowired;
14 | import org.springframework.boot.test.context.SpringBootTest;
15 | import org.springframework.boot.test.mock.mockito.MockBean;
16 | import org.springframework.test.context.junit4.SpringRunner;
17 | import org.springframework.transaction.annotation.Transactional;
18 |
19 | @RunWith(SpringRunner.class)
20 | @SpringBootTest
21 | @Transactional
22 | public class UserServiceTest {
23 |
24 | @Autowired
25 | UserService userService;
26 |
27 | @MockBean
28 | private CreditSystemService creditSystemService;
29 |
30 | @Test
31 | public void testService() {
32 |
33 |
34 | int userId = 10;
35 | int expectedCredit = 100;
36 | given(this.creditSystemService.getUserCredit(anyInt())).willReturn(expectedCredit);
37 | int credit = userService.getCredit(10);
38 | assertEquals(expectedCredit, credit);
39 | }
40 |
41 | @Test
42 | public void testUpdateUser() {
43 |
44 | User user = new User();
45 | user.setId(1);
46 | user.setName("ok22223343");
47 | boolean ret = userService.updateUser(user);
48 | assertTrue(ret);
49 |
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/ch09-test/src/test/resources/application-test.properties:
--------------------------------------------------------------------------------
1 | spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false
2 | spring.datasource.username=root
3 | spring.datasource.password=root
4 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver
5 |
--------------------------------------------------------------------------------
/ch10-rest/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 |
6 | study-boot2
7 | com.zccoder
8 | 1.0.0-SNAPSHOT
9 |
10 |
11 | ch10-rest
12 | jar
13 |
14 |
15 | 2.0.0.RELEASE
16 |
17 |
18 |
19 |
20 |
21 | org.springframework.boot
22 | spring-boot-dependencies
23 | ${spring.boot.version}
24 | pom
25 | import
26 |
27 |
28 |
29 |
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-starter-web
34 |
35 |
36 | org.springframework.boot
37 | spring-boot-devtools
38 | true
39 |
40 |
41 | com.squareup.okhttp3
42 | okhttp
43 | 3.9.1
44 |
45 |
46 | com.ibeetl
47 | beetl-framework-starter
48 | 1.1.40.RELEASE
49 |
50 |
51 |
52 |
53 |
54 |
55 | org.springframework.boot
56 | spring-boot-maven-plugin
57 | ${spring.boot.version}
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/ch10-rest/src/main/java/com/zccoder/boot2/ch10/rest/RestApplication.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch10.rest;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | *
8 | * 标题: 启动类
9 | * 描述: 启动类
10 | * 时间: 2018/09/26
11 | *
12 | * @author zc
13 | */
14 | @SpringBootApplication
15 | public class RestApplication {
16 |
17 | public static void main(String[] args) {
18 | SpringApplication.run(RestApplication.class, args);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/ch10-rest/src/main/java/com/zccoder/boot2/ch10/rest/conf/RestConf.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch10.rest.conf;
2 |
3 | import org.springframework.boot.web.client.RestTemplateCustomizer;
4 | import org.springframework.context.annotation.Configuration;
5 | import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
6 | import org.springframework.web.client.RestTemplate;
7 |
8 | /**
9 | *
10 | * 标题: Rest配置
11 | * 描述: Rest配置
12 | * 时间: 2018/09/26
13 | *
14 | * @author zc
15 | */
16 | @Configuration
17 | public class RestConf implements RestTemplateCustomizer {
18 |
19 | @Override
20 | public void customize(RestTemplate restTemplate) {
21 | OkHttp3ClientHttpRequestFactory okHttp = (OkHttp3ClientHttpRequestFactory) restTemplate.getRequestFactory();
22 | okHttp.setReadTimeout(5000);
23 | okHttp.setWriteTimeout(3000);
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/ch10-rest/src/main/java/com/zccoder/boot2/ch10/rest/controller/OrderApi2Controller.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch10.rest.controller;
2 |
3 | import javax.servlet.http.HttpServletRequest;
4 | import javax.servlet.http.HttpServletResponse;
5 |
6 | import org.beetl.ext.simulate.WebSimulate;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.web.bind.annotation.RequestMapping;
9 | import org.springframework.web.bind.annotation.RestController;
10 |
11 | /**
12 | *
13 | * 标题: V2 接口
14 | * 描述: V2 接口
15 | * 时间: 2018/09/26
16 | *
17 | * @author zc
18 | */
19 | @RestController
20 | @RequestMapping("/api/v2/")
21 | public class OrderApi2Controller {
22 |
23 | @Autowired
24 | private WebSimulate webSimulate;
25 |
26 | @RequestMapping("/**")
27 | public void simulateJson(HttpServletRequest request, HttpServletResponse response) {
28 | webSimulate.execute(request, response);
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/ch10-rest/src/main/java/com/zccoder/boot2/ch10/rest/controller/OrderApiController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch10.rest.controller;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.springframework.web.bind.annotation.DeleteMapping;
7 | import org.springframework.web.bind.annotation.GetMapping;
8 | import org.springframework.web.bind.annotation.PathVariable;
9 | import org.springframework.web.bind.annotation.PostMapping;
10 | import org.springframework.web.bind.annotation.PutMapping;
11 | import org.springframework.web.bind.annotation.RequestBody;
12 | import org.springframework.web.bind.annotation.RequestMapping;
13 | import org.springframework.web.bind.annotation.RestController;
14 |
15 | import com.zccoder.boot2.ch10.rest.entity.Order;
16 |
17 | /**
18 | *
19 | * 标题: V1 接口
20 | * 描述: V1 接口
21 | * 时间: 2018/09/26
22 | *
23 | * @author zc
24 | */
25 | @RestController
26 | @RequestMapping("/api/v1")
27 | public class OrderApiController {
28 |
29 | @GetMapping("/order/{orderId}")
30 | public Order getOrder(@PathVariable String orderId) throws Exception {
31 | Order order = new Order();
32 | order.setId("O170501-" + orderId);
33 | return order;
34 | }
35 |
36 | @GetMapping("/order")
37 | public List getOrder(Integer offset) throws Exception {
38 | Order order = new Order();
39 | order.setId("O170501-1");
40 | List list = new ArrayList();
41 | list.add(order);
42 | return list;
43 | }
44 |
45 | @PostMapping("/order")
46 | public String addOrder(@RequestBody Order order) throws Exception {
47 | return "{\"success\":true,\"message\":\"添加成功\"}";
48 | }
49 |
50 |
51 | @PostMapping("/orders")
52 | public String batchAdd(@RequestBody List order) throws Exception {
53 | return "{success:true,message:\"批量添加成功\"}";
54 | }
55 |
56 | @PutMapping("/order")
57 | public String updateOrder(Order order) throws Exception {
58 | return "{\"success\":true,\"message\":\"更新成功\"}";
59 | }
60 |
61 |
62 | @DeleteMapping("/order/{orderId}")
63 | public String cancelOrder(@PathVariable() String orderId) throws Exception {
64 | return "{\"success\":true,\"message\":\"订单取消成功\"}";
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/ch10-rest/src/main/java/com/zccoder/boot2/ch10/rest/entity/Order.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch10.rest.entity;
2 |
3 | import java.io.Serializable;
4 | import java.util.Date;
5 |
6 | /**
7 | *
8 | * 标题: 订单实体
9 | * 描述: 订单实体
10 | * 时间: 2018/09/26
11 | *
12 | * @author zc
13 | */
14 | public class Order implements Serializable {
15 |
16 | private static final long serialVersionUID = 687873238202700359L;
17 | private String id;
18 | private String name;
19 | private Date createDate;
20 |
21 | public String getId() {
22 | return id;
23 | }
24 |
25 | public void setId(String id) {
26 | this.id = id;
27 | }
28 |
29 | public String getName() {
30 | return name;
31 | }
32 |
33 | public void setName(String name) {
34 | this.name = name;
35 | }
36 |
37 | public Date getCreateDate() {
38 | return createDate;
39 | }
40 |
41 | public void setCreateDate(Date createDate) {
42 | this.createDate = createDate;
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/ch10-rest/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | # 可以参考zookeeper一章学会服务注册,而不是写死访问地址,或者使用spring cloud
2 | api.order=http://127.0.0.1:8080/api/v1
--------------------------------------------------------------------------------
/ch10-rest/src/main/resources/static/swagger3/favicon-16x16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zccodere/study-boot2/2118ce215c52abc1991aa72747f907206d319970/ch10-rest/src/main/resources/static/swagger3/favicon-16x16.png
--------------------------------------------------------------------------------
/ch10-rest/src/main/resources/static/swagger3/favicon-32x32.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zccodere/study-boot2/2118ce215c52abc1991aa72747f907206d319970/ch10-rest/src/main/resources/static/swagger3/favicon-32x32.png
--------------------------------------------------------------------------------
/ch10-rest/src/main/resources/static/swagger3/sample.json:
--------------------------------------------------------------------------------
1 | {
2 | "swagger": "2.0",
3 | "info": {
4 | "description": "这是一个项目简单实例",
5 | "version": "1.0",
6 | "title": "某系统接口",
7 |
8 | },
9 |
10 | "basePath": "/api/v1",
11 |
12 | "paths": {
13 | "/order/{orderId}": {
14 | "get": {
15 |
16 | "summary": "获取订单详细信息",
17 | "description": "传入订单编号,获取订单信息",
18 |
19 | "parameters": [
20 | {
21 | "name": "orderId",
22 | "in": "path",
23 | "description": "订单id",
24 | "required": true,
25 |
26 | },
27 | {
28 | "name": "username",
29 | "in": "params",
30 | "description": "订单id",
31 | "required": true,
32 |
33 | }
34 | ],
35 | "responses": {
36 | "200": {
37 | "description": "获取用户信息成功"
38 |
39 | }
40 | }
41 |
42 | }
43 | },
44 | "/order":{
45 | "post":{
46 | "summary": "创建订单",
47 | "description": "创建一个新的订单",
48 |
49 | "parameters": [
50 | {
51 | "name": "order",
52 | "in": "body",
53 | "description": "包含订单信息的json",
54 | "required": true,
55 | "schema": {
56 | "$ref": "#/definitions/order"
57 | }
58 |
59 | }
60 | ],
61 | "responses": {
62 | "200": {
63 | "description": "创建订单成功"
64 |
65 | }
66 | }
67 | }
68 | }
69 |
70 | },
71 | "definitions": {
72 | "order": {
73 | "type": "object",
74 | "properties": {
75 | "id": {
76 | "type": "string",
77 |
78 | },
79 | "name": {
80 | "type": "string",
81 |
82 | }
83 |
84 | }
85 | }
86 | }
87 | }
--------------------------------------------------------------------------------
/ch10-rest/src/main/resources/static/swagger3/sample2.json:
--------------------------------------------------------------------------------
1 | {
2 | "swagger": "2.0",
3 | "info": {
4 | "description": "这是一个项目简单实例",
5 | "version": "1.0",
6 | "title": "某系统接口",
7 |
8 | },
9 |
10 | "basePath": "/api/v1"
11 | }
--------------------------------------------------------------------------------
/ch10-rest/src/main/resources/static/swagger3/swagger-ui-bundle.js.map:
--------------------------------------------------------------------------------
1 | {"version":3,"file":"swagger-ui-bundle.js","sources":["webpack:///swagger-ui-bundle.js"],"mappings":"AAAA;AAu/FA;AA6+FA;;;;;;;;;;;;;;;;;;;;;;;;;;AA0dA;;;;;;AAoIA;AAk7FA;AAmtCA;;;;;AA0uIA;AA+4IA;AAm9FA;AA2rGA;AA8lFA;AA2nFA;AAu9CA;AAyhDA;AAqrCA;AAy4EA;AA8/GA;;;;;;;;;;;;;;AA+mJA;AA4mIA;AAquJA;AAwsHA;AAinGA;AAmiEA;AAy4DA;AAm2DA;AA0nBA;;;;;;AA4iFA;AAk0FA;;;;;AA23CA;AA2qFA;AAw2CA;AAglCA;AA0/CA;AAykFA;AA+1FA;;;;;;;;;AAk4CA;AA2zIA;AAk4DA;AAolDA","sourceRoot":""}
--------------------------------------------------------------------------------
/ch10-rest/src/main/resources/static/swagger3/swagger-ui-standalone-preset.js.map:
--------------------------------------------------------------------------------
1 | {"version":3,"file":"swagger-ui-standalone-preset.js","sources":["webpack:///swagger-ui-standalone-preset.js"],"mappings":"AAAA;;;;;AA4QA;AAitGA","sourceRoot":""}
--------------------------------------------------------------------------------
/ch10-rest/src/main/resources/static/swagger3/swagger-ui.css.map:
--------------------------------------------------------------------------------
1 | {"version":3,"file":"swagger-ui.css","sources":[],"mappings":"","sourceRoot":""}
--------------------------------------------------------------------------------
/ch10-rest/src/main/resources/static/swagger3/swagger-ui.js.map:
--------------------------------------------------------------------------------
1 | {"version":3,"file":"swagger-ui.js","sources":["webpack:///swagger-ui.js"],"mappings":"AAAA;;;;;;AA4wCA;AAoyHA;AAuxHA;AAy4FA;AA8rCA;AAugCA;AA+hCA;AA24BA","sourceRoot":""}
--------------------------------------------------------------------------------
/ch10-rest/src/main/resources/templates/values/api/v2/order/$$.var:
--------------------------------------------------------------------------------
1 | var orderId = pathVars[0];
2 | var json = {"id":"abc",name:"订单12"+orderId};
--------------------------------------------------------------------------------
/ch11-mongodb/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 |
6 | study-boot2
7 | com.zccoder
8 | 1.0.0-SNAPSHOT
9 |
10 |
11 | ch11-mongodb
12 | jar
13 |
14 |
15 | 2.0.0.RELEASE
16 |
17 |
18 |
19 |
20 |
21 | org.springframework.boot
22 | spring-boot-dependencies
23 | ${spring.boot.version}
24 | pom
25 | import
26 |
27 |
28 |
29 |
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-starter-web
34 |
35 |
36 | org.springframework.boot
37 | spring-boot-devtools
38 | true
39 |
40 |
41 | org.springframework.boot
42 | spring-boot-starter-data-mongodb
43 |
44 |
45 |
46 |
47 |
48 |
49 | org.springframework.boot
50 | spring-boot-maven-plugin
51 | ${spring.boot.version}
52 |
53 |
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/ch11-mongodb/src/main/java/com/zccoder/boot2/ch11/mongodb/MongodbApplication.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch11.mongodb;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | *
8 | * 标题: 启动类
9 | * 描述: 启动类
10 | * 时间: 2018/09/28
11 | *
12 | * @author zc
13 | */
14 | @SpringBootApplication
15 | public class MongodbApplication {
16 |
17 | public static void main(String[] args) {
18 | SpringApplication.run(MongodbApplication.class, args);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/ch11-mongodb/src/main/java/com/zccoder/boot2/ch11/mongodb/controller/DriverTestController.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch11.mongodb.controller;
2 |
3 | import org.bson.Document;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.dao.DataAccessException;
6 | import org.springframework.data.mongodb.core.DbCallback;
7 | import org.springframework.data.mongodb.core.MongoTemplate;
8 | import org.springframework.web.bind.annotation.GetMapping;
9 | import org.springframework.web.bind.annotation.PathVariable;
10 | import org.springframework.web.bind.annotation.RequestMapping;
11 | import org.springframework.web.bind.annotation.RestController;
12 |
13 | import com.zccoder.boot2.ch11.mongodb.entity.Baike;
14 | import com.mongodb.MongoException;
15 | import com.mongodb.client.MongoCollection;
16 | import com.mongodb.client.MongoCursor;
17 | import com.mongodb.client.MongoDatabase;
18 |
19 | /**
20 | *
21 | * 标题: 驱动测试Controller
22 | * 描述: 驱动测试Controller
23 | * 时间: 2018/09/28
24 | *
25 | * @author zc
26 | */
27 | @RestController
28 | @RequestMapping("/driver")
29 | public class DriverTestController {
30 | @Autowired
31 | private MongoTemplate mongoTemplate;
32 |
33 | @GetMapping("/baike/{name}")
34 | public Baike findUser(@PathVariable String name) {
35 | final String id = name;
36 | Baike baike = mongoTemplate.execute(new DbCallback() {
37 | @Override
38 | public Baike doInDB(MongoDatabase db) throws MongoException, DataAccessException {
39 | MongoCollection collection = db.getCollection("baike");
40 | MongoCursor cursor = collection.find(new Document("_id", id)).iterator();
41 | try {
42 | while (cursor.hasNext()) {
43 | System.out.println(cursor.next().toJson());
44 | }
45 | } finally {
46 | cursor.close();
47 | }
48 | return null;
49 | }
50 | });
51 |
52 | return baike;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/ch11-mongodb/src/main/java/com/zccoder/boot2/ch11/mongodb/entity/Baike.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch11.mongodb.entity;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Date;
5 | import java.util.List;
6 |
7 | /**
8 | *
9 | * 标题: 百科
10 | * 描述: 百科
11 | * 时间: 2018/09/28
12 | *
13 | * @author zc
14 | */
15 | public class Baike {
16 |
17 | private String id;
18 | private String desc;
19 | private List tag = new ArrayList();
20 | private Comment comment = null;
21 | private Date crateDate = null;
22 | private Date updateDate = null;
23 | private int status = 0;
24 |
25 | public String getId() {
26 | return id;
27 | }
28 |
29 | public void setId(String id) {
30 | this.id = id;
31 | }
32 |
33 | public String getDesc() {
34 | return desc;
35 | }
36 |
37 | public void setDesc(String desc) {
38 | this.desc = desc;
39 | }
40 |
41 | public List getTag() {
42 | return tag;
43 | }
44 |
45 | public void setTag(List tag) {
46 | this.tag = tag;
47 | }
48 |
49 | public Comment getComment() {
50 | return comment;
51 | }
52 |
53 | public void setComment(Comment comment) {
54 | this.comment = comment;
55 | }
56 |
57 | public Date getCrateDate() {
58 | return crateDate;
59 | }
60 |
61 | public void setCrateDate(Date crateDate) {
62 | this.crateDate = crateDate;
63 | }
64 |
65 | public Date getUpdateDate() {
66 | return updateDate;
67 | }
68 |
69 | public void setUpdateDate(Date updateDate) {
70 | this.updateDate = updateDate;
71 | }
72 |
73 |
74 | }
75 |
--------------------------------------------------------------------------------
/ch11-mongodb/src/main/java/com/zccoder/boot2/ch11/mongodb/entity/Comment.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch11.mongodb.entity;
2 |
3 | /**
4 | *
5 | * 标题: 评论
6 | * 描述: 评论
7 | * 时间: 2018/09/28
8 | *
9 | * @author zc
10 | */
11 | public class Comment {
12 | private int good;
13 | private int bad;
14 |
15 | public int getGood() {
16 | return good;
17 | }
18 |
19 | public void setGood(int good) {
20 | this.good = good;
21 | }
22 |
23 | public int getBad() {
24 | return bad;
25 | }
26 |
27 | public void setBad(int bad) {
28 | this.bad = bad;
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/ch11-mongodb/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.data.mongodb.uri=mongodb://test:123%25abc!@127.0.0.1:27017/baike
2 | logging.level.org.springframework.data=debug
--------------------------------------------------------------------------------
/ch11-mongodb/src/main/resources/xxx.btl:
--------------------------------------------------------------------------------
1 | <%
2 | var a = 1;
3 | var b = getDbValue("客户表","盈利");
4 | if(c==2){
5 | print(a*23).ccc;
6 | }
7 |
8 | %>
--------------------------------------------------------------------------------
/ch12-redis/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 |
6 | study-boot2
7 | com.zccoder
8 | 1.0.0-SNAPSHOT
9 |
10 |
11 | ch12-redis
12 | jar
13 |
14 |
15 | 2.0.0.RELEASE
16 |
17 |
18 |
19 |
20 |
21 | org.springframework.boot
22 | spring-boot-dependencies
23 | ${spring.boot.version}
24 | pom
25 | import
26 |
27 |
28 |
29 |
30 |
31 | org.springframework.boot
32 | spring-boot-starter-web
33 |
34 |
35 | org.springframework.boot
36 | spring-boot-devtools
37 | true
38 |
39 |
40 | org.springframework.boot
41 | spring-boot-starter-data-redis
42 |
43 |
44 |
45 |
46 |
47 |
48 | org.springframework.boot
49 | spring-boot-maven-plugin
50 | ${spring.boot.version}
51 |
52 |
53 |
54 |
55 |
56 |
--------------------------------------------------------------------------------
/ch12-redis/src/main/java/com/zccoder/boot2/ch12/redis/RedisApplication.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch12.redis;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | *
8 | * 标题: 启动类
9 | * 描述: 启动类
10 | * 时间: 2018/09/28
11 | *
12 | * @author zc
13 | */
14 | @SpringBootApplication
15 | public class RedisApplication {
16 |
17 | public static void main(String[] args) {
18 | SpringApplication.run(RedisApplication.class, args);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/ch12-redis/src/main/java/com/zccoder/boot2/ch12/redis/conf/RedisChannelListenerConf.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch12.redis.conf;
2 |
3 | import java.io.UnsupportedEncodingException;
4 |
5 | import org.springframework.context.annotation.Bean;
6 | import org.springframework.context.annotation.Configuration;
7 | import org.springframework.data.redis.connection.Message;
8 | import org.springframework.data.redis.connection.MessageListener;
9 | import org.springframework.data.redis.connection.RedisConnectionFactory;
10 | import org.springframework.data.redis.listener.PatternTopic;
11 | import org.springframework.data.redis.listener.RedisMessageListenerContainer;
12 | import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
13 | import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
14 |
15 | /**
16 | *
17 | * 标题: Redis消息监听
18 | * 描述: Redis消息监听
19 | * 时间: 2018/09/28
20 | *
21 | * @author zc
22 | */
23 | @Configuration
24 | public class RedisChannelListenerConf {
25 |
26 | @Bean
27 | MessageListenerAdapter listenerAdapter() {
28 | MessageListenerAdapter adapter = new MessageListenerAdapter(new MyRedisChannelListener());
29 | adapter.setSerializer(new JdkSerializationRedisSerializer());
30 | return adapter;
31 |
32 | }
33 |
34 | @Bean
35 | RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
36 | MessageListenerAdapter listenerAdapter) {
37 | RedisMessageListenerContainer container = new RedisMessageListenerContainer();
38 | container.setConnectionFactory(connectionFactory);
39 | //订阅所有news.* 频道内容
40 | container.addMessageListener(listenerAdapter, new PatternTopic("news.*"));
41 | return container;
42 | }
43 | }
44 |
45 | class MyRedisChannelListener implements MessageListener {
46 |
47 | @Override
48 | public void onMessage(Message message, byte[] pattern) {
49 | byte[] channal = message.getChannel();
50 | byte[] bs = message.getBody();
51 | try {
52 | String content = new String(bs, "UTF-8");
53 | String p = new String(channal, "UTF-8");
54 | System.out.println("get " + content + " from " + p);
55 | } catch (UnsupportedEncodingException e) {
56 | e.printStackTrace();
57 | }
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/ch12-redis/src/main/java/com/zccoder/boot2/ch12/redis/conf/RedisConfig.java:
--------------------------------------------------------------------------------
1 | package com.zccoder.boot2.ch12.redis.conf;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 | import org.springframework.data.redis.connection.RedisConnectionFactory;
6 | import org.springframework.data.redis.core.RedisTemplate;
7 | import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
8 | import org.springframework.data.redis.serializer.RedisSerializer;
9 | import org.springframework.data.redis.serializer.StringRedisSerializer;
10 |
11 | /**
12 | *
13 | * 标题: Redis配置
14 | * 描述: Redis配置
15 | * 时间: 2018/09/28
16 | *
17 | * @author zc
18 | */
19 | @Configuration
20 | public class RedisConfig {
21 | @Bean("jsonRedisTemplate")
22 | public RedisTemplate