Article Directory
Update and delete operations in MyBatisPlus
Update operation
1.updateById(user) update according to id
As shown below:
2.update(user, updateWrapper) update according to conditions
As shown below:
3. Tables in the database
As shown below:
Delete operation
Physical deletion
As shown below:
Tombstone
Logical deletion refers to not deleting the data corresponding to the table in the database, but changing the value of the deleted field of the row in the table;
1. Add a deleted field to the database
2. Add a deleted attribute to the entity class
Where logic means "logical"
3. Configuration
When logical deletion, you need to configure a plug-in in the configuration class of MyBatisPlus, and you also need to configure the value of the deleted field in the database table after logical deletion and before logical deletion in the SpringBoot configuration file.
4. Test
Logically delete the row with id 5. Before the logical deletion, the table in the database is as follows:
The code for testing tombstone is as follows:
The table in the database after logical deletion is as follows:
If logical deletion is used in the program, the condition of deleted=0 will be added by default after the where condition every time you query, which means that only rows that have not been logically deleted will be queried, as shown in the following figure: