@TableName
@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)
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
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
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
fill indicates whether to automatically fill in
the data. When the object is stored in the data, mybatisplus automatically assigns values to certain fields.
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
Create an autofill processor
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
2. Add the version member variable to the entity class and add @version
3.Write configuration class
test
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
2. Create a new enumeration class
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