Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use physical deletion and logical deletion of MyBatis-Plus

Shulou Source: shulou.com Published: 2022-06-03 10:02:45 10月02日 Update

This article mainly introduces the physical deletion and logical deletion of MyBatis-Plus how to use, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Physical deletion: actual deletion, deleting the data from the database. The deleted data cannot be queried after deletion.

Logical deletion: false deletion to facilitate data recovery after deletion

Add a status field to the table as the standard for deletion. Each time you delete, modify the flag bit. 0 means not deleted, 1 indicates deletion.

The status field of the deleted record is modified to "deleted status" (1), and the data can be queried after deletion.

Usage scenarios for logical deletion

1. Data recovery is required

two。 Associated data, inconvenient to delete

# mysql// test logic delete status int null default null# entity class / / implement logic delete @ TableLogic@TableField (fill=FieldFill.INSERT) / / to facilitate testing private Integer status;# MpConfig configuration class / / status is automatically populated with a default of 0 when added, and the undeleted state (1 is deleted) this.setFieldValByName ("status", 0MetaObject); # Test test class / / add test data @ Testpublic void testAdd () {User user=new User () first / / the id attribute primary key is not set for self-growth, but mybatis-plus uses the primary key policy to automatically generate id values user.setName ("wangwu"); user.setAge (18); user.setEmail ("licy@163.com"); int insert=userMapper.insert (user); System.out.println (insert) } / / Retest logic delete / / add a status field to the table as the standard for deletion. Each time you delete, modify the flag bit, 0 means not deleted, 1 means delete / / UPDATE user SET status=1 WHERE id=? The AND status=0 query condition has a status=0, that is, the records that have not been deleted are queried and the deleted records will not be queried. @ Testpublic void testDeleteByLogic () {int rows=userMapper.deleteById (1441596779656179713L); / / after the configuration of logical deletion, use id to delete, and the statement executed is changed to modify id=?. And a record with a status of 0 will not be deleted if the status is 1 (deleted), System.out.println (rows);}

Add test data first

Retest logic deletion

Thank you for reading this article carefully. I hope the article "how to use the physical deletion and logical deletion of MyBatis-Plus" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

Tags: Logic data tests queries representatives articles physics fields status data recovery standards flags configuration inconvenience value interest and at the same time scenarios entities that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Redmi Shulou Information Linux Shulou Technology