MybatisPlus實現邏輯刪除

2021-10-08 12:33:07 字數 1489 閱讀 4038

//查詢user表所有資料

@test

public void findall()

//批量刪除

@test

public void testdeletebatchids()

@test

public void testdeletebymap()

1)資料庫中新增 deleted欄位

alter table `user` add column `deleted` boolean
(2)實體類新增deleted欄位

並加上 @tablelogic 註解 和 @tablefield(fill = fieldfill.insert) 註解

@tablelogic

@tablefield(fill = fieldfill.insert)

private integer deleted;

(3)元物件處理器介面新增deleted的insert預設值

@override

public void insertfill(metaobject metaobject)

此為預設值,如果你的預設值和mp預設的一樣,該配置可無

mybatis-plus.global-config.db-config.logic-delete-value=1

mybatis-plus.global-config.db-config.logic-not-delete-value=0

(5)在 mybatisplusconfig 中註冊 bean

@bean

public isqlinjector sqlinjector()

(6)測試邏輯刪除

/**

* 測試 邏輯刪除

*/@test

public void testlogicdelete()

(7)測試邏輯刪除後的查詢

mybatis plus中查詢操作也會自動新增邏輯刪除欄位的判斷

/**

* 測試 邏輯刪除後的查詢:

* 不包括被邏輯刪除的記錄

*/@test

public void testlogicdeleteselect()

測試後分析列印的sql語句,包含 where deleted=0 

select id,name,age,email,create_time,update_time,deleted from user where deleted=0

mybatis plus 實現邏輯刪除

1.物理刪除 真實刪除,將對應資料從資料庫中刪除,之後查詢不到此條被刪除資料 2.邏輯刪除 假刪除,將對應資料中代表是否被刪除字段狀態修改為 被刪除狀態 之後在資料庫中仍舊能看到此條資料記錄 3.資料庫中新增 deleted欄位 alter table user add column deleted...

mybatis plus邏輯刪除

1.先在資料庫加delete status欄位,還有實體類加deletestatus欄位 你試試實體類加delete status,會有問題,預設是駝峰 這就是告訴spring,我要把這欄位設為刪除標誌 tablelogic 邏輯刪除 private integer deleted 2.在3.1.1...

mybatis plus 邏輯刪除

整合步驟 1 匯入依賴 2 配置 資料來源 驅動 鏈結資訊 spring datasource username root password root url jdbc mysql driver class name com.mysql.cj.jdbc.driver name gulimall pr...