Basic attribute mapping

 

This piece is mainly based on some random records recorded by some online courses.

  This course mainly uses springboot and then the database operation uses the spring-data-jpa framework. Is the front-end and back-end separated? This course mainly talks about the back-end, or spring security, etc.

Jpa is a specification of orm, according to the definition on the Internet

  JPA stands for Java Persistence API, which is a collection of classes and methods used to store data in the database. JPA describes the object-relational table mapping relationship through JDK 5.0 annotations or XML, and persists the runtime entity objects to the database.

First create a class

  As shown

      

 

  The @Entity annotation in the class means: that this class is an entity class, and uses the default orm rules, that is, the name of the class corresponds to that, and the class name is the field name.

  The @Id in the class indicates that the attribute of an entity class is mapped to the primary key column of the database.

  @Id can also be on the get method.

  @Lombok is a plugin is lombok that omits the setget method.

  @GneratedValue:

    @GeneratedValue is used to annotate the generation strategy of annotations, and the strategy attribute is used to specify which generation strategy of annotations is (@AUTO is used here. Normally, there is no such thing in the formal environment. Auto generally uses the snowflake algorithm to generate the annotation strategy)

      The following strategies are defined in java.persistence.GenerationType

        -IDENTITY: Use the id self-growth method to increase the primary key field, Oracle does not support this method

        -AUTO: jpa automatically selects the appropriate strategy

        –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 by the table, and the framework uses the table to simulate the sequence to generate the primary key. Using this strategy can make the application easier to migrate to the database.

Run the springboot project and observe the database at the same time. Before doing this, remember to do the following in application.properties:

    

At this time, run the application main function. At this time, observe the database and see that the table has been generated.

    

At this time, you can see that the database table name and field name are the same as Category, but at this time I want to add a prefix to the table or field. At this time, @table can be used, as shown in the figure:

 

 Because the strategy configured in the application is update, a new table will be generated at this time. If I want to add a prefix to the field, I can use the @Column annotation

 

 

@Entity
@Data
@Table(name = "rc_category")
public class Category {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;


    @Column(name = "rc_name")
    private String name;
}

Running the application again will generate fields opposite to @Column

But sometimes some fields do not need to be stored in the database, we can use the @Transient annotation

@Entity
@Data
@Table(name = "rc_category")
public class Category {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;


    @Column(name = "rc_name")
    private String name;

    @Transient
    private String xxxx;
}

After running the application, as shown below

 

 Explain that @Transient has worked

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324642460&siteId=291194637