当前位置: 首页 > backend >正文

尚庭公寓-----day1----逻辑删除功能

在Mybaties Plus 里面的逻辑删除功能简介

逻辑删除是一种数据管理策略,它允许应用程序通过标记数据记录为“已删除”而不是从数据库中物理删除记录来维护数据的完整性。

在 MyBatis Plus 中实现逻辑删除的简介如下:

• 字段约定:在数据表中定义一个逻辑删除标志字段,常用的字段名为is_deleteddeleted,字段类型通常为tinyintboolean

• 默认值:该字段的默认值应设置为表示未删除的状态,例如0(未删除)和1(已删除)。

• 查询过滤:在执行查询操作时,自动添加过滤条件以排除逻辑删除的数据。MyBatis Plus 提供了自动过滤逻辑删除记录的功能,无需手动编写 SQL 语句来排除这些记录。

• 操作接口:MyBatis Plus 提供了SoftDelete接口,实体类实现该接口后,可以自动应用逻辑删除策略。

• 删除操作:更新操作不再直接删除数据库记录,而是更新逻辑删除标志字段的值为已删除状态。

• 配置:在 MyBatis Plus 的配置中,可以指定逻辑删除的字段名和删除值,以便框架自动识别和处理逻辑删除。

逻辑删除的优点包括:

• 数据恢复:逻辑删除的数据可以被恢复,因为它们仍然存在于数据库中。

• 数据审计:可以追踪数据的删除历史,有助于数据审计和分析。

• 性能:避免了物理删除操作可能引起的性能问题,如索引重组等。

逻辑删除是一种在业务逻辑层面上处理删除操作的方法,它通过改变数据的状态来模拟删除效果,而不是真正从数据库中移除数据。这种方法在需要保留历史数据记录的场景中非常有用。

使用案例

controller层

package com.nie.lease.web.admin.controller.apartment;import com.nie.lease.common.result.Result;
import com.nie.lease.model.entity.PaymentType;
import com.nie.lease.web.admin.service.PaymentTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@Tag(name = "支付方式管理")
@RequestMapping("/admin/payment")
@RestController
public class PaymentTypeController {@Autowiredprivate PaymentTypeService paymentTypeService;@Operation(summary = "查询全部支付方式列表")@GetMapping("list")public Result<List<PaymentType>> listPaymentType() {List<PaymentType> list = paymentTypeService.list();return Result.ok(list);}@Operation(summary = "保存或更新支付方式")@PostMapping("saveOrUpdate")public Result saveOrUpdatePaymentType(@RequestBody PaymentType paymentType) {return Result.ok();}@Operation(summary = "根据ID删除支付方式")@DeleteMapping("deleteById")public Result deletePaymentById(@RequestParam Long id) {return Result.ok();}}

service层

package com.nie.lease.web.admin.service;import com.nie.lease.model.entity.PaymentType;
import com.baomidou.mybatisplus.extension.service.IService;/**
* @author liubo
* @description 针对表【payment_type(支付方式表)】的数据库操作Service
* @createDate 2023-07-24 15:48:00
*/
public interface PaymentTypeService extends IService<PaymentType> {}

当我们这样写之后 我们可以看出 他逻辑删除的数据依旧被查询出来了
在这里插入图片描述

解决办法:

解决方案一:

直接添加一个过滤条件 只查询isDeleted为0的数据

package com.nie.lease.web.admin.controller.apartment;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nie.lease.common.result.Result;
import com.nie.lease.model.entity.PaymentType;
import com.nie.lease.web.admin.service.PaymentTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@Tag(name = "支付方式管理")
@RequestMapping("/admin/payment")
@RestController
public class PaymentTypeController {@Autowiredprivate PaymentTypeService paymentTypeService;@Operation(summary = "查询全部支付方式列表")@GetMapping("list")public Result<List<PaymentType>> listPaymentType() {LambdaQueryWrapper<PaymentType> paymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();paymentTypeLambdaQueryWrapper.eq(PaymentType::getIsDeleted,0);List<PaymentType> list = paymentTypeService.list(paymentTypeLambdaQueryWrapper);return Result.ok(list);}

·

解决方案二:直接使用Mybaties plus的逻辑删除功能

两个办法二选一即可

办法一:配置application.yml
mybatis-plus:global-config:db-config:logic-delete-field: flag # 全局逻辑删除的实体字段名(配置后可以忽略不配置步骤二)logic-delete-value: 1 # 逻辑已删除值(默认为 1)logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
办法二:在实体类中的删除标识字段上增加@TableLogic注解

@TableLogic的作用是标识逻辑删除字段

package com.nie.lease.model.entity;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;import java.io.Serializable;
import java.util.Date;@Data
public class BaseEntity implements Serializable {@Schema(description = "主键")@TableId(value = "id", type = IdType.AUTO)private Long id;@Schema(description = "创建时间")@TableField(value = "create_time")private Date createTime;@Schema(description = "更新时间")@TableField(value = "update_time")private Date updateTime;@Schema(description = "逻辑删除")@TableLogic@TableField("is_deleted")private Byte isDeleted;}

再次进行测试

package com.nie.lease.web.admin.controller.apartment;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nie.lease.common.result.Result;
import com.nie.lease.model.entity.PaymentType;
import com.nie.lease.web.admin.service.PaymentTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@Tag(name = "支付方式管理")
@RequestMapping("/admin/payment")
@RestController
public class PaymentTypeController {@Autowiredprivate PaymentTypeService paymentTypeService;@Operation(summary = "查询全部支付方式列表")@GetMapping("list")public Result<List<PaymentType>> listPaymentType() {List<PaymentType> list = paymentTypeService.list();return Result.ok(list);}}

这样我们就可以看到被逻辑删除的数据就不会再次显示了
在这里插入图片描述

http://www.xdnf.cn/news/15513.html

相关文章:

  • PHP语法高级篇(三):Cookie与会话
  • 构建 Go 可执行文件镜像 | 探索轻量级 Docker 基础镜像(我应该选择哪个 Docker 镜像?)
  • DGNNet:基于双图神经网络的少样本故障诊断学习模型
  • element plus使用插槽方式自定义el-form-item的label
  • 3D数据:从数据采集到数据表示,再到数据应用
  • 本地电脑安装Dify|内网穿透到公网
  • 【Qt】插件机制详解:从原理到实战
  • 【科研绘图系列】R语言绘制中国地图和散点图以及柱状图
  • ES2023 新特性解析_数组与对象的现代化操作指南
  • 一文厘清楼宇自控系统架构:包含哪些关键子系统及其作用
  • 部署项目将dll放到system32?不可取
  • 基于LAMP环境的校园论坛项目
  • 阿里开源项目 XRender:全面解析与核心工具分类介绍
  • Spring面试核心知识点整理
  • iOS高级开发工程师面试——Swift
  • 驭码 CodeRider 产品介绍
  • AR眼镜颠覆医疗:精准手术零误差
  • 再见吧,Windows自带记事本,这个轻量级文本编辑器太香了
  • DeepSWE:通过强化学习扩展训练开源编码智能体
  • PySpark 常用算子详解
  • kotlin的自学笔记1
  • King’s LIMS:实验室数字化转型的智能高效之选
  • 19.如何将 Python 字符串转换为 Slug
  • 极致cms多语言建站|设置主站默认语言与设置后台固定语言为中文
  • 手机当路由,连接机器人和电脑
  • Postman + Newman + Jenkins 接口自动化测试
  • 说下对mysql MVCC的理解
  • DNS的含义以及例子
  • 传输协议和消息队列
  • Claude 背后金主亚马逊亲自下场,重磅发布 AI 编程工具 Kiro 现已开启免费试用