Advantages of using annotations:

1. Adopt pure Java code, no need to configure complex XML files

2. You can also enjoy the benefits of object-oriented in your configuration

3. Type safety supports refactoring well

4. Reduce the complexity of configuration files while enjoying the capabilities of the Spring IOC container

1, annotation detail (equipped with a perfect definition) ——(can use Ctrl +F to search oh)

@SpringBootApplication: states that SpringBoot should automatically configure the program as necessary. This configuration is equivalent to:

@Configuration, @EnableAutoConfiguration and @ComponentScan.

@responseBody: Indicates that the return result of this method is written directly into the HTTP ResponseBody and is typically used when retrieving data asynchronously to build a RESTful API. With @RequestMapping, the return value is usually parsed as the jump path, and with @esponseBody the return result is not parsed as the jump path, but is written directly to the HTTP Response Body. For example, if you get JSON data asynchronously, if you add @responseBody, it will return JSON data directly. This annotation is typically used in conjunction with @RequestMapping.

@Controller: This is used to define the Controller class. In the Spring project, the Controller is responsible for forwarding the URL request from the user to the corresponding service interface (Service layer). Usually this annotation is in the class, and usually the method needs to be matched with the annotation @RequestMapping.

@RestController: Used to annotate a collection of control layer components (such as Action in Struts), @ResponseBody, and @Controller.

@RequestMapping: Provides routing information that is responsible for mapping URLs to specific functions in the Controller.

EnableAutoConfiguration: SpringBoot auto-configuration: Try to automatically configure your Spring application based on the JAR dependencies you add. For example, if you have HSQLDB under your CLASSPATH and you have not manually configured any database connection beans, then we will automatically configure an in-memory database. You can add the @EnableAutoConfiguration or @SpringBootApplication annotation to a @Configuration class to select AutoConfiguration. If you find that specific automatic configuration classes that you don’t want are applied, you can disable them by using the exclusion property of the @EnableAutoConfiguration annotation.

@ComponentScan: Indicates that this class will automatically discover scanned components. If you scan a class with @Component, @Controller, @Service, etc., and register it as a Bean, you can automatically collect all Spring components, including the @Configuration class. We often use the @ComponentScan annotation to search for beans and import in conjunction with the @Autowired annotation. You can automatically collect all Spring components, including the @Configuration class. We often use the @ComponentScan annotation to search for beans and import in conjunction with the @Autowired annotation. If it is not configured, Spring Boot will scan for classes that use @Service,@Repository and other annotations under the Boot package and subpackages.

@Configuration: It is equivalent to a traditional XML Configuration file. If some third-party libraries need XML files, it is recommended to still use the @Configuration class as the main Configuration class for the project — you can load the XML Configuration file using the @ImportResource annotation.

@import: Used to Import other configuration classes.

ImportResource: Used to load the XML configuration file.

Autowired: Automatically imports dependent beans

Service: Typically used to decorate a component in the Service layer

@Repository: Use the @Repository annotation to ensure that exception translations are provided by the DAO or Repositories classes. The DAO or Repositories classes that are modified by this annotation are found and configured by ComponetScan, and there is no need to provide XML configuration items for them.

@Bean: The @Bean annotation method is equivalent to a configured Bean in XML.

@value: Injecting the Value of the property configured by Spring Boot Application.properties. Sample code:

@Inject: Equivalent to the default @Autowired, but without the required attribute;

@Component: A generic reference to a Component. We can use this annotation to indicate when a Component is difficult to categorize.

@bean: The equivalent of XML, placed on top of a method, not a class, meaning that a Bean is generated and handed over to Spring to manage.

Autowired: Automatically imports dependent beans. ByType way. The configured Bean is used to complete the assembly of properties and methods. It can annotate class member variables, methods and constructors to complete the auto-assembly work. When added (required=false), it does not report an error even if the bean is not found.

@Qualifier: When there are multiple beans of the same type, you can specify it with @Qualifier(” name “). Used in conjunction with @Autowired. In addition to name injection, the @Qualifier Qualifier Qualifier provides more granular control over how candidates are selected. It can be used as follows:

@resource (name= “name”,type= “type”) : Default byName if there is no parentheses. Something similar to @Autowired.

The list of comments is as follows

@SpringBootApplication: Includes @ComponentScan, @Configuration, and @EnableAutoConfiguration annotations. Among them

@ComponentScan: Let Spring Boot scan to the Configuration class and add it to the program context.

@Configuration: An XML Configuration file equivalent to Spring; Using Java code, you can check type safety.

EnableAutoConfiguration: Automatic configuration.

@ComponentScan: Component scan to automatically discover and assemble some beans.

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

@RESTController: The annotation is a combination of @Controller and @ResponseBody, indicating that this is a Controller bean and that it is a RESTful Controller that fills the return value of the function directly into the body of the HTTP response.

@Autowired: Auto Import.

@pathVariable: Get the parameters.

@JsonBackReference: Solves the problem of nested outer chains.

@ RepositoryRestResourcepublic: cooperate with spring – the boot – starter – data – rest.

3. JPA annotations

@Entity: @Table(name= “”) : Indicates that this is an Entity class. Generally used in JPA, the two annotations are used together, but if the Table name and entity class name are the same, the @Table annotation can be omitted

MappedSuperClass: Used to identify an Entity that is a parent class. Subclasses can inherit properties from their parent classes.

@NoRepositoryBean: Normally used as a repository for the parent class. Spring will not instantiate this repository with this annotation.

@Column: If the field name is the same as the Column name, you can omit it.

@id: Indicates that this property is the primary key.

@ GeneratedValue (strategy = GenerationType SEQUENCE, the generator = “repair_seq”) : Indicates that the primary key generation policy is SEQUENCE (Auto, IDENTITY, native, etc., Auto means that you can switch between multiple databases), and the name of the sequence specified is REPAIR_SEQ.

@SequenceGenereTor (name = “repair_seq”, SequenceName = “seq_repair”, allocationSize = 1) : Name is the name of the sequence for use, Sequencename is the name of the sequence for the database, and the two names can be consistent.

@transient: Indicates that this attribute is not a mapping to a field in a database table and will be ignored by the ORM framework. If an attribute is not a field map to a database table, it must be marked as @transient; otherwise, the ORM framework defaults to an annotation of @Basic. @basic (FETCH = FETCHTYPE.lazy) : The flag specifies how an entity property should be loaded

@JsonIgnore: This ignores some properties in the Java bean when serializing JSON, affecting both serialization and deserialization.

@JoinColumn (name= “loginID”) : one-to-one: A foreign key that points to another table in this table. One to many: The other table points to the foreign key of the table.

@OneToOne, @OneToAny, @ManyToOne: One to one, OneToMany, many-to-one in a Hibernate configuration file.

4. SpringMVC related annotations

@RequestMapping: @RequestMapping(” /path “) indicates that this controller handles all UR L requests for “/path”. RequestMapping is an annotation that handles the mapping of request addresses and can be used on a class or method. Used on a class to indicate that all methods in the class that respond to requests have that address as the parent path. The annotation has six attributes: params: Specifies that the request must contain some parameter values for this method to handle. HEADERS: Specifies that some specified header value must be included in the request for the method to handle the request. Method: Specifies the Type of Method that is the request. Get, Post, Put, Delete, and so on are the Content-type that is used to handle the request. Such as application/json, text/HTML. Produces: Specifies the content type to be returned, returned only if the specified type is included in the (Accept) type in the request header

@RequestParam: Used before a method parameter. @requestParam String a = Request.getParameter (” a “).

@pathVariable: A PathVariable. Such as

The arguments should be the same as the names in the braces.

Global exception handling

@ControllerAdvice: Contains @Component. It can be scanned. Uniform handling of exceptions.

@ExceptionHandler (Exception.class) : Used on a method to indicate that the following method is executed when this Exception is encountered.


VI. Specific configuration analysis and use environment in the project

@ MappedSuperclass:

The 1.@MappedSuperclass annotation is used on the parent class to identify the parent class

The class identified by 2.@MappedSuperclass indicates that it cannot be mapped to a database table because it is not a full entity class, but that it has properties that can be mapped to the database table that its subclass pairs use

Classes identified by 3.@MappedSuperclass cannot have an @Entity or @Table annotation

@ the Column:

1. The @Column annotation is required when an entity’s attribute has a different name than the Column in the database table it maps to. This attribute is usually placed before the entity’s attribute declaration statement and can also be used with the @ID annotation.

A common attribute for the 2.@Column annotation is name, which is used to set the column name of the mapped database table. In addition, the annotation contains several other attributes, such as unique, nullable, length, precision, and so on. The details are as follows:

1. Name attribute: The name attribute defines the name of the annotated field in the database table

2 UNIQUECONSTRINT attribute: The UNIQUECONSTRINT attribute indicates whether the field is unique or not. Default is false. If a field in the Table requires a unique identifier, you can either use this tag or use @UNIQUECONSTRINT from the @TABLE annotation

Nullable property: The nullable property indicates whether the field can be NULL. Default is true

INSERTABLE attribute: The INSERTABLE attribute indicates whether the value of the field needs to be inserted when data is inserted using the “INSERT” statement

5. Updateable property: The updateable property indicates whether the value of the field needs to be updated when data is inserted using the “UPDATE” statement

Insertable and updateable properties: Typically used for read-only properties, such as primary and foreign keys, these fields are automatically generated

7 ColumnDefinition property: ColumnDefinition property represents the SQL statement created by this field when creating a table. ColumnDefinition property is generally used when generating a table definition through an Entity. If the table in the database has already been created, this property is not necessary

Table property: The table property defines the name of the table containing the current field

9 length attribute: The length attribute represents the length of the field. This attribute is only valid if the field type is VARCHAR, which defaults to 255 characters

10. Precision and Scale attributes: Together, precision and scale represent precision. When the field type is double, precision represents the total length of the number of decimal points, and scale represents the number of decimal points

The details are as follows: If the columnDefinition property specifies that the number type is DECIMAL and the precision is specified, the columnDefinition property specifies that the number type is DECIMAL and the precision is specified. BigDecimal type is mapped to Decimal type in the database, and precision and scale attributes are valid only in BigDecimal type

3.@Column ColumnDefinition property: Represents the actual type of the field in the database. Usually the ORM framework can automatically determine the type of the field in the database according to the attribute type, but for the DATE type, it is still unable to determine whether the field type in the database is DATE,TIME or TIMESTAMP. In addition, the default mapping type for String is VARCHAR, if you want to map a String type to a BLOB or TEXT field type for a particular database.

The 4.@Column annotation can also be placed before the getter method of the property

@getter and @setter (Lombok)

@setter: annotations on attributes; Provide the setting method @getter for the property: annotate the property; Provide the Getting method for the property

1@Data: Annotations on the class; Provides the getting and setting methods for all properties of the class, as well as the equals, canEqual, hashCode, and toString methods 2 3 @setter: annotations on properties; Provide the setting method for the property 4 5@getter: annotate the property; Provide the Getting method for attributes 6 7@log4j2: annotate on the class; Provide a log4j log object with the property log for the class, similar to the @log4j annotation Add a constructor to the class that doesn’t have arguments. 12 13@EqualSandHashCode: By default, all non-transient and non-static fields are used to generate the equals and hascode methods. You can also specify which properties to use. 14 15 @toString: Generates the toString method. By default, the class name and all the properties are printed out in order, separated by commas. If we need to reload more than one constructor, we need to reload more than one constructor. We need to reload more than one constructor. 21 22 @val: Comments on attributes must not be NULL 21 22 @val: Comments on attributes must not be NULL 21 22 @val: Comments on attributes must not be NULL 21 22 @val: Comments on attributes must not be NULL 21 22 @val: Comments on attributes must be set to final

When you execute various persistence methods, the state of the entity changes accordingly, and the state changes trigger different lifecycle events. These events can use different annotation characters to indicate the callback function when it occurs.

@ javax.mail. Persistence. PostLoad: after loading.

Before @ javax.mail. Persistence. PrePersist: persistence.

After @ javax.mail. Persistence. PostPersist: persistence.

@ javax.mail. Persistence. PreUpdate: before updating.

@ javax.mail. Persistence. PostUpdate: updated.

@ javax.mail. Persistence. PreRemove: delete the former.

@ javax.mail. Persistence. PostRemove: after deletion.

1) Database query

The @postload event is triggered when:

Execute the EntityManager.find() or getReference () methods after loading an entity.

After executing the JPQL query.

After the EntityManager.refresh() method is called.

2) Database insert

The @Prepersist and @Postpersist events occur during the insertion of entity objects into the database:

The @PrePersist event occurs immediately after the persist() method is called, before the data is actually inserted into the database.

The @PostPersist event occurs after the data has been inserted into the database.

3) Database update

@PreUpdate and @PostUpdate events are triggered by the update entity:

The @Preupdate event is triggered before the state of the entity is synchronized to the database, before the data is actually updated to the database.

The @PostUpdate event is triggered after the state of the entity is synchronized to the database, which occurs when the transaction commits.

4) Database deletion

The @Preremove and @Postremove events are triggered by the deletion of entities:

The @Preremove event is triggered before the entity is removed from the database, that is, when the remove() method is called to remove, before the data is actually removed from the database.

The @postremove event is triggered when an entity is removed from the database.