当前位置:网站首页>Common technical notes

Common technical notes

2022-06-22 19:32:00 Algeria GG is learning

One 、JPA Entity annotations

  • @Entity - This is a tag comment , Indicates that this class is an entity , This annotation must be placed on the class name .
  • @Table - When the entity class is different from its mapped database table name, you need to use @Table, A common attribute of this annotation is name, Used to indicate the table name of the database .
  • @Id - Located in a specific field that holds the persistent identity attribute . This field is considered the primary key in the database .
  • @Column - It is used to identify the correspondence between the attributes in the entity class and the fields in the data table
  • @GeneratedValue - Generate a unique primary key for the entity class , Provides the primary key generation strategy .

It includes :

  1.  strategy: Primary key generation policy
  2.  generator: key generator

Two 、Jackson

brief introduction

  • Jackson It is widely used at present , Used to serialize and deserialize json Of Java Open source framework of .

  • Jackson The community is relatively active , The update speed is also relatively fast , from Github According to the statistics in ,Jackson It's the most popular json One of the parsers .

  • Spring MVC Default json The parser is Jackson. Jackson There are many advantages .

  • Jackson The dependent jar Fewer bags , Simple and easy to use .

  • And others Java Of json Framework Gson Such as compared to , Jackson Analyze the big json Documents are relatively fast ;Jackson It takes up less memory at runtime , Good performance ;

  • Jackson There are flexible API, It's easy to expand and customize .

Commonly used annotations

  • @JsonFormat - When obtaining data from the database and presenting it in the previous paragraph , Use this annotation
  • @DataTimeFormat - When the foreground transfers data to the background , Use this annotation
  • @JsonInclude - Specify the policy of entity class in serialization

    Common attribute values :
      - JsonJsonInclude.Include.ALWAYS : This is the default policy , In any case, serialize the field , It's the same effect as not writing this annotation .

     - JsonJsonInclude.Include.NON_NULL: This is the most commonly used , That is, if the annotated field is null, Then you won't serialize this field .

     - JsonJsonInclude.Include.NON_ABSENT:java There are some complex data structures in , such as AtomicReference or Optional, If a property is of this type , Although this property is not null, But the contents of the package are null Of , The purpose of this annotation is , Only this attribute is not null, And the content of its wrapper will be serialized only if it has value

     - JsonJsonInclude.Include.NON_EMPTY: This property contains NON_NULL,NON_ABSENT It also includes that if the field is empty, it will not be serialized . This one is also more commonly used

     - JsonJsonInclude.Include.NON_DEFAULT: It's easy to understand , If the field is the default value, it will not be serialized .

3、 ... and 、Mybatis

  • @Param -  The basic type has parameters that must be added @Param annotation ( Only one can be ignored but it is suggested to add ), among value The attribute value should be consistent with the configuration file parameters .

原网站

版权声明
本文为[Algeria GG is learning]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221759424634.html