在Mybaties Plus 里面的逻辑删除功能简介
逻辑删除是一种数据管理策略,它允许应用程序通过标记数据记录为"已删除"而不是从数据库中物理删除记录来维护数据的完整性。
在 MyBatis Plus 中实现逻辑删除的简介如下:
• 字段约定:在数据表中定义一个逻辑删除标志字段,常用的字段名为is_deleted
或deleted
,字段类型通常为tinyint
或boolean
。
• 默认值:该字段的默认值应设置为表示未删除的状态,例如0
(未删除)和1
(已删除)。
• 查询过滤:在执行查询操作时,自动添加过滤条件以排除逻辑删除的数据。MyBatis Plus 提供了自动过滤逻辑删除记录的功能,无需手动编写 SQL 语句来排除这些记录。
• 操作接口:MyBatis Plus 提供了SoftDelete
接口,实体类实现该接口后,可以自动应用逻辑删除策略。
• 删除操作:更新操作不再直接删除数据库记录,而是更新逻辑删除标志字段的值为已删除状态。
• 配置:在 MyBatis Plus 的配置中,可以指定逻辑删除的字段名和删除值,以便框架自动识别和处理逻辑删除。
逻辑删除的优点包括:
• 数据恢复:逻辑删除的数据可以被恢复,因为它们仍然存在于数据库中。
• 数据审计:可以追踪数据的删除历史,有助于数据审计和分析。
• 性能:避免了物理删除操作可能引起的性能问题,如索引重组等。
逻辑删除是一种在业务逻辑层面上处理删除操作的方法,它通过改变数据的状态来模拟删除效果,而不是真正从数据库中移除数据。这种方法在需要保留历史数据记录的场景中非常有用。
使用案例
controller层
java
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 {
@Autowired
private 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层
java
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的数据
java
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 {
@Autowired
private 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
yml
mybatis-plus:
global-config:
db-config:
logic-delete-field: flag # 全局逻辑删除的实体字段名(配置后可以忽略不配置步骤二)
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
办法二:在实体类中的删除标识字段上增加@TableLogic注解
@TableLogic的作用是标识逻辑删除字段
java
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;
}
再次进行测试
java
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 {
@Autowired
private PaymentTypeService paymentTypeService;
@Operation(summary = "查询全部支付方式列表")
@GetMapping("list")
public Result<List<PaymentType>> listPaymentType() {
List<PaymentType> list = paymentTypeService.list();
return Result.ok(list);
}
}
这样我们就可以看到被逻辑删除的数据就不会再次显示了