题记部分
一、物理删除&逻辑删除
物理删除:delete from table_name where xxx = ?;
逻辑删除:update table_name set deleted = 0 where xxx = ?;
二、测试
(1)增加逻辑删除字段deleted(默认1,1:存在,0:删除)
alter table user add column deleted int(1) default 1 comment '逻辑删除' after version;
(2)POJO实体类增加属性
package com.harley.pojo;import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;import java.util.Date;/*** @author harley* @date 2024/06/06 17:30*/@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {@TableId(type = IdType.AUTO)private Long id;private String name;private Integer age;private String email;@Versionprivate Integer version;@TableLogic // 逻辑删除private Integer deleted;@TableField(fill = FieldFill.INSERT)private Date createTime;@TableField(fill = FieldFill.INSERT_UPDATE)private Date updateTime;
}
(3)配置 application.properties
spring.application.name: mybatis-plus
# 数据库连接配置
spring.datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&userUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghaiusername: rootpassword: '!QAZ2wsx'mybatis-plus:global-config:# 配置逻辑删除 db-config:# 删除为0logic-delete-value: 0# 存在为1logic-not-delete-value: 1configuration:# 配置日志log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
(4)测试删除
- 首先查询user表中的数据
- 漩涡鸣人开启仙人模式干掉6个佩恩
@Test
void testDelete(){int res = userMapper.deleteBatchIds(Arrays.asList(7L,8L,9L,10L,11L,12L));if (res > 0){System.out.println("漩涡鸣人开启仙人模式干掉了"+res+"个佩恩");}
}
— 业精于勤荒于嬉,行成于思毁于随 —