詳解MybatisPlus中@TableLogic注解的使用
1. 簡單介紹
嗨,大家好,今天給想給大家分享一下關于Mybatis-plus 的 Service 層的一些方法的使用。今天沒有總結,因為都是一些API沒有什么可以總結的,直接看著調用就可以了。
下面我們將介紹 @TableLogic 注解的用法,以及每個屬性的實際意義和用法
2. 注解說明
@TableLogic 用于實現(xiàn)數(shù)據(jù)庫數(shù)據(jù)邏輯刪除
注意,該注解只對自動注入的 sql 起效
3. @TableLogic 對于 CIUD 的限制
3.1 插入(insert)
不作限制
3.2 查找(select)
@TableLogic 注解將會在 select 語句的 where 條件添加條件,過濾掉已刪除數(shù)據(jù)
且使用 wrapper.entity 生成的 where 條件會忽略該字段
SELECT user_id,name,sex,age,deleted FROM user WHERE user_id=1 AND deleted='0'
3.3 更新(update)
@TableLogic 注解將會在 update 語句的 where 條件后追加條件,防止更新到已刪除數(shù)據(jù)
且使用 wrapper.entity 生成的 where條件會忽略該字段
update user set deleted=1 where id = 1 and deleted=0
3.4 刪除(delete)
@TableLogic 注解會將 delete 語句轉變?yōu)?update 語句
update user set deleted=1 where id = 1 and deleted=0
4. @TableLogic 字段類型支持說明:
支持所有數(shù)據(jù)類型(推薦使用 Integer、Boolean、LocalDateTime)
如果數(shù)據(jù)庫字段使用 datetime,邏輯未刪除值和已刪除值支持配置為字符串 null,另一個值支持配置為函數(shù)來獲取值如now()
附錄:
(1)邏輯刪除是為了方便數(shù)據(jù)恢復和保護數(shù)據(jù)本身價值等等的一種方案,但實際就是刪除。
(2)如果你需要頻繁查出來看就不應使用邏輯刪除,而是以一個狀態(tài)去表示。
5. 屬性說明
5.1 value
用來指定邏輯未刪除值,默認為空字符串
5.2 delval
用來指定邏輯刪除值,默認為空字符串。
6. 在配置文件中實現(xiàn)
當然,你也可以不在 @TableLogic 注解中指定 value 和 delval 屬性的值。使用全局邏輯刪除配置信息,配置如下:
# application.yml
mybatis-plus:
global-config:
db-config:
# 全局邏輯刪除的實體字段名 (since 3.3.0, 配置后可以忽略 @TableLogic 中的配置)
logic-delete-field: flag
# 邏輯已刪除值(默認為 1)
logic-delete-value: 1
# 邏輯未刪除值(默認為 0)
logic-not-delete-value: 0
7. 代碼實踐
7.1 代碼實踐過程
我們在 user 數(shù)據(jù)表中添加一個 deleted 字段。
如果該字段值為1,表示記錄被刪除。如果該字段值為0,表示記錄未被刪除
1.向 user 數(shù)據(jù)表添加 deleted 字段,sql 如下:
-- 添加一個 deleted 字段,實現(xiàn)邏輯刪除 ALTER TABLE `user` ADD COLUMN `deleted` varchar(1) NULL DEFAULT 0 COMMENT '是否刪除(1-刪除;0-未刪除)';
2.創(chuàng)建 user 表的實體類 AnnotationUser7Bean
使用 @TableLogic 注解將 deleted 成員變量指定為邏輯刪除字段
import com.baomidou.mybatisplus.annotation.*;
@TableName(value = "user")
public class AnnotationUser7Bean {
@TableId(value = "user_id", type = IdType.AUTO)
private int userId;
@TableField("name")
private String name;
@TableField("sex")
private String sex;
@TableField("age")
private Integer age;
@TableLogic(value = "0", delval = "1")
private String deleted;
// 忽略 getter 和 setter 方法
@Override
public String toString() {
return "UserBean{" +
"userId=" + userId +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", deleted=" + deleted +
'}';
}
}
上面代碼中,使用 @TableLogic 注解將 deleted 成員變量指定為邏輯刪除字段
@TableLogic(value = "0", delval = "1") private String deleted;
3.客戶端代碼,先查詢用戶ID為1的用戶是否存在
如果存在,則刪除該用戶信息
然后,查詢用戶ID小于10的用戶信息
package com.hxstrive.mybatis_plus.simple_mapper.annotation;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxstrive.mybatis_plus.mapper.AnnotationUser7Mapper;
import com.hxstrive.mybatis_plus.model.AnnotationUser7Bean;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
class AnnotationDemo7 {
@Autowired
private AnnotationUser7Mapper userMapper;
@Test
void contextLoads() throws Exception {
// 刪除用戶ID為1的用戶信息
AnnotationUser7Bean oldUserBean = userMapper.selectById(1);
if(null != oldUserBean) {
userMapper.deleteById(oldUserBean.getUserId());
}
// 查詢用戶信息
QueryWrapper<AnnotationUser7Bean> wrapper = new QueryWrapper<>();
wrapper.lt("user_id", 10);
for(AnnotationUser7Bean item : userMapper.selectList(wrapper)) {
System.out.println(item);
}
}
}
7.2 數(shù)據(jù)庫代碼執(zhí)行說明
根據(jù)用戶ID查詢用戶信息
Preparing: SELECT user_id,name,sex,age,deleted FROM user WHERE user_id=? AND deleted='0' Parameters: 1(Integer)
根據(jù)用戶ID刪除用戶信息
sql 是一個更新語句,這是因為我們使用了邏輯刪除,而不是物理刪除
Preparing: UPDATE user SET deleted='1' WHERE user_id=? AND deleted='0' Parameters: 1(Integer)
查詢用戶ID小于10的用戶信息
Preparing: SELECT user_id,name,sex,age,deleted FROM user WHERE deleted='0' AND (user_id < ?) Parameters: 10(Integer)
以上就是詳解MybatisPlus中@TableLogic注解的使用的詳細內容,更多關于MybatisPlus @TableLogic注解的資料請關注腳本之家其它相關文章!
相關文章
Java實現(xiàn)的獲取和判斷文件頭信息工具類用法示例
這篇文章主要介紹了Java實現(xiàn)的獲取和判斷文件頭信息工具類,結合實例形式分析了Java針對文件讀取及頭信息判斷相關操作技巧,需要的朋友可以參考下2017-11-11
Java實現(xiàn)單鏈表SingleLinkedList增刪改查及反轉 逆序等
單鏈表是鏈表的其中一種基本結構。一個最簡單的結點結構如圖所示,它是構成單鏈表的基本結點結構。在結點中數(shù)據(jù)域用來存儲數(shù)據(jù)元素,指針域用于指向下一個具有相同結構的結點。 因為只有一個指針結點,稱為單鏈表2021-10-10
Spring中ApplicationListener的使用解析
這篇文章主要介紹了Spring中ApplicationListener的使用解析,ApplicationContext事件機制是觀察者設計模式的實現(xiàn),通過ApplicationEvent類和ApplicationListener接口,需要的朋友可以參考下2023-12-12
springBoot 過濾器去除請求參數(shù)前后空格實例詳解
這篇文章主要為大家介紹了springBoot 過濾器去除請求參數(shù)前后空格實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11

