邏輯刪除
SpringBoot 配置方式:
-
application.yml 加入配置(如果你的默認(rèn)值和mp默認(rèn)的一樣,該配置可無):
mybatis-plus: global-config: db-config: logic-delete-value: 1 # 邏輯已刪除值(默認(rèn)為 1) logic-not-delete-value: 0 # 邏輯未刪除值(默認(rèn)為 0) -
注冊(cè) Bean(3.1.1開始不再需要這一步):import com.baomidou.mybatisplus.core.injector.ISqlInjector; import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyBatisPlusConfiguration { @Bean public ISqlInjector sqlInjector() { return new LogicSqlInjector(); } } -
實(shí)體類字段上加上
@TableLogic注解@TableLogic private Integer deleted; -
效果: 使用mp自帶方法刪除和查找都會(huì)附帶邏輯刪除功能 (自己寫的xml不會(huì))
example 刪除時(shí) update user set deleted=1 where id =1 and deleted=0 查找時(shí) select * from user where deleted=0
::: tip 附件說明
- 邏輯刪除是為了方便數(shù)據(jù)恢復(fù)和保護(hù)數(shù)據(jù)本身價(jià)值等等的一種方案,但實(shí)際就是刪除。
- 如果你需要再查出來就不應(yīng)使用邏輯刪除,而是以一個(gè)狀態(tài)去表示。
如: 員工離職,賬號(hào)被鎖定等都應(yīng)該是一個(gè)狀態(tài)字段,此種場景不應(yīng)使用邏輯刪除。
- 若確需查找刪除數(shù)據(jù),如老板需要查看歷史所有數(shù)據(jù)的統(tǒng)計(jì)匯總信息,請(qǐng)單獨(dú)手寫sql。
:::