@@ -141,11 +141,11 @@ public class UserController {
141
141
142
142
#### 2.3. ` @RestController `
143
143
144
- ` @RestController ` 注解是` @Controller和 ` @ ` ResponseBody ` 的合集,表示这是个控制器 bean,并且是将函数的返回值直 接填入 HTTP 响应体中,是 REST 风格的控制器。
144
+ ` @RestController ` 注解是` @Controller ` 和 ` @ ResponseBody` 的合集,表示这是个控制器 bean,并且是将函数的返回值直接填入 HTTP 响应体中,是 REST 风格的控制器。
145
145
146
146
_ Guide 哥:现在都是前后端分离,说实话我已经很久没有用过` @Controller ` 。如果你的项目太老了的话,就当我没说。_
147
147
148
- 单独使用 ` @Controller ` 不加 ` @ResponseBody ` 的话一般使用在要返回一个视图的情况 ,这种情况属于比较传统的 Spring MVC 的应用,对应于前后端不分离的情况。` @Controller ` +` @ResponseBody ` 返回 JSON 或 XML 形式数据
148
+ 单独使用 ` @Controller ` 不加 ` @ResponseBody ` 的话一般是用在要返回一个视图的情况 ,这种情况属于比较传统的 Spring MVC 的应用,对应于前后端不分离的情况。` @Controller ` +` @ResponseBody ` 返回 JSON 或 XML 形式数据
149
149
150
150
关于` @RestController ` 和 ` @Controller ` 的对比,请看这篇文章:[ @RestController vs @Controller ] ( https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=2247485544&idx=1&sn=3cc95b88979e28fe3bfe539eb421c6d8&chksm=cea247a3f9d5ceb5e324ff4b8697adc3e828ecf71a3468445e70221cce768d1e722085359907&token=1725092312&lang=zh_CN#rd ) 。
151
151
@@ -213,7 +213,7 @@ public ResponseEntity<List<User>> getAllUsers() {
213
213
``` java
214
214
@PostMapping (" /users" )
215
215
public ResponseEntity<User > createUser(@Valid @RequestBody UserCreateRequest userCreateRequest) {
216
- return userRespository. save(user );
216
+ return userRespository. save(userCreateRequest );
217
217
}
218
218
```
219
219
@@ -271,7 +271,7 @@ public List<Teacher> getKlassRelatedTeachers(
271
271
}
272
272
```
273
273
274
- 如果我们请求的 url 是:` /klasses/{ 123456} /teachers?type=web `
274
+ 如果我们请求的 url 是:` /klasses/123456/teachers?type=web `
275
275
276
276
那么我们服务获取到的数据就是:` klassId=123456,type=web ` 。
277
277
@@ -325,7 +325,7 @@ public class UserRegisterRequest {
325
325
326
326
** 下面我们来看一下 Spring 为我们提供了哪些方式帮助我们从配置文件中读取这些配置信息。**
327
327
328
- 我们的数据源` application.yml ` 内容如下::
328
+ 我们的数据源` application.yml ` 内容如下:
329
329
330
330
``` yaml
331
331
wuhan2020 : 2020年初武汉爆发了新型冠状病毒,疫情严重,但是,我相信一切都会过去!武汉加油!中国加油!
@@ -345,7 +345,7 @@ library:
345
345
description : 如何养成一个新习惯?如何让心智变得更成熟?如何拥有高质量的关系? 如何走出人生的艰难时刻?
346
346
` ` `
347
347
348
- #### 5.1. ` @value `(常用)
348
+ #### 5.1. ` @Value `(常用)
349
349
350
350
使用 `@Value("${property}")` 读取比较简单的配置信息:
351
351
@@ -380,7 +380,7 @@ class LibraryProperties {
380
380
381
381
你可以像使用普通的 Spring bean 一样,将其注入到类中使用。
382
382
383
- # ### 5.3. `PropertySource`(不常用)
383
+ # ### 5.3. `@ PropertySource`(不常用)
384
384
385
385
` @PropertySource` 读取指定 properties 文件
386
386
@@ -432,7 +432,7 @@ SpringBoot 项目的 spring-boot-starter-web 依赖中已经有 hibernate-valida
432
432
- ` @DecimalMin(value)` 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
433
433
- ` @DecimalMax(value)` 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
434
434
- ` @Size(max=, min=)` 被注释的元素的大小必须在指定的范围内
435
- - ` @Digits (integer, fraction)` 被注释的元素必须是一个数字,其值必须在可接受的范围内
435
+ - ` @Digits(integer, fraction)` 被注释的元素必须是一个数字,其值必须在可接受的范围内
436
436
- ` @Past` 被注释的元素必须是一个过去的日期
437
437
- ` @Future` 被注释的元素必须是一个将来的日期
438
438
- ......
@@ -479,7 +479,7 @@ public class PersonController {
479
479
480
480
# ### 6.3. 验证请求参数(Path Variables 和 Request Parameters)
481
481
482
- **一定一定不要忘记在类上加上 `Validated` 注解了,这个参数可以告诉 Spring 去校验方法参数。**
482
+ **一定一定不要忘记在类上加上 `@ Validated` 注解了,这个参数可以告诉 Spring 去校验方法参数。**
483
483
484
484
` ` ` java
485
485
@RestController
@@ -562,7 +562,7 @@ public class Role {
562
562
private Long id;
563
563
` ` `
564
564
565
- JPA 使用枚举定义了 4 中常见的主键生成策略 ,如下:
565
+ JPA 使用枚举定义了 4 种常见的主键生成策略 ,如下:
566
566
567
567
_Guide 哥:枚举替代常量的一种用法_
568
568
@@ -674,7 +674,7 @@ private String userName;
674
674
设置字段类型并且加默认值,这个还是挺常用的。
675
675
676
676
` ` ` java
677
- Column(columnDefinition = "tinyint(1) default 1")
677
+ @ Column(columnDefinition = "tinyint(1) default 1")
678
678
private Boolean enabled;
679
679
` ` `
680
680
@@ -685,7 +685,7 @@ private Boolean enabled;
685
685
如果我们想让`secrect` 这个字段不被持久化,可以使用 `@Transient`关键字声明。
686
686
687
687
` ` ` java
688
- Entity(name="USER")
688
+ @ Entity(name="USER")
689
689
public class User {
690
690
691
691
......
@@ -699,7 +699,7 @@ public class User {
699
699
700
700
` ` ` java
701
701
static String secrect; // not persistent because of static
702
- final String secrect = “ Satish” ; // not persistent because of final
702
+ final String secrect = " Satish" ; // not persistent because of final
703
703
transient String secrect; // not persistent because of transient
704
704
` ` `
705
705
@@ -718,7 +718,7 @@ private String content;
718
718
719
719
` ` ` java
720
720
@Lob
721
- //指定 Lob 类型数据的获取策略, FetchType.EAGER 表示非延迟 加载 ,而 FetchType. LAZY 表示延迟加载 ;
721
+ //指定 Lob 类型数据的获取策略, FetchType.EAGER 表示非延迟加载 ,而 FetchType.LAZY 表示延迟加载 ;
722
722
@Basic(fetch = FetchType.EAGER)
723
723
//columnDefinition 属性指定数据表对应的 Lob 字段类型
724
724
@Column(name = "content", columnDefinition = "LONGTEXT NOT NULL")
@@ -756,7 +756,7 @@ public class Role {
756
756
}
757
757
` ` `
758
758
759
- 数据库里面对应存储的是 MAIL/FEMAIL 。
759
+ 数据库里面对应存储的是 MALE/FEMALE 。
760
760
761
761
# ### 8.7. 增加审计功能
762
762
@@ -808,9 +808,9 @@ public class AuditSecurityConfiguration {
808
808
}
809
809
` ` `
810
810
811
- 简单介绍一下上面设计到的一些注解 :
811
+ 简单介绍一下上面涉及到的一些注解 :
812
812
813
- 1. `@CreatedDate` : 表示该字段为创建时间时间字段 ,在这个实体被 insert 的时候,会设置值
813
+ 1. `@CreatedDate` : 表示该字段为创建时间字段 ,在这个实体被 insert 的时候,会设置值
814
814
2. `@CreatedBy` :表示该字段为创建人,在这个实体被 insert 的时候,会设置值
815
815
816
816
` @LastModifiedDate` 、`@LastModifiedBy`同理。
@@ -835,8 +835,8 @@ public interface UserRepository extends JpaRepository<User, Integer> {
835
835
836
836
- ` @OneToOne` 声明一对一关系
837
837
- ` @OneToMany` 声明一对多关系
838
- - ` @ManyToOne` 声明多对一关系
839
- - ` MangToMang` 声明多对多关系
838
+ - ` @ManyToOne` 声明多对一关系
839
+ - ` @ MangToMang` 声明多对多关系
840
840
841
841
更多关于 Spring Boot JPA 的文章请看我的这篇文章:[一文搞懂如何在 Spring Boot 正确中使用 JPA](https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=2247485689&idx=1&sn=061b32c2222869932be5631fb0bb5260&chksm=cea24732f9d5ce24a356fb3675170e7843addbfcc79ee267cfdb45c83fc7e90babf0f20d22e1&token=292197051&lang=zh_CN#rd) 。
842
842
@@ -852,14 +852,14 @@ public void save() {
852
852
853
853
` ` `
854
854
855
- 我们知道 Exception 分为运行时异常 RuntimeException 和非运行时异常。在`@Transactional`注解中如果不配置`rollbackFor`属性,那么事物只会在遇到 `RuntimeException`的时候才会回滚,加上`rollbackFor=Exception.class`,可以让事物在遇到非运行时异常时也回滚 。
855
+ 我们知道 Exception 分为运行时异常 RuntimeException 和非运行时异常。在`@Transactional`注解中如果不配置`rollbackFor`属性,那么事务只会在遇到 `RuntimeException`的时候才会回滚,加上`rollbackFor=Exception.class`,可以让事务在遇到非运行时异常时也回滚 。
856
856
857
- ` @Transactional` 注解一般用在可以作用在 `类`或者`方法`上。
857
+ ` @Transactional` 注解一般可以作用在 `类`或者`方法`上。
858
858
859
- - **作用于类**:当把`@Transactional 注解放在类上时,表示所有该类的` public 方法都配置相同的事务属性信息。
859
+ - **作用于类**:当把`@Transactional` 注解放在类上时,表示所有该类的 public 方法都配置相同的事务属性信息。
860
860
- **作用于方法**:当类配置了`@Transactional`,方法也配置了`@Transactional`,方法的事务会覆盖类的事务配置信息。
861
861
862
- 更多关于关于 Spring 事务的内容请查看:
862
+ 更多关于 Spring 事务的内容请查看:
863
863
864
864
1. [可能是最漂亮的 Spring 事务管理详解](https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=2247484943&idx=1&sn=46b9082af4ec223137df7d1c8303ca24&chksm=cea249c4f9d5c0d2b8212a17252cbfb74e5fbe5488b76d829827421c53332326d1ec360f5d63&token=1082669959&lang=zh_CN#rd)
865
865
2. [一口气说出 6 种 @Transactional 注解失效场景](https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=2247486483&idx=2&sn=77be488e206186803531ea5d7164ec53&chksm=cea243d8f9d5cacecaa5c5daae4cde4c697b9b5b21f96dfc6cce428cfcb62b88b3970c26b9c2&token=816772476&lang=zh_CN#rd)
@@ -878,7 +878,6 @@ public class User {
878
878
private String userName;
879
879
private String fullName;
880
880
private String password;
881
- @JsonIgnore
882
881
private List<UserRole> userRoles = new ArrayList<>();
883
882
}
884
883
` ` `
@@ -900,7 +899,7 @@ public class User {
900
899
901
900
# ### 10.2. 格式化 json 数据
902
901
903
- ` @JsonFormat` 一般用来格式化 json 数据。:
902
+ ` @JsonFormat` 一般用来格式化 json 数据。
904
903
905
904
比如:
906
905
@@ -916,9 +915,7 @@ private Date date;
916
915
@Setter
917
916
@ToString
918
917
public class Account {
919
- @JsonUnwrapped
920
918
private Location location;
921
- @JsonUnwrapped
922
919
private PersonInfo personInfo;
923
920
924
921
@Getter
0 commit comments