- preamble
- 1, Mybatis commonly used annotations
- 2, SpringMVC commonly used annotations
-
3, Spring commonly used annotations
- 1. IoC annotations
- 2. DI annotations
- 3. Transaction notes
- 4, SpringBoot commonly used annotations
- 5. Lombok annotations
preamble
OOP (Object Oriented Programming), IoC (Inversion of Control), AOP (Cutting Oriented Programming) are all programming ideas, and DI (Dependency Injection) is a concrete implementation of IoC.
1, Mybatis commonly used annotations
- @Select: query operation.
- @Insert: Add operation.
- @Update: Modify operation.
- @Delete: Delete operation.
- @Mapper: Generate a proxy object (implementation class object) for the Mapper interface and put it into the ioc container.
- @MapperScan: Instead of @Mapper on each Mapper interface, just write this, specify the package name of the mapper interface, and generate corresponding proxy objects for all Mapper interfaces under the package, and put them into the ioc container.
2, SpringMVC commonly used annotations
- @RequestMapping: put on method: specify the access path of the interface, query/delete based on id is usually used. Usually, put on class: specify the module name of the interface, usually the module name plus s, e.g. /users.
- @RequestParam: when receiving data in key=value format, the front-end parameter name and the back-end formal parameter name don't match, use this annotation to give the formal parameter an alias, so that the alias is the same as the front-end parameter name.
- @RequestBody: receive the json string sent by the post request, use this annotation.
- @ResponseBody: return a string, if the interface returns a string, return the string as is, if the interface returns an object, the annotation will be converted to a json string, and then return, the annotation needs the support of jackson dependency.
- @PathVariable: used to receive path parameters, for example, the request path is like this: /users/1, the back-end configuration of the access path so that with /users/{id}, the id should be the same as the formal parameter name, not the same as the form of the parameter with the annotation to the form of the alias, so that the alias and the id of the same.
- @GetMapping: get request Query operation use.
- @PostMapping: post request Add/new operation use.
- @PutMapping: put request Modify/update operation use.
- @DeleteMapping: delete request Used by delete operations.
The above four is the rest style of the four notes, usually this is the agreement, depending on how the company requirements, the vast majority of enterprise development or to comply with this agreement.
3, Spring commonly used annotations
1. IoC annotations
- @Controller: put on controller, create controller object, put in ioc container.
- @Service: put on service implementation class, create service object, put in ioc container.
- @Repository: put on mapper implementation class, create mapper object, put in ioc container.
- @Component: put on classes other than controller, service, mapper, create normal objects, put in ioc container.
- @Configuration+@Bean: @Configuration is placed on the class, indicating that the class is a configuration class, which serves the same purpose as an xml configuration file, and @Bean, placed on the method, indicating that the object returned by the method is placed in the ioc container.
- @RestController=@Controller+@ResponseBody so that you don't have to add @ResponseBody to each method, and the backend usually returns a json string, for example:
2. DI annotations
- @Value injects the basic type, String string.
- @Autowired: inject custom objects, third-party objects.
3. Transaction notes
@Transactional: usually placed on the service layer method, indicating that the method to add transactions, the execution of the method before the automatic opening of the transaction, the normal execution of the commit transaction, the emergence of abnormal rollback transactions.Note: When the method throws a runtime exception or error, the default rollback transaction; when the method throws a compile-time exception, the default does not roll back the transaction, which may lead to transaction failure, you can use the rollbackFor attribute to specify which exceptions are also rolled back by the method to roll back the transaction, when trycatch on the exception to catch, the equivalent of the exception is not thrown, and will not roll back the transaction!
4, SpringBoot commonly used annotations
@SpringBootApplication: placed on the class to indicate that this is the startup/boot class for the springboot project and is the entry point for the springboot project.
@SpringBootTest: placed on the class to indicate that it is a unit test class.
5. Lombok annotations
- @NoArgsConstructor: generates a no-args constructor.
- @AllArgsConstructor: generate all-args constructor.
- @Getter: generate get method.
- @Setter: generates the set method.
- @ToString: generate toString method.
- @EqualsAndHashCode: generate equals method and hashCode method.
- @Data: generate get, set, toString, equals, hashCode, etc. method @Data=@Getter+@Setter+@toString+@Equals+@hashCode.
To summarize: when using it, @NoArgsConstructor + @AllArgsConstructor + @Data is enough.