SoFunction
Updated on 2025-03-08

Pit record encountered using @TableField(updateStrategy=)

@TableField(updateStrategy=)

@TableField(updateStrategy = )
private String phoneNo;

Because the contact phone number can be changed to a null value, adding the @TableField(updateStrategy = ) annotation can make this field not ignore when phoneNo is empty.

Later, when I modified other properties of the object, I did not set the value of phoneNo. When I updated, I found that phoneNo was updated to empty. So to avoid this situation, I must not re-query the field value and assign it again.

Remember to use this annotation with caution! ! !

Notes on the usage notes of @TableField

@TableField is an annotation that identifies field mapping relationships in entity classes. It is one of the annotations provided by the MyBatis-Plus framework for mapping with database table fields.

When using MyBatis-Plus for database operations, we can use the @TableField annotation to specify the mapping relationship between fields in the entity class and fields in the database table. This annotation can be used on class member variables or on class methods.

Common @TableField annotation parameters include

  • value: Specifies the mapping relationship between entity class fields and database table fields. If the entity class field name is the same as the database table field name, this parameter can be omitted.
  • exist: Specifies whether this field is a database table field. The default is true, which means it is a database table field; setting to false means it is not a database table field.
  • fill: Specifies whether the field is an autofill field, which is used to automatically fill the field's value during insertion and update operations.
public class User {
    @TableField("user_name")
    private String userName;

    @TableField(value = "age", exist = false)
    private Integer age;

    // getter and setter
}

In the above code, the userName field is mapped to the database table field user_name, while the age field is not a database table field.

in addition:

@TableName annotation, the value value specifies the corresponding table name.

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("t_user")
public class User {
    private Long id;
    private String name;

}

@TableId annotation: MyBatisPlus will use Id as the primary key when implementing CRUD. When inserting data, use the snowflake algorithm to generate Id. If the primary key is not called Id, the addition function will fail.

So @TableId has the following usage:

@TableId(value = “uid”) //value specifies the field currently as the primary key@TableId(value = “id”,type = ) //Use the field corresponding to the current attribute as the primary key
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("t_user")
public class User {
    @TableId
    private Long id;
    private String name;

}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.