Springboot--springboot annotations

Springboot annotation:
@Service: Annotation on the class, indicating that this is a business layer bean
@Controller: Annotation on the class, indicating that this is a control layer bean
@Repository: Annotation on the class, indicating that this is a data access layer bean
@ Component: Annotated on the class, which means a generic bean. If the value is not written, the default is the lowercase letter of the class name
@Autowired: injected by type. The default property required = true; when you cannot be sure that a certain type of bean must be in the Spring container, you can You can use @Autowired (required = false) where you need to automatically inject this type of Bean, which is equivalent to telling Spring: BeanCreationException is not thrown when no matching Bean is found. When @Autowired and @Qualifier are used together, the automatic injection strategy changes from byType to byName. @Autowired can annotate member variables, methods and constructors, while @ Qualifier's annotation objects are member variables, method input parameters, and constructor function input parameters. Because of the different annotation objects, Spring does not unify @Autowired and @Qualifier into a single annotation class.
@Resource: Assemble by name

the difference:

@Resource defaults to matching beans by name, and @Autowired defaults to matching beans by type

@Resource (importjavax.annotation.Resource;) is the annotation of J2EE, @Autowired (importorg.springframework.beans.factory.annotation.Autowired;) is the annotation of
Spring @Configuration: The annotation is on the class, indicating that this is an IOC container, Equivalent to spring configuration file, java configuration. The configuration class of the IOC container is generally used in conjunction with the @Bean annotation, the @Configuration annotation class is equivalent to configuring beans in XML, and the @Bean annotation method is equivalent to configuring beans in XML.
@Bean: Annotated on the method, declare that the current method returns a Bean

@Scope: Annotated on the class, describing how the spring container creates a Bean instance.

(1) singleton: represents a singleton in the spring container, and always returns the only instance when the bean is obtained through the spring container

(2) prototype: indicates that each time a bean is obtained, a new object is generated

(3) request: indicates that it is valid within one http request (only applicable to web applications)

(4) session: indicates that it is valid within a user session (only applicable to web applications)

(5) globalSession: indicates that it is valid in the global session (only applicable to web applications)

In most cases, we will only use the singleton and prototype scopes. If the scope attribute is not specified, the default is singleton
@Value: Annotated on the variable, read from the configuration file.

例如:@Value(value = “#{message}”)

@ConfigurationProperties Assign values ​​and convert annotations to objects. Assign values ​​to objects. Auto insurance project: HttpClientSetting class

@Profile: Annotation will take effect in a specific environment where you choose to instantiate different Beans in different situations on the method class! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !

@SpringBootApplication: @ SpringBootApplication = @ ComponentScan + @ Configuration + @ EnableAutoConfiguration: convention is better than configuration

@EnableAutoConfiguration enables automatic configuration of the Spring application context, trying to guess and configure the beans you may need. Auto-configuration classes are usually applied based on your classpath and already defined beans objects. The package annotated with @EnableAutoConfiguration has a specific meaning and is used as the default configuration. It is generally recommended to configure @EnableAutoConfiguration under the root package so that all subpackages and classes can be found.

@ComponentScan: Annotated on the class, scan the class annotated with @Controller and other annotations, and register as a bean. @ComponentScan configures component scanning instructions for @Configuration annotated classes. The @ComponentScan annotation will automatically scan all the classes marked with @Component annotation under the specified package and register as beans, of course including the sub-annotations @Service, @Repository, @Controller under @Component. 

@RestController @RestController is an annotation that combines @ResponseBody and @Controller

The @Responsebody annotation indicates that the result returned by this method is directly written into the HTTP response body (ResponseBody), which is generally used when acquiring data asynchronously, usually after using @RequestMapping, the return value is usually parsed as a jump path, plus @Responsebody The returned result will not be parsed as a jump path, but directly written into the HTTP response body.

@RequestBody

@PathVariable

@RequestParam

The role of both is to bind the value of the parameter in the request to the method parameter in contorl, the difference is that the URL is written differently.

An exception occurs when the request parameter username does not exist, which can be solved by setting the attribute required = false, for example:

@RequestParam(value="username",required=false)

When using @RequestParam, the URL looks like this: http: // host: port / path? Parameter name = parameter value

When using @PathVariable, the URL looks like this: http: // host: port / path / parameter value

When not writing, you can also get the parameter value, but the name must correspond. Parameters can be omitted without writing

@RequestMapping and the request message are corresponding to   
  a: value, specify the address of the request 
  b: method request method type If this is not written, adaptive: get or post
  c: consumes the submitted content type of the request 
  d: producers specifies the returned content The type is only returned if the specified type is included in the (Accept) type in the request request header
  e: params specifies that the request must contain certain parameter values 
  f: headers specifies that the request must contain the specified header value

g: name specifies the name of the map  

 @RequestMapping(method = RequestMethod.GET)

 @RequestMapping(method = RequestMethod.POST)

 @RequestMapping(method = RequestMethod.PUT)

 @RequestMapping(method = RequestMethod.DELETE)

 Of course you can also use

 @GetMapping

 @PostMapping

 @PutMapping

 @DeleteMapping This is the same effect as above

 

@EnablCaching @EnableCaching annotation is an annotation-driven cache management function in spring framework. This annotation has been added since spring version 3.1. If you use this annotation, then you do not need to configure the cache manager in the XML file.

@suppresswarnings suppress warnings

@Modifying If it is added, modified, deleted and added this annotation

1: The return value of the method should be int, indicating the number of rows affected by the update statement.

2: The transaction must be added at the calling place, and no transaction cannot be executed normally. @Transactional transaction annotation

@Query custom query statement JPQL

JPA notes

@Entity:

@Table (name = "" "): The annotation indicates that this is an entity class. These two annotations are generally used together with jpa, but if the table name and entity class name are the same, @Table can be omitted

@Column: set by @Column annotation, including the following settings 
name: database table field name 
unique: whether it is unique 
nullable: whether it can be empty 

Length: length
inserttable: whether it can be inserted into 
updateable: whether it can be updated 
columnDefinition: defines the DDL
secondaryTable that created this column when creating the table  : secondary table name. If this column is not built on the master table (it is built on the master table by default), this attribute defines the name of the slave table where the column is located. 
@Column (name = "user_code", nullable = false, length = 32) // Set the field corresponding to the attribute userCode to user_code, length is 32, non-empty     
private String userCode;     
@Column (name = "user_wages", nullable = true , precision = 12, scale = 2) // Set the field corresponding to attribute wages to user_wages, 12 digits can retain two decimal places, can be empty     
private double wages;  

@Id: indicates that the attribute is the primary key.

@Temporal (TemporalType.DATE) // Set to time type     
private Date joinDate; 

@Transient: indicates that this attribute is not a mapping to a database table field, the ORM framework will ignore this attribute. If an attribute is not a field mapping of a database table, it must be marked as @Transient, otherwise, the ORM framework defaults its annotation to @Basic. @Basic (fetch = FetchType.LAZY): The tag can specify the loading method of entity attributes

@JsonIgnore: The role is to ignore some properties in the Java bean when json serialization, serialization and deserialization are affected.

@JoinColumn (name = ”loginId”): One-to-one: the foreign key in this table pointing to another table. One-to-many: Another table points to a foreign key in this table.

@OneToOne, @OneToMany, @ManyToOne: corresponding to one-to-one, one-to-many, and many-to-one in hibernate configuration files.

@GeneratedValue is used to annotate the generation strategy of the primary key, specified by the strategy attribute. By default, JPA automatically selects a primary key generation strategy that is most suitable for the underlying database: SqlServer corresponds to identity, and MySQL corresponds to auto increment. The following alternative strategies are defined in javax.persistence.GenerationType:

IDENTITY: The method of self-increment of database ID comes from increasing the primary key field, Oracle does not support this method;

AUTO: JPA automatically selects the appropriate strategy, which is the default option;

SEQUENCE: Generate the primary key through the sequence, specify the sequence name through the @SequenceGenerator annotation, MySql does not support this method

TABLE: The primary key is generated from the table, and the framework generates the primary key through the table simulation sequence. Using this strategy can make the application easier for database migration.

 
————————————————
Copyright Statement: This article is an original article by CSDN blogger "java-elementary student", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and reprint This statement.
Original link: https://blog.csdn.net/yitian_66/article/details/80866571

Published 7 original articles · 69 praises · 200,000+ views

Guess you like

Origin blog.csdn.net/u014320421/article/details/105483314