【SpringBoot】Spring常用注解总结

 

目录

 

⭐spring springmvc和springboot的区别

@Autowired 和@Resource的区别和联系

1. @SpringBootApplication

2. Spring Bean 相关

2.1. @Autowired

2.2. @Component,@Repository,@Service, @Controller

2.3. @RestController

2.4. @Scope

2.5. @Configuration

3. 处理常见的 HTTP 请求类型

3.1. GET 请求

3.2. POST 请求

3.3. PUT 请求

3.4. DELETE 请求

3.5. PATCH 请求

4. 前后端传值

4.1. @PathVariable 和 @RequestParam

4.2. @RequestBody

5. 读取配置信息

5.1. @value(常用)

5.2. @ConfigurationProperties(常用)

5.3. PropertySource(不常用)

6. 参数校验

6.1. 一些常用的字段验证的注解

6.2. 验证请求体(RequestBody)

6.3. 验证请求参数(Path Variables 和 Request Parameters)

7. 全局处理 Controller 层异常

8. JPA 相关

8.1. 创建表

8.2. 创建主键

8.3. 设置字段类型

8.4. 指定不持久化特定字段

8.5. 声明大字段

8.6. 创建枚举类型的字段

8.7. 增加审计功能

8.8. 删除/修改数据

8.9. 关联关系

9. 事务 @Transactional

10. json 数据处理

10.1. 过滤 json 数据

10.2. 格式化 json 数据

10.3. 扁平化对象

11. 测试相关


可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景。对于每一个注解我都说了具体用法,掌握搞懂,使用 SpringBoot 来开发项目基本没啥大问题了!

为什么要写这篇文章?

最近看到网上有一篇关于 SpringBoot 常用注解的文章被转载的比较多,我看了文章内容之后属实觉得质量有点低,并且有点会误导没有太多实际使用经验的人(这些人又占据了大多数)。所以,自己索性花了大概 两天时间简单总结一下了。

因为我个人的能力和精力有限,如果有任何不对或者需要完善的地方,请帮忙指出!Guide 哥感激不尽!

⭐spring springmvc和springboot的区别

  • SpringBoot 的自动配置机制

@Autowired 和@Resource的区别和联系

  • Resource 按照名称进行匹配 Autowired 按照类型进行匹配

  • Autowired 是spring的注解 Resource是java的一个注解

1. @SpringBootApplication

这里先单独拎出@SpringBootApplication 注解说一下,虽然我们一般不会主动去使用它。

Guide 哥:这个注解是 Spring Boot 项目的基石,创建 SpringBoot 项目之后会默认在主类加上。

@SpringBootApplication
public class SpringSecurityJwtGuideApplication {public static void main(java.lang.String[] args) {SpringApplication.run(SpringSecurityJwtGuideApplication.class, args);}
}

我们可以把 @SpringBootApplication看作是 @Configuration@EnableAutoConfiguration@ComponentScan 注解的集合。

package org.springframework.boot.autoconfigure;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {......
}
​
package org.springframework.boot;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
​
}
根据 SpringBoot 官网,这三个注解的作用分别是:
  • @EnableAutoConfiguration:启用 SpringBoot 的自动配置机制

  • @ComponentScan: 扫描被@Component (@Service,@Controller)注解的 bean,注解默认会扫描该类所在的包下所有的类。

  • @Configuration:允许在 Spring 上下文中注册额外的 bean 或导入其他配置类

2. Spring Bean 相关

2.1. @Autowired

自动导入对象到类中,被注入进的类同样要被 Spring 容器管理比如:Service 类注入到 Controller 类中。

@Service
public class UserService {......
}
​
@RestController
@RequestMapping("/users")
public class UserController {@Autowiredprivate UserService userService;......
}
2.2. @Component,@Repository,@Service, @Controller

我们一般使用 @Autowired 注解让 Spring 容器帮我们自动装配 bean。要想把类标识成可用于 @Autowired 注解自动装配的 bean 的类,可以采用以下注解实现:

  • @Component :通用的注解,可标注任意类为 Spring 组件。如果一个 Bean 不知道属于哪个层,可以使用@Component 注解标注。

  • @Repository : 对应持久层即 Dao 层,主要用于数据库相关操作。

  • @Service : 对应服务层,主要涉及一些复杂的逻辑,需要用到 Dao 层。

  • @Controller : 对应 Spring MVC 控制层,主要用于接受用户请求并调用 Service 层返回数据给前端页面。

2.3. @RestController

@RestController注解是@Controller和@ResponseBody的合集,表示这是个控制器 bean,并且是将函数的返回值直 接填入 HTTP 响应体中,是 REST 风格的控制器。

Guide 哥:现在都是前后端分离,说实话我已经很久没有用过@Controller。如果你的项目太老了的话,就当我没说。

单独使用 @Controller 不加 @ResponseBody的话一般使用在要返回一个视图的情况,这种情况属于比较传统的 Spring MVC 的应用,对应于前后端不分离的情况。@Controller +@ResponseBody 返回 JSON 或 XML 形式数据

关于@RestController@Controller的对比,请看这篇文章:@RestController vs @Controller。

2.4. @Scope

声明 Spring Bean 的作用域,使用方法:

@Bean
@Scope("singleton")
public Person personSingleton() {return new Person();
}

四种常见的 Spring Bean 的作用域:

  • singleton : 唯一 bean 实例,Spring 中的 bean 默认都是单例的。

  • prototype : 每次请求都会创建一个新的 bean 实例。

  • request : 每一次 HTTP 请求都会产生一个新的 bean,该 bean 仅在当前 HTTP request 内有效。

  • session : 每一次 HTTP 请求都会产生一个新的 bean,该 bean 仅在当前 HTTP session 内有效。

2.5. @Configuration

一般用来声明配置类,可以使用 @Component注解替代,不过使用@Configuration注解声明配置类更加语义化。

@Configuration
public class AppConfig {@Beanpublic TransferService transferService() {return new TransferServiceImpl();}
​
}

3. 处理常见的 HTTP 请求类型

5 种常见的请求类型:

  • GET :请求从服务器获取特定资源。举个例子:GET /users(获取所有学生)

  • POST :在服务器上创建一个新的资源。举个例子:POST /users(创建学生)

  • PUT :更新服务器上的资源(客户端提供更新后的整个资源)。举个例子:PUT /users/12(更新编号为 12 的学生)

  • DELETE :从服务器删除特定的资源。举个例子:DELETE /users/12(删除编号为 12 的学生)

  • PATCH :更新服务器上的资源(客户端提供更改的属性,可以看做作是部分更新),使用的比较少,这里就不举例子了。

3.1. GET 请求
@GetMapping("users") 等价于@RequestMapping(value="/users",method=RequestMethod.GET)@GetMapping("/users")
public ResponseEntity<List<User>> getAllUsers() {return userRepository.findAll();
}
3.2. POST 请求

@PostMapping("users") 等价于@RequestMapping(value="/users",method=RequestMethod.POST)

关于@RequestBody注解的使用,在下面的“前后端传值”这块会讲到。

@PostMapping("/users")
public ResponseEntity<User> createUser(@Valid @RequestBody UserCreateRequest userCreateRequest) {return userRespository.save(user);
}
3.3. PUT 请求

@PutMapping("/users/{userId}") 等价于@RequestMapping(value="/users/{userId}",method=RequestMethod.PUT)

@PutMapping("/users/{userId}")
public ResponseEntity<User> updateUser(@PathVariable(value = "userId") Long userId,@Valid @RequestBody UserUpdateRequest userUpdateRequest) {......
}
3.4. DELETE 请求

@DeleteMapping("/users/{userId}")等价于@RequestMapping(value="/users/{userId}",method=RequestMethod.DELETE)

@DeleteMapping("/users/{userId}")
public ResponseEntity deleteUser(@PathVariable(value = "userId") Long userId){......
}
3.5. PATCH 请求

一般实际项目中,我们都是 PUT 不够用了之后才用 PATCH 请求去更新数据。

  @PatchMapping("/profile")public ResponseEntity updateStudent(@RequestBody StudentUpdateRequest studentUpdateRequest) {studentRepository.updateDetail(studentUpdateRequest);return ResponseEntity.ok().build();}

4. 前后端传值

掌握前后端传值的正确姿势,是你开始 CRUD 的第一步!

4.1. @PathVariable@RequestParam

@PathVariable用于获取路径参数,@RequestParam用于获取查询参数。

举个简单的例子:

@GetMapping("/klasses/{klassId}/teachers")
public List<Teacher> getKlassRelatedTeachers(@PathVariable("klassId") Long klassId,@RequestParam(value = "type", required = false) String type ) {
...
}

如果我们请求的 url 是:/klasses/{123456}/teachers?type=web

那么我们服务获取到的数据就是:klassId=123456,type=web

4.2. @RequestBody

用于读取 Request 请求(可能是 POST,PUT,DELETE,GET 请求)的 body 部分并且Content-Type 为 application/json 格式的数据,接收到数据之后会自动将数据绑定到 Java 对象上去。系统会使用HttpMessageConverter或者自定义的HttpMessageConverter将请求的 body 中的 json 字符串转换为 java 对象。

我用一个简单的例子来给演示一下基本使用!

我们有一个注册的接口:

@PostMapping("/sign-up")
public ResponseEntity signUp(@RequestBody @Valid UserRegisterRequest userRegisterRequest) {userService.save(userRegisterRequest);return ResponseEntity.ok().build();
}

UserRegisterRequest对象:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserRegisterRequest {@NotBlankprivate String userName;@NotBlankprivate String password;@NotBlankprivate String fullName;
}

我们发送 post 请求到这个接口,并且 body 携带 JSON 数据:

{"userName":"coder","fullName":"shuangkou","password":"123456"}

这样我们的后端就可以直接把 json 格式的数据映射到我们的 UserRegisterRequest 类上。

 

👉 需要注意的是:一个请求方法只可以有一个@RequestBody,但是可以有多个@RequestParam@PathVariable。 如果你的方法必须要用两个 @RequestBody来接受数据的话,大概率是你的数据库设计或者系统设计出问题了!

5. 读取配置信息

很多时候我们需要将一些常用的配置信息比如阿里云 oss、发送短信、微信认证的相关配置信息等等放到配置文件中。

下面我们来看一下 Spring 为我们提供了哪些方式帮助我们从配置文件中读取这些配置信息。

我们的数据源application.yml内容如下::

wuhan2020: 2020年初武汉爆发了新型冠状病毒,疫情严重,但是,我相信一切都会过去!武汉加油!中国加油!
​
my-profile:name: Guide哥email: koushuangbwcx@163.com
​
library:location: 湖北武汉加油中国加油books:- name: 天才基本法description: 二十二岁的林朝夕在父亲确诊阿尔茨海默病这天,得知自己暗恋多年的校园男神裴之即将出国深造的消息——对方考取的学校,恰是父亲当年为她放弃的那所。- name: 时间的秩序description: 为什么我们记得过去,而非未来?时间“流逝”意味着什么?是我们存在于时间之内,还是时间存在于我们之中?卡洛·罗韦利用诗意的文字,邀请我们思考这一亘古难题——时间的本质。- name: 了不起的我description: 如何养成一个新习惯?如何让心智变得更成熟?如何拥有高质量的关系? 如何走出人生的艰难时刻?
5.1. @value(常用)

使用 @Value("${property}") 读取比较简单的配置信息:

@Value("${wuhan2020}")
String wuhan2020;
5.2. @ConfigurationProperties(常用)

通过@ConfigurationProperties读取配置信息并与 bean 绑定。

@Component
@ConfigurationProperties(prefix = "library")
class LibraryProperties {@NotEmptyprivate String location;private List<Book> books;
​@Setter@Getter@ToStringstatic class Book {String name;String description;}省略getter/setter......
}

你可以像使用普通的 Spring bean 一样,将其注入到类中使用。

5.3. PropertySource(不常用)

@PropertySource读取指定 properties 文件

@Component
@PropertySource("classpath:website.properties")
​
class WebSite {@Value("${url}")private String url;
​省略getter/setter......
}

更多内容请查看我的这篇文章:《10 分钟搞定 SpringBoot 如何优雅读取配置文件?》 。

6. 参数校验

数据的校验的重要性就不用说了,即使在前端对数据进行校验的情况下,我们还是要对传入后端的数据再进行一遍校验,避免用户绕过浏览器直接通过一些 HTTP 工具直接向后端请求一些违法数据。

JSR(Java Specification Requests) 是一套 JavaBean 参数校验的标准,它定义了很多常用的校验注解,我们可以直接将这些注解加在我们 JavaBean 的属性上面,这样就可以在需要校验的时候进行校验了,非常方便!

校验的时候我们实际用的是 Hibernate Validator 框架。Hibernate Validator 是 Hibernate 团队最初的数据校验框架,Hibernate Validator 4.x 是 Bean Validation 1.0(JSR 303)的参考实现,Hibernate Validator 5.x 是 Bean Validation 1.1(JSR 349)的参考实现,目前最新版的 Hibernate Validator 6.x 是 Bean Validation 2.0(JSR 380)的参考实现。

SpringBoot 项目的 spring-boot-starter-web 依赖中已经有 hibernate-validator 包,不需要引用相关依赖。如下图所示(通过 idea 插件—Maven Helper 生成):

非 SpringBoot 项目需要自行引入相关依赖包,这里不多做讲解,具体可以查看我的这篇文章:《如何在 Spring/Spring Boot 中做参数校验?你需要了解的都在这里!》。

👉 需要注意的是: 所有的注解,推荐使用 JSR 注解,即javax.validation.constraints,而不是org.hibernate.validator.constraints

6.1. 一些常用的字段验证的注解
  • @NotEmpty 被注释的字符串的不能为 null 也不能为空

  • @NotBlank 被注释的字符串非 null,并且必须包含一个非空白字符

  • @Null 被注释的元素必须为 null

  • @NotNull 被注释的元素必须不为 null

  • @AssertTrue 被注释的元素必须为 true

  • @AssertFalse 被注释的元素必须为 false

  • @Pattern(regex=,flag=)被注释的元素必须符合指定的正则表达式

  • @Email 被注释的元素必须是 Email 格式。

  • @Min(value)被注释的元素必须是一个数字,其值必须大于等于指定的最小值

  • @Max(value)被注释的元素必须是一个数字,其值必须小于等于指定的最大值

  • @DecimalMin(value)被注释的元素必须是一个数字,其值必须大于等于指定的最小值

  • @DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值

  • @Size(max=, min=)被注释的元素的大小必须在指定的范围内

  • @Digits (integer, fraction)被注释的元素必须是一个数字,其值必须在可接受的范围内

  • @Past被注释的元素必须是一个过去的日期

  • @Future 被注释的元素必须是一个将来的日期

  • ......

6.2. 验证请求体(RequestBody)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
​@NotNull(message = "classId 不能为空")private String classId;
​@Size(max = 33)@NotNull(message = "name 不能为空")private String name;
​@Pattern(regexp = "((^Man$|^Woman$|^UGM$))", message = "sex 值不在可选范围")@NotNull(message = "sex 不能为空")private String sex;
​@Email(message = "email 格式不正确")@NotNull(message = "email 不能为空")private String email;
​
}

我们在需要验证的参数上加上了@Valid注解,如果验证失败,它将抛出MethodArgumentNotValidException

@RestController
@RequestMapping("/api")
public class PersonController {
​@PostMapping("/person")public ResponseEntity<Person> getPerson(@RequestBody @Valid Person person) {return ResponseEntity.ok().body(person);}
}
6.3. 验证请求参数(Path Variables 和 Request Parameters)

一定一定不要忘记在类上加上 Validated 注解了,这个参数可以告诉 Spring 去校验方法参数。

@RestController
@RequestMapping("/api")
@Validated
public class PersonController {
​@GetMapping("/person/{id}")public ResponseEntity<Integer> getPersonByID(@Valid @PathVariable("id") @Max(value = 5,message = "超过 id 的范围了") Integer id) {return ResponseEntity.ok().body(id);}
}

 

7. 全局处理 Controller 层异常

介绍一下我们 Spring 项目必备的全局处理 Controller 层异常。

相关注解:

  1. @ControllerAdvice :注解定义全局异常处理类

  2. @ExceptionHandler :注解声明异常处理方法

如何使用呢?拿我们在第 5 节参数校验这块来举例子。如果方法参数不对的话就会抛出MethodArgumentNotValidException,我们来处理这个异常。

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
​/*** 请求参数异常处理*/@ExceptionHandler(MethodArgumentNotValidException.class)public ResponseEntity<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex, HttpServletRequest request) {......}
}

 

8. JPA 相关

8.1. 创建表

@Entity声明一个类对应一个数据库实体。

@Table 设置表名

@Entity
@Table(name = "role")
public class Role {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;private String description;省略getter/setter......
}
8.2. 创建主键

@Id :声明一个字段为主键。

使用@Id声明之后,我们还需要定义主键的生成策略。我们可以使用 @GeneratedValue 指定主键生成策略。

1.通过 @GeneratedValue直接使用 JPA 内置提供的四种主键生成策略来指定主键生成策略。

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

JPA 使用枚举定义了 4 中常见的主键生成策略,如下:

 

public enum GenerationType {
​/*** 使用一个特定的数据库表格来保存主键* 持久化引擎通过关系数据库的一张特定的表格来生成主键,*/TABLE,
​/***在某些数据库中,不支持主键自增长,比如Oracle、PostgreSQL其提供了一种叫做"序列(sequence)"的机制生成主键*/SEQUENCE,
​/*** 主键自增长*/IDENTITY,
​/***把主键生成策略交给持久化引擎(persistence engine),*持久化引擎会根据数据库在以上三种主键生成 策略中选择其中一种*/AUTO
}

@GeneratedValue注解默认使用的策略是GenerationType.AUTO

public @interface GeneratedValue {
​GenerationType strategy() default AUTO;String generator() default "";
}

一般使用 MySQL 数据库的话,使用GenerationType.IDENTITY策略比较普遍一点(分布式系统的话需要另外考虑使用分布式 ID)。

2.通过 @GenericGenerator声明一个主键策略,然后 @GeneratedValue使用这个策略

@Id
@GeneratedValue(generator = "IdentityIdGenerator")
@GenericGenerator(name = "IdentityIdGenerator", strategy = "identity")
private Long id;

等价于:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

jpa 提供的主键生成策略有如下几种:

public class DefaultIdentifierGeneratorFactoryimplements MutableIdentifierGeneratorFactory, Serializable, ServiceRegistryAwareService {
​@SuppressWarnings("deprecation")public DefaultIdentifierGeneratorFactory() {register( "uuid2", UUIDGenerator.class );register( "guid", GUIDGenerator.class );            // can be done with UUIDGenerator + strategyregister( "uuid", UUIDHexGenerator.class );         // "deprecated" for new useregister( "uuid.hex", UUIDHexGenerator.class );     // uuid.hex is deprecatedregister( "assigned", Assigned.class );register( "identity", IdentityGenerator.class );register( "select", SelectGenerator.class );register( "sequence", SequenceStyleGenerator.class );register( "seqhilo", SequenceHiLoGenerator.class );register( "increment", IncrementGenerator.class );register( "foreign", ForeignGenerator.class );register( "sequence-identity", SequenceIdentityGenerator.class );register( "enhanced-sequence", SequenceStyleGenerator.class );register( "enhanced-table", TableGenerator.class );}
​public void register(String strategy, Class generatorClass) {LOG.debugf( "Registering IdentifierGenerator strategy [%s] -> [%s]", strategy, generatorClass.getName() );final Class previous = generatorStrategyToClassNameMap.put( strategy, generatorClass );if ( previous != null ) {LOG.debugf( "    - overriding [%s]", previous.getName() );}}
​
}
8.3. 设置字段类型

@Column 声明字段。

示例:

设置属性 userName 对应的数据库字段名为 user_name,长度为 32,非空

@Column(name = "user_name", nullable = false, length=32)
private String userName;

设置字段类型并且加默认值,这个还是挺常用的。

Column(columnDefinition = "tinyint(1) default 1")
private Boolean enabled;
8.4. 指定不持久化特定字段

@Transient :声明不需要与数据库映射的字段,在保存的时候不需要保存进数据库 。

如果我们想让secrect 这个字段不被持久化,可以使用 @Transient关键字声明。

Entity(name="USER")
public class User {
​......@Transientprivate String secrect; // not persistent because of @Transient
​
}

除了 @Transient关键字声明, 还可以采用下面几种方法:

static String secrect; // not persistent because of static
final String secrect = “Satish”; // not persistent because of final
transient String secrect; // not persistent because of transient

一般使用注解的方式比较多。

8.5. 声明大字段

@Lob:声明某个字段为大字段。

@Lob
private String content;

更详细的声明:

@Lob
//指定 Lob 类型数据的获取策略, FetchType.EAGER 表示非延迟 加载,而 FetchType. LAZY 表示延迟加载 ;
@Basic(fetch = FetchType.EAGER)
//columnDefinition 属性指定数据表对应的 Lob 字段类型
@Column(name = "content", columnDefinition = "LONGTEXT NOT NULL")
private String content;
8.6. 创建枚举类型的字段

可以使用枚举类型的字段,不过枚举字段要用@Enumerated注解修饰。

public enum Gender {MALE("男性"),FEMALE("女性");
​private String value;Gender(String str){value=str;}
}
@Entity
@Table(name = "role")
public class Role {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;private String description;@Enumerated(EnumType.STRING)private Gender gender;省略getter/setter......
}

数据库里面对应存储的是 MAIL/FEMAIL。

8.7. 增加审计功能

只要继承了 AbstractAuditBase的类都会默认加上下面四个字段。

@Data
@AllArgsConstructor
@NoArgsConstructor
@MappedSuperclass
@EntityListeners(value = AuditingEntityListener.class)
public abstract class AbstractAuditBase {
​@CreatedDate@Column(updatable = false)@JsonIgnoreprivate Instant createdAt;
​@LastModifiedDate@JsonIgnoreprivate Instant updatedAt;
​@CreatedBy@Column(updatable = false)@JsonIgnoreprivate String createdBy;
​@LastModifiedBy@JsonIgnoreprivate String updatedBy;
}

我们对应的审计功能对应地配置类可能是下面这样的(Spring Security 项目):

@Configuration
@EnableJpaAuditing
public class AuditSecurityConfiguration {@BeanAuditorAware<String> auditorAware() {return () -> Optional.ofNullable(SecurityContextHolder.getContext()).map(SecurityContext::getAuthentication).filter(Authentication::isAuthenticated).map(Authentication::getName);}
}

简单介绍一下上面设计到的一些注解:

  1. @CreatedDate: 表示该字段为创建时间时间字段,在这个实体被 insert 的时候,会设置值

  2. @CreatedBy :表示该字段为创建人,在这个实体被 insert 的时候,会设置值

    @LastModifiedDate@LastModifiedBy同理。

@EnableJpaAuditing:开启 JPA 审计功能。

8.8. 删除/修改数据

@Modifying 注解提示 JPA 该操作是修改操作,注意还要配合@Transactional注解使用。

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
​@Modifying@Transactional(rollbackFor = Exception.class)void deleteByUserName(String userName);
}
8.9. 关联关系
  • @OneToOne 声明一对一关系

  • @OneToMany 声明一对多关系

  • @ManyToOne声明多对一关系

  • MangToMang声明多对多关系

更多关于 Spring Boot JPA 的文章请看我的这篇文章:一文搞懂如何在 Spring Boot 正确中使用 JPA 。

9. 事务 @Transactional

在要开启事务的方法上使用@Transactional注解即可!

@Transactional(rollbackFor = Exception.class)
public void save() {......
}

我们知道 Exception 分为运行时异常 RuntimeException 和非运行时异常。在@Transactional注解中如果不配置rollbackFor属性,那么事物只会在遇到RuntimeException的时候才会回滚,加上rollbackFor=Exception.class,可以让事物在遇到非运行时异常时也回滚。

@Transactional 注解一般用在可以作用在或者方法上。

  • 作用于类:当把@Transactional 注解放在类上时,表示所有该类的public 方法都配置相同的事务属性信息。

  • 作用于方法:当类配置了@Transactional,方法也配置了@Transactional,方法的事务会覆盖类的事务配置信息。

 

10. json 数据处理

10.1. 过滤 json 数据

@JsonIgnoreProperties 作用在类上用于过滤掉特定字段不返回或者不解析。

//生成json时将userRoles属性过滤
@JsonIgnoreProperties({"userRoles"})
public class User {
​private String userName;private String fullName;private String password;@JsonIgnoreprivate List<UserRole> userRoles = new ArrayList<>();
}

@JsonIgnore一般用于类的属性上,作用和上面的@JsonIgnoreProperties 一样。

public class User {
​private String userName;private String fullName;private String password;//生成json时将userRoles属性过滤@JsonIgnoreprivate List<UserRole> userRoles = new ArrayList<>();
}
10.2. 格式化 json 数据

@JsonFormat一般用来格式化 json 数据。:

比如:

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", timezone="GMT")
private Date date;
10.3. 扁平化对象
@Getter
@Setter
@ToString
public class Account {@JsonUnwrappedprivate Location location;@JsonUnwrappedprivate PersonInfo personInfo;
​@Getter@Setter@ToStringpublic static class Location {private String provinceName;private String countyName;}@Getter@Setter@ToStringpublic static class PersonInfo {private String userName;private String fullName;}
}

未扁平化之前:

{"location": {"provinceName":"湖北","countyName":"武汉"},"personInfo": {"userName": "coder1234","fullName": "shaungkou"}
}

使用@JsonUnwrapped 扁平对象之后:

@Getter
@Setter
@ToString
public class Account {@JsonUnwrappedprivate Location location;@JsonUnwrappedprivate PersonInfo personInfo;......
}
{"provinceName":"湖北","countyName":"武汉","userName": "coder1234","fullName": "shaungkou"
}

11. 测试相关

@ActiveProfiles一般作用于测试类上, 用于声明生效的 Spring 配置文件。

@SpringBootTest(webEnvironment = RANDOM_PORT)
@ActiveProfiles("test")
@Slf4j
public abstract class TestBase {......
}

@Test声明一个方法为测试方法

@Transactional被声明的测试方法的数据会回滚,避免污染测试数据。

@WithMockUser Spring Security 提供的,用来模拟一个真实用户,并且可以赋予权限。

    @Test@Transactional@WithMockUser(username = "user-id-18163138155", authorities = "ROLE_TEACHER")void should_import_student_success() throws Exception {......}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/2806209.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

CentOS和Ubuntu之间的区别和联系

CentOS&#xff08;Community ENTerprise Operating System&#xff09;和Ubuntu是两种流行的Linux发行版&#xff0c;它们在企业和个人用户中都有广泛的应用。尽管它们都是基于Linux内核&#xff0c;但它们在设计理念、更新策略、包管理系统等方面存在一些关键的区别和联系。下…

力扣 724. 寻找数组的中心下标

思路&#xff1a; 创建两个变量sum和sum1&#xff0c;sum代表左边元素的和&#xff0c;sum1代表右边元素的和 然后假设从数组下标0开始&#xff0c;一直到最后一个作为中心下标 如果sumsum1&#xff0c;返回此时的中心下标 如果所有下标循环完了&#xff0c;发现没有return…

Sqli-labs靶场第8关详解[Sqli-labs-less-8]

Sqli-labs-Less-8 前言&#xff1a; SQL注入的三个条件&#xff1a; ①参数可控&#xff1b;&#xff08;从参数输入就知道参数可控&#xff09; ②参数过滤不彻底导致恶意代码被执行&#xff1b;&#xff08;需要在测试过程中判断&#xff09; ③参数带入数据库执行。&#…

Pytorch安装如何使用命令确认CUDA版本

Pytorch安装如何使用命令确认CUDA版本 一、NVIDIA版本确认命令解析二、Pytorch对应的NVIDIA版本选择 欢迎学习交流&#xff01; 邮箱&#xff1a; z…1…6.com 网站&#xff1a; https://zephyrhours.github.io/ 一、NVIDIA版本确认命令解析 在使用深度学习的Pytorch库时&…

关于字符集(彻底搞清楚一个中文占几个字节?)

目录 一、字符集二、ASCII码(字符编码)三、ISO-8859-1(字符集)四、GBxxx(字符集)五、Unicode码(字符集)六、UTF-8(字符编码)总结 一、字符集 编码与解码 计算机中储存的信息都是用二进制数表示的而我们在屏幕上看到的数字、英文、标点符号、汉字等字符是二进制数转换之后的结…

【踩坑】 修复报错 No module named ‘Crypto‘

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhang.cn] 这个直接使用pip安装Crypto是没有用的&#xff0c;网上说的装pycrypto实际上也没有用。 真正需要这样装&#xff1a; pip uninstall crypto pip uninstall pycrypto pip install pycryptodome 再运行就可以用…

Axtue使用笔记

1、有三种方式可以设置元件顺序 第一种是鼠标右键点击顺序&#xff0c;选择调整操作置顶、置底、上移一层、下移一层&#xff1b; 第二种是在顶部工具栏中&#xff0c;选择调整操作置顶、置底、上移一层、下移一层; 第三种是使用快捷键操作 Windows&#xff1a;置顶&#xff1a…

设计模式篇---观察者模式

文章目录 概念结构实例总结 概念 观察者模式&#xff1a;定义对象之间的一种一对多的依赖关系&#xff0c;使得每当一个对象状态发生改变时&#xff0c;其他相关依赖对象都得到通知并被自动更新。 观察者模式是使用频率较高的一个模式&#xff0c;它建立了对象与对象之间的依赖…

Git的基本操作和原理

目录 写在前面的话 为什么要有Git&#xff08;git初识&#xff09;&#xff1f; Git安装(Centos为例) Git基本操作 创建Git本地仓库 Git配置 认识工作区、暂存区、版本库 概念认识 添加文件 查看.git文件 修改文件 版本回退 撤销修改 情况一&#xff1a;…

MATLAB中对多项式求积分和微分

此示例演示如何使用 polyint 和 polyder 函数对由系数向量表示的任何多项式求解析积分或微分。 使用 polyder 获取多项式 p(x)x^3−2x−5 的导数。生成的多项式为 p [1 0 -2 -5]; q polyder(p) q 133 0 -2同样&#xff0c;使用 polyint 对多项式 p(x)4x^3−3x^21 求…

猫头虎分享已解决Bug || TypeError: props is not a function (React)

博主猫头虎的技术世界 &#x1f31f; 欢迎来到猫头虎的博客 — 探索技术的无限可能&#xff01; 专栏链接&#xff1a; &#x1f517; 精选专栏&#xff1a; 《面试题大全》 — 面试准备的宝典&#xff01;《IDEA开发秘籍》 — 提升你的IDEA技能&#xff01;《100天精通鸿蒙》 …

【Java程序设计】【C00285】基于Springboot的游戏分享网站(有论文)

基于Springboot的游戏分享网站&#xff08;有论文&#xff09; 项目简介项目获取开发环境项目技术运行截图 项目简介 这是一个基于Springboot的游戏分享网站 本系统分为系统功能模块、管理员功能模块以及用户功能模块。 系统功能模块&#xff1a;在网站首页可以查看首页、游戏…

redis的搭建 RabbitMq搭建 Elasticsearch 搭建

官网 Download | Redis wget https://github.com/redis/redis/archive/7.2.4.tar.gz 编译安装 yum install gcc g tar -zxvf redis-7.2.4.tar.gz -C /usr/localcd /usr/local/redis make && make install 常见报错 zmalloc.h:50:10: fatal error: jemalloc/jemal…

[算法沉淀记录] 排序算法 —— 归并排序

排序算法 —— 归并排序 算法介绍 归并排序是一种分治算法&#xff0c;由约翰冯诺伊曼在1945年发明。它的工作原理是将未排序的列表划分为n个子列表&#xff0c;每个子列表包含一个元素(包含一个元素的列表被认为是有序的)&#xff0c;然后重复合并子列表以生成新的有序子列表…

- 工程实践 - 《QPS百万级的有状态服务实践》05 - 持久化存储

本文属于专栏《构建工业级QPS百万级服务》 继续上篇《QPS百万级的有状态服务实践》04 - 服务一致性。目前我们的系统如图1。现在我们虽然已经尽量把相同用户的请求转发到相同的机器&#xff0c;并且在客户端做了适配。但是因为成本&#xff0c;更极端的情况下&#xff0c;服务依…

机器学习 深度学习资料 资源machine learning

Kaggle入门&#xff0c;看这一篇就够了 - 知乎 (zhihu.com)https://zhuanlan.zhihu.com/p/25686876 day1-1.什么是机器学习_哔哩哔哩_bilibiliday1-1.什么是机器学习是10天学会机器学习从入门到深度学习的第1集视频&#xff0c;该合集共计62集&#xff0c;视频收藏或关注UP主&a…

猫头虎分享已解决Bug || ValueError: Setting an array element with a sequence.

博主猫头虎的技术世界 &#x1f31f; 欢迎来到猫头虎的博客 — 探索技术的无限可能&#xff01; 专栏链接&#xff1a; &#x1f517; 精选专栏&#xff1a; 《面试题大全》 — 面试准备的宝典&#xff01;《IDEA开发秘籍》 — 提升你的IDEA技能&#xff01;《100天精通鸿蒙》 …

临时内核映射

临时内核映射与永久内核映射的区别是&#xff0c;临时内核映射可以在中断处理程序和可延迟函数内部使用&#xff0c;它不堵塞当前进程。 一 原理介绍 临时内核映射的线性地址在永久内核映射的后面&#xff0c;范围是[FIXADDR_START, FIXADDR_TOP)&#xff0c;其基本逻辑是获取…

vivo 基于 StarRocks 构建实时大数据分析平台,为业务搭建数据桥梁

在大数据时代&#xff0c;数据分析和处理能力对于企业的决策和发展至关重要。 vivo 作为一家全球移动互联网智能终端公司&#xff0c;需要基于移动终端的制造、物流、销售等各个方面的数据进行分析以满足业务决策。 而随着公司数字化服务的演进&#xff0c;业务诉求和技术架构有…

基于MATLAB优化的多焦点相位

1、概要 目前智能手机的显示屏得益于机械或化学性能的稳定&#xff0c;让这些手机非常耐用&#xff0c;显示屏具有足够硬度使其可以承受住很大的压力&#xff0c;甚至多年使用下来都没有磨损迹象。 但是另一方面&#xff0c;材料的硬度通常伴随着脆性&#xff0c;手机的屏幕玻…