Commonly used annotations in springBoot development

A programmer may encounter various algorithms in his life, but there are always a few algorithms that a programmer will definitely encounter and need to master with a high probability. Let’s talk about these very important “must catch!” algorithms today~

 

I. Introduction

 Among springboot developers, the most common ones are Controller, Service, Mapper, and entity. Now each layer uses annotations to modify classes or objects. Common annotations are: @RestController @RequestMapping @Autowired @ApiOperation @PostMapping @RequestBody @Service @Transactional @Repository @TableName @Data So these annotations encapsulate those algorithms.  

Two: Introduction to Common Algorithms

@RestController 

  • @RestControllerNote, equivalent to @ResponseBody + @Controller
  • 使用@RestController批注对控制器类进行注释, 不再需要将@ResponseBody添加到所有请求映射方法中。@ResponseBody注释默认处于活动状态。

    说明它同时拥有这2注解的功能,@Controller注解表示后,该类将会被spring管理,@ResponseBody注解标识后,响应数据可以是文本或者JSON数据类型。所以这里我们return map的时候,可以直接输出JSON数据。当然@RestController注解属于springmvc的,并不属于springboot的。

@RequestMapping 

Use @RequestMapping to map requests in Spring MVC, which is to specify which URL requests the controller can handle, which is equivalent to configuring in web.xml in Servlet


@Autowired 

It can mark class member variables, methods and constructors, and let spring complete the work of bean automatic assembly.


@ApiOperation

Used to build Api documents, @ApiOperation(value = "interface description", httpMethod = "interface request method", response = "
interface return parameter type", notes = "interface release description"
 
 
@PostMapping  

Is a composite annotation, Spring framework 4.3 introduced a variant of the @RequestMapping annotation to better represent the semantics of the annotated method, as a shortcut for @RequestMapping(method = RequestMethod.POST). That is to say, it can be simplified to @PostMapping(value="/abc" ), mainly for the convenience of memorization.
 

 
@RequestBody 

@RequestBody is mainly used to receive the data in the json string passed from the front end to the backend (data in the request body); and the most commonly used request body to pass parameters is undoubtedly the POST request, so when using @RequestBody to receive data , usually submitted by POST. In the same receiving method of the backend, @RequestBody and @RequestParam() can be used at the same time, @RequestBody can only have one at most, and @RequestParam() can have multiple.
 A request has only one RequestBody; a request can have multiple RequestParams.

 
@Service 

The @Service annotation is used on the class to mark the current class as a service class. Adding this annotation will automatically inject the current class into the spring container, and there is no need to define beans in the applicationContext.xml file.

 
@Transactional  

The @Transactional annotation is an annotation provided by the Spring framework for declarative transaction management. It can be applied to methods or classes to identify methods or classes that require transaction management. By using the @Transactional annotation, we can manage transactions more conveniently and ensure data consistency and reliability. Before introducing the @Transactional annotation, let's first understand the basic concept of transactions. Transactions have four basic properties: atomicity, consistency, isolation, and durability.
 

 
@Repository 

@Repository is a Spring annotation used to declare a Bean.

 
@TableName

@TableName is an annotation in mybatis-plus, mainly to implement mapping between entity types and tables in the database.


@Data

The main function of the @Data annotation is to improve the simplicity of the code. Using this annotation can save a lot of get(), set(), toString() and other methods in the code;

 

Three: Algorithm Summary

 springboot startup process and sequence

Startup process: initialize a SpringApplication object and execute the run method of the object.

The first part is the initialization module of SpringApplication, and configures some basic environment variables, resources, constructors, and listeners.
The second part implements the specific startup scheme of the application, including the monitoring module of the startup process, the loading configuration environment module, and the core creation context module.
The third part is the automatic configuration module. This module serves as the core of springboot automatic configuration.

The annotation of spring is actually the configuration that encapsulates the xml file. Annotation is meant to be a kind of identification, a kind of mark. Indicates that it has been configured.

 Annotation list 

1. @SpringBootApplication:  = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan 组合注解

1.1. @Configuration: Equivalent to spring's XML configuration file.

1.2 @EnableAutoConfiguration: Enable automatic configuration (core annotation).


2. The @RestController annotation is a collection of @Controller and @ResponseBody, indicating that this is a controller bean, and it is a REST-style controller that directly fills the return value of the function into the HTTP response body.

1.4 @Component can be used with CommandLineRunner to perform some basic tasks after the program starts.

Time formatting in entity

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date createTime;

 

Guess you like

Origin blog.csdn.net/dongjing991/article/details/132403586