Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method for Mybatis plus logic to delete the annotation @ TableLogic

2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Mybatis plus logic to delete the annotation @ TableLogic what is the method, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

First of all, this note is produced by Bao Mi Dou, which is what we often call the upgraded version of mybatis.

Briefly explain the use of this annotation: when we are doing database design, sometimes even deletions do not really go through physical deletions, after all, a lot of data cannot be recovered, and most of the time, many deletions are deleted logically.

Physical deletion and logical deletion

To put it simply, a physical deletion actually deletes this piece of data from the database.

The logical deletion is mainly to use a field in the table as an identifier. Generally speaking, the default initial value is 0. When we want to delete this data, we will change this identifier to 1. In this way, the record is deleted from our common sense. And any query will add this condition, that is, to ensure that the query data is valid in the code.

@ TableLogic annotation @ TableLogic annotation default value:

I have just said that this annotation is used for logical deletion, which represents an identifier. Since it is an identifier, it must have a value. For example, as I said above, 0 indicates existence and 1 means deletion. However, it cannot be ruled out that some designs do not follow common sense, such as 1 for existence and 2 for deletion. So the @ TableLogic annotation can be set by default. The usage is as follows:

@ TableLogic (value= "identity exists", delval= "Delete identity") private Integer del_flag

If you do not write value and delval, the identity is 0. 0 by default. The deletion logo is 1.

@ TableLogic annotation usage

The use of this annotation is relatively simple, when we add this comment to a field in the entity class, it is tantamount to telling mp that the field is an identifier. All the addition, deletion and modification methods that we call BaseMapper or IService will be affected.

The condition constructor is used when querying. Instead of judging this field, the existing values are automatically filtered out. Here we need to pay attention to take my above demo as an example: even if we pass the condition del_flag = 1, it will not take effect. To sum up briefly: the results queried by the mp conditional constructor must exist logically.

The time of modification is about the same. If we assign the del_flag of the entity object to 1 and then save it, it will not be successful.

If we do not use this annotation when deleting, it will be physically deleted by using mp's delete method or remove method. But if this annotation is used, then all delete or remove methods are logically deleted, that is, changing del_flag to 1. 0.

It should be noted that the above only affect the mp query, we use xml or comments to write our own sql is not valid!

After reading the above, have you mastered the method of Mybatis plus logically deleting the annotation @ TableLogic? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report