Update and delete operations in MyBatisPlus

Update and delete operations in MyBatisPlus

Update operation

1.updateById(user) update according to id

As shown below:

Insert picture description here

2.update(user, updateWrapper) update according to conditions

As shown below:

Insert picture description here

3. Tables in the database

As shown below:

Insert picture description here

Delete operation

Physical deletion

As shown below:

Insert picture description here

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

Insert picture description here

2. Add a deleted attribute to the entity class

Insert picture description here

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.

Insert picture description here

Insert picture description here

4. Test

Logically delete the row with id 5. Before the logical deletion, the table in the database is as follows:

Insert picture description here

The code for testing tombstone is as follows:

Insert picture description here

The table in the database after logical deletion is as follows:

Insert picture description here

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:

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45950109/article/details/112685778