MyBatis-Plus實(shí)現(xiàn)邏輯刪除功能解析
前言
有時(shí)候并不需要真正的刪除數(shù)據(jù),而是想邏輯刪除,方便數(shù)據(jù)恢復(fù)。
MyBatis-Plus可以很方便的實(shí)現(xiàn)邏輯刪除的功能。
Entity類(lèi)
首先,數(shù)據(jù)庫(kù)表添加一個(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
對(duì)應(yīng)的UserEntity實(shí)體類(lèi):
@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í)體類(lèi)中的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());
}
}
測(cè)試一下
下面來(lái)測(cè)試一下。
首先,添加一個(gè)新的用戶(hù):
@Test
public void testLogicDelete() {
// 插入一個(gè)新的用戶(hù)
UserEntity newUser = new UserEntity();
newUser.setId(11L);
newUser.setName("Kevin");
newUser.setAge(25);
newUser.setEmail("kevin@163.com");
userMapper.insert(newUser);
}
控制臺(tái)日志:
==> 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ù)庫(kù)數(shù)據(jù):

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

到此這篇關(guān)于MyBatis-Plus實(shí)現(xiàn)邏輯刪除功能解析的文章就介紹到這了,更多相關(guān)MyBatis-Plus邏輯刪除內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis-Plus邏輯刪除實(shí)現(xiàn)過(guò)程
- MyBatis-Plus中的邏輯刪除功能及實(shí)例分析
- mybatis-plus實(shí)現(xiàn)邏輯刪除的示例代碼
- mybatis-plus邏輯刪除與唯一約束沖突問(wèn)題
- Mybatis-Plus邏輯刪除的用法詳解
- mybatis-plus實(shí)現(xiàn)邏輯刪除的示例代碼
- MyBatis-Plus中的邏輯刪除使用詳解
- MyBatis-Plus邏輯刪除和字段自動(dòng)填充的實(shí)現(xiàn)
- mybatis-plus 如何配置邏輯刪除
- MyBatis-Plus 邏輯刪除的實(shí)現(xiàn)示例
相關(guān)文章
詳解使用Jenkins自動(dòng)編譯部署web應(yīng)用
本篇主要介紹基于Jenkins實(shí)現(xiàn)持續(xù)集成的方式,通過(guò)案例介紹線上自動(dòng)編譯及部署的配置過(guò)程,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
初探Spring Cloud Gateway實(shí)戰(zhàn)
這篇文章主要介紹了創(chuàng)建網(wǎng)關(guān)項(xiàng)目(Spring Cloud Gateway)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-08-08
Java大小寫(xiě)轉(zhuǎn)換及一些常見(jiàn)的注意事項(xiàng)詳解
這篇文章主要介紹了Java大小寫(xiě)轉(zhuǎn)換及一些常見(jiàn)的注意事項(xiàng)的相關(guān)資料,Java提供了多種方法進(jìn)行大小寫(xiě)轉(zhuǎn)換,包括String類(lèi)的toLowerCase和toUpperCase方法,以及Character類(lèi)的toLowerCase和toUpperCase方法,需要的朋友可以參考下2025-02-02
Java C++ leetcode執(zhí)行一次字符串交換能否使兩個(gè)字符串相等
這篇文章主要為大家介紹了Java C++ leetcode1790執(zhí)行一次字符串交換能否使兩個(gè)字符串相等,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
SpringBoot和Vue項(xiàng)目服務(wù)器發(fā)布流程分享
本文詳細(xì)介紹了如何將SpringBoot和Vue項(xiàng)目發(fā)布到阿里云ECS服務(wù)器上的步驟,包括準(zhǔn)備服務(wù)器、安裝寶塔、配置數(shù)據(jù)庫(kù)、打包項(xiàng)目、上傳文件、設(shè)置端口、安裝軟件和注冊(cè)網(wǎng)站等2025-02-02
SpringBoot WebService服務(wù)端&客戶(hù)端使用案例教程
這篇文章主要介紹了SpringBoot WebService服務(wù)端&客戶(hù)端使用案例教程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-10-10
詳解SpringCloud服務(wù)認(rèn)證(JWT)
本篇文章主要介紹了SpringCloud服務(wù)認(rèn)證(JWT),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01

