MyBatis-Plus實(shí)現(xiàn)邏輯刪除功能解析
前言
有時(shí)候并不需要真正的刪除數(shù)據(jù),而是想邏輯刪除,方便數(shù)據(jù)恢復(fù)。
MyBatis-Plus可以很方便的實(shí)現(xiàn)邏輯刪除的功能。
Entity類
首先,數(shù)據(jù)庫表添加一個(gè)表示邏輯刪除的字段delete_flag:
CREATE TABLE `tb_user` ( `id` bigint NOT NULL COMMENT '主鍵ID', `name` varchar(30) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '姓名', `age` int DEFAULT NULL COMMENT '年齡', `email` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '郵箱', `create_time` timestamp NOT NULL COMMENT '創(chuàng)建時(shí)間', `update_time` timestamp NOT NULL COMMENT '更新時(shí)間', `delete_flag` tinyint(1) NOT NULL COMMENT '邏輯刪除,0 - 未刪除;-1 - 已刪除', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
對應(yīng)的UserEntity實(shí)體類:
@Data
@TableName("tb_user")
public class UserEntity {
private Long id;
private String name;
private Integer age;
private String email;
@TableField(value = "create_time", fill = FieldFill.INSERT)
private Date createTime;
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
private Integer deleteFlag;
}
配置
首先,需要在UserEntity實(shí)體類中的deleteFlag字段上進(jìn)行注解配置:
@TableLogic(value = "0", delval = "-1")配置邏輯刪除字段的值,value的值表示未刪除的時(shí)候的值,delval的值表示已刪除時(shí)候的值;
@TableField(value = "delete_flag", fill = FieldFill.INSERT)配置deleteFlag字段的自動(dòng)填充規(guī)則。
@Data
@TableName("tb_user")
public class UserEntity {
private Long id;
private String name;
private Integer age;
private String email;
@TableField(value = "create_time", fill = FieldFill.INSERT)
private Date createTime;
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
@TableLogic(value = "0", delval = "-1")
@TableField(value = "delete_flag", fill = FieldFill.INSERT)
private Integer deleteFlag;
}
可以配置deleteFlag在insert數(shù)據(jù)的時(shí)候,默認(rèn)填充0:
/**
* 自動(dòng)填充字段值得配置
*/
@Component
public class AutoFillFieldValueConfig implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
this.strictInsertFill(metaObject, "deleteFlag", Integer.class, 0);
}
@Override
public void updateFill(MetaObject metaObject) {
this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
}
}
測試一下
下面來測試一下。
首先,添加一個(gè)新的用戶:
@Test
public void testLogicDelete() {
// 插入一個(gè)新的用戶
UserEntity newUser = new UserEntity();
newUser.setId(11L);
newUser.setName("Kevin");
newUser.setAge(25);
newUser.setEmail("kevin@163.com");
userMapper.insert(newUser);
}
控制臺日志:
==> Preparing: INSERT INTO tb_user ( id, name, age, email, create_time, update_time, delete_flag ) VALUES ( ?, ?, ?, ?, ?, ?, ? )
==> Parameters: 11(Long), Kevin(String), 25(Integer), kevin@163.com(String), 2021-09-20 21:29:37.232(Timestamp), 2021-09-20 21:29:37.234(Timestamp), 0(Integer)
<== Updates: 1
數(shù)據(jù)庫數(shù)據(jù):

可以看到,delete_flag默認(rèn)填充了0。
下面,來測試一下邏輯刪除(現(xiàn)在調(diào)用所有的刪除方法,都是邏輯刪除):
@Test
public void testLogicDelete() {
// 插入一個(gè)新的用戶
// UserEntity newUser = new UserEntity();
// newUser.setId(11L);
// newUser.setName("Kevin");
// newUser.setAge(25);
// newUser.setEmail("kevin@163.com");
// userMapper.insert(newUser);
// 邏輯刪除
userMapper.deleteById(11L);
}
控制臺日志:
==> Preparing: UPDATE tb_user SET delete_flag=-1 WHERE id=? AND delete_flag=0
==> Parameters: 11(Long)
<== Updates: 1
可以看到,刪除方法并沒有執(zhí)行DELETE語句,而是執(zhí)行的UPDATE語句,更新了delete_flag字段的值。
數(shù)據(jù)庫數(shù)據(jù):

到此這篇關(guān)于MyBatis-Plus實(shí)現(xiàn)邏輯刪除功能解析的文章就介紹到這了,更多相關(guān)MyBatis-Plus邏輯刪除內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Knife4j?3.0.3?整合SpringBoot?2.6.4的詳細(xì)過程
本文要講的是?Knife4j?3.0.3?整合SpringBoot?2.6.4,在SpringBoot?2.4以上的版本和之前的版本還是不一樣的,這個(gè)也容易導(dǎo)致一些問題,本文就這兩個(gè)版本的整合做一個(gè)實(shí)戰(zhàn)介紹2022-09-09
SpringBoot實(shí)現(xiàn)快遞物流查詢功能(快遞鳥)
本文將基于springboot2.4.0實(shí)現(xiàn)快遞物流查詢,物流信息的獲取通過快遞鳥第三方實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-10-10
關(guān)于工廠方法模式的Java實(shí)現(xiàn)
這篇文章主要介紹了關(guān)于工廠方法模式的Java實(shí)現(xiàn)講解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Spring定時(shí)任務(wù)輪詢本地?cái)?shù)據(jù)庫實(shí)現(xiàn)過程解析
這篇文章主要介紹了Spring定時(shí)任務(wù)輪詢本地?cái)?shù)據(jù)庫實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01

