Explanation of Commonly Used plus Entity Class Annotation Characters in java Development of MyBatis

  • 2021-11-29 06:54:45
  • OfStack

Directory mybatis-plus Common Annotation 1. Table Name Annotation (@ TableName)
2. Primary key annotation (@ TableId)
3. Property annotation (@ TableField)

mybatis-plus Comments

1. Table name annotation (@ TableName)

Function: Entity classes and tables in the database establish corresponding relations: such as


@TableName("thotset")
public class HotsetEntity implements Serializable {
	private static final long serialVersionUID = 1L;
	private Integer fclass;
	private Integer fpwid;
	@JsonFormat(pattern = "yyyy-MM-dd")
	private Date fbdate;
	@JsonFormat(pattern = "yyyy-MM-dd")
	private Date fedate;
}

Representative: HotsetEntity corresponds to the table in the database as thotset

2. Primary key annotation (@ TableId)

Function: Identify the attribute of the entity class corresponding to the primary key in the table, and configure the generation strategy of the primary key, such as:


@TableName("tsvbase")
public class PaintLifeEntity implements Serializable {
	private static final long serialVersionUID = 1L;
	@TableId(type = IdType.AUTO)
	private String recid;
	
	private String fcode;
	private String fname;
	}

Representative: recid is the primary key in the table, and the generation strategy of the primary key is self-increasing.
Strategy and Attention of Primary Key Generation in mybaits-plus
IdType.ASSIGN_ID The primary key type is long integer or string. When using this type of primary key, we should pay attention to the problem that the length of long integer does not match the length of integer during data conversion at the front end, which will cause errors.
IdType.ASSIGN_UUID The primary key type is String, and 32 is a non-repeating string. Note that the string is out of order, pay attention to one record when using it, and the newly added record after the page refresh is not the last one or the first one, so the newly added record is often not found, and the user experience is very poor during maintenance.
IdType.AUTO Self-increasing;
IdType.input Before inserting data, you need to use other methods to get the primary key, and assign the obtained data to the primary key.
IdType.NONE Stateless, similar to Input

Note mybatis-plus Other primary key annotations have been discarded in higher versions

3. Property annotation (@ TableField)

Function: This attribute is not a primary key attribute, which solves the problem that the attribute name does not match the field name, whether the attribute is a field in the data table, insert, update generation strategy, etc. Such as:


@TableName("thotset")
public class HotsetEntity implements Serializable {
	private static final long serialVersionUID = 1L;
	private Integer fclass;
	private Integer fpwid;
	@TableField ( vlaue="fb_date" ) 
	private Date fbdate;
	@TableField ( exist=false ) 
	private Date fedate;
}

The first annotation represents that the database field corresponding to the attribute fbDate is named fb_date
The second annotation means that the fedate attribute does not match the fields in the table, so it is not necessary to match when adding or modifying

These are the three commonly used ones, and other annotations will not be described in detail.

The above is java development MyBatis commonly used plus entity class annotation symbol details, more information about MyBatis commonly used plus entity class annotation please pay attention to other related articles on this site!


Related articles: