In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.