Detailed explanation of common annotations in MyBatis-Plus

@TableName

abb436bf7018e2b45cbe9879d1f34da6.png

@TableId
primary key exclusive

For example, the field in the table in the data is id

But the entity class is userId

Then you need to put this annotation on userId

usage

Set the primary key mapping value mapping primary key field name

type sets the primary key generation strategy for the primary key type (the circled ones are important)

8d46e6c6d9fc4626f8290dd269036b85.png

f9bd61c7e0bac5058fdc42408c133b51.png

1. Database auto-increment AUTO.
Developers do not need to assign values. They will auto-increment +1 based on the maximum ID value in the current table.

If you assign the value manually, you should still use the maximum value + 1 in the database id to assign the id.


2 Automatically generate primary keys using the snowflake algorithm NONE.
When using it with new additions, you need to pay attention to the length of the primary key. However, in 3.20, the maximum value of this field + 1 is adopted.

1. Change the data type from int to long

2. The length of the database table needs to be changed

You need to pay attention to the generated length, otherwise an error will be reported.

Caused by: org.apache.ibatis.reflection.ReflectionException:
 Could not set property 'id' of 'class com.lin.mybatisplus.pojo.User'
 with value '1289924709438889985' Cause: java.lang.IllegalArgumentException: 
 argument type mismatch
then You also need to modify the data type before you can save it.


3 The developer manually assigns INPUT.
If the id is not assigned a value during the insertion operation, then the value stored in the database will be 0 (long).

The database generally uses the method of auto-incrementing the ID to process the value of the current database number + 1

If a value is given, use the given value


4 ASSIGN_ID
is equivalent to ID_ WORKER(3), ID_ WORKER STR(3),

Using mp to automatically assign values ​​uses the snowflake algorithm, but in 3.20, the maximum value of the field + 1 is used.

5 ASSIGN_ID
is equivalent to UUID

The primary key type must be String type, and UUID will be automatically generated for assignment.

The field design of the database needs to be considered. The int type in the database cannot be saved, but because the primary key auto-increments, if you want to modify the int type to a varchar type, you need to remove the primary key auto-increment.

@TableField
non-primary key field usage

For example, the field in the table in the data is name

But the entity class is userName

3c2a34ae327a164fa0f94d476722e1ef.png

Then you need to put this annotation on userName


Mapping non-primary key fields value mapping field names

exist indicates whether it is a database field.
When doing VO DTO, we often query some fields that are not in the database. If they are not labeled

4d56078ee21c9c9624673e147ea402eb.png

Then the query will report an error (if the member face change in the entity class does not have a corresponding field in the database, it can be regarded as the same)

Needs to be set to false


select indicates whether to query this field
and does not participate in the query, and returns null

2239b8ef52590d7659c2f5198a031d51.png

fill indicates whether to automatically fill in
the data. When the object is stored in the data, mybatisplus automatically assigns values ​​to certain fields.

7a2dc0c6a9d40b27a36b67b5a5c1b48b.png

Application scenarios

create_time update_time

For example, the registration time and modification time are basically used by every table. The implementation is to obtain the time and save it.

But it's annoying

Then let mp do it (take the following configuration and need to create a processor)


DEFAULT -- not processed by default

INSERT --Fill the fields when inserting (when adding for the first time)

INSERT_UPDATE --Fill fields when inserting and updating

(The latest update means that both insertion and update are satisfied)

UPDATE --Fill fields when updating

c758820ba62330fc63df75cc86180d05.png
Create an autofill processor

35271fc1ebe813d8d9bfd69b98b5a708.png
test


@Version
tag optimistic locking-main modification operation

-Prevent data from being manipulated repeatedly

-Prevent two threads from operating the same data at the same time to ensure data security

principle:

Mark optimistic locking to ensure data security through the version field. When modifying data,

Version will be used as a condition, and the modification will be successful only when the condition is met.

version=1

Thread 1: update ... set version = 2 where version= 1

Thread 2: update ... set version = 2 where version= 1

explain:

If any thread successfully operates and the version is changed to 2, then the other thread will fail to execute.

Environment setup:

1. Add a version field to the database with a default value of 1

eaca6eb425a8eb608fa9e2103ad90ac7.png
2. Add the version member variable to the entity class and add @version

b2ebf3887b42c71a0bcba7e4b7a90b16.png
3.Write configuration class

e2465166af969ac4037088ae81bf384f.png

test

13122c0d004e9270eb557f76e9dee5ed.png

Modify the situation at the same time - if you are interested, you can try it


@EnumValue
is a general enumeration class annotation that maps database fields to enumeration type member variables of entity classes.

Awareness: Change the enumeration into a member variable and map it to the database field, and find the object corresponding to the enumeration based on the value of the database field

1. Add status field to database

56c31d15c6c4dd7b2937974198f0e04c.png
2. Create a new enumeration class

65cd92dbe0d610c086c7e477bd7f4b25.png
Adding fields - please note that they must be mapped with database fields


Configure enumeration package scanning in the configuration file
@TableLogic

Map tombstone

Commonly used annotations
@TableName: annotate data table names

@TableId: table primary key identifier

@TableId(value = “id”, type = IdType.AUTO): automatic

@TableId(value = “id”, type = IdType.ID_WORKER_STR): Distributed global unique ID string type

@TableId(value = “id”, type = IdType.INPUT): Enter it yourself

@TableId(value = “id”, type = IdType.ID_WORKER): distributed global unique ID long integer type

@TableId(value = “id”, type = IdType.UUID): 32-bit UUID string

@TableId(value = “id”, type = IdType.NONE): stateless

@TableField: table field identifier

@TableField(exist = false): Indicates that the attribute is not a database table field, but it must be used.

@TableField(exist = true): Indicates that the property is a database table field.

@TableField(condition = SqlCondition.LIKE): Indicates that this property can be fuzzy searched.

@TableField(fill = FieldFill.INSERT): Annotate the fill field, and the generator strategy part can also be configured!

@FieldStrategy:

@FieldFill

@Version: Optimistic lock annotations and tags

@EnumValue: Pass enumeration class annotation

@TableLogic: Table field logical processing annotation (logical deletion)

@SqlParser: tenant annotation
 

Guess you like

Origin blog.csdn.net/qq_43012298/article/details/118362066