一、项目搭建
项目的搭建过程省略,参考此文章
CREATE TABLE `user` ( `id` bigint(20) NOT NULL COMMENT '主键', `name` varchar(30) DEFAULT NULL COMMENT '姓名', `age` int(11) DEFAULT NULL COMMENT '年龄', `email` varchar(50) DEFAULT NULL COMMENT '邮箱', `manager_id` bigint(20) DEFAULT NULL COMMENT '直属上级id', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `update_time` datetime DEFAULT NULL COMMENT '更新时间', `version` int(10) DEFAULT '1' COMMENT '版本', `deleted` varchar(1) DEFAULT '0' COMMENT '删除标识', PRIMARY KEY (`id`), KEY `manager_fk` (`manager_id`), CONSTRAINT `manager_fk` FOREIGN KEY (`manager_id`) REFERENCES `user` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `mp`.`user`(`id`, `name`, `age`, `email`, `manager_id`, `create_time`, `update_time`, `version`, `deleted`) VALUES (1087982257332887553, '大boss', 40, 'boss@baomidou.com', NULL, '2019-01-11 14:20:20', NULL, 1, '0'); INSERT INTO `mp`.`user`(`id`, `name`, `age`, `email`, `manager_id`, `create_time`, `update_time`, `version`, `deleted`) VALUES (1088248166370832385, '王天风', 26, 'wtf2@baomidou.com', 1087982257332887553, '2019-02-05 11:12:22', NULL, 1, '0'); INSERT INTO `mp`.`user`(`id`, `name`, `age`, `email`, `manager_id`, `create_time`, `update_time`, `version`, `deleted`) VALUES (1088250446457389058, '李艺伟', 30, 'lyw2021@baomidou.com', 1088248166370832385, '2019-02-14 08:31:16', NULL, 1, '0'); INSERT INTO `mp`.`user`(`id`, `name`, `age`, `email`, `manager_id`, `create_time`, `update_time`, `version`, `deleted`) VALUES (1094590409767661570, '张雨琪', 31, 'zjq@baomidou.com', 1088248166370832385, '2019-01-14 09:15:15', NULL, 1, '0'); INSERT INTO `mp`.`user`(`id`, `name`, `age`, `email`, `manager_id`, `create_time`, `update_time`, `version`, `deleted`) VALUES (1094592041087729666, '刘红雨', 32, 'lhm@baomidou.com', 1088248166370832385, '2019-01-14 09:48:16', NULL, 1, '0');
二、代码实战
1、配置文件增加全局配置
目的是全局设置逻辑未删除和逻辑删除在数据库中对应的值
# Mybatis Plus配置
mybatis-plus:
global-config:
db-config:
logic-delete-value: 1
logic-not-delete-value: 0
2、使用@TableLogic来标记逻辑删除的字段
该注解也能局部控制逻辑未删除和逻辑删除在数据库中对应的值,@TableLogic(value = "0", delval = "1")
@Data public class User extends Model<User> { //主键 @TableId(type= IdType.ID_WORKER) private Long id; //姓名 private String name; //年龄 private Integer age; //邮箱 private String email; //直属上级id private Long managerId; //创建时间 private Date createTime; //更新时间 private Date updateTime; //版本 private String version; //删除标识:0未删除1已删除 @TableLogic private String deleted; }
3、注入配置类
高版本MP不需要这一步
@Configuration public class MybatisPlusConfig { @Bean public ISqlInjector sqlInjector(){ return new LogicSqlInjector(); } }
4、测试
@SpringBootTest public class LogicDeleteTest { @Autowired private UserMapper userMapper; @Test public void logicDelete() { int rows = userMapper.deleteById("1094592041087729666"); System.out.println("rows===" + rows); } }
修改后的查询、修改也会将逻辑已删除的记录排除在外
@Test public void select() { List<User> list = userMapper.selectList(null); System.out.println("list===" + list); }
三、查询中排除删除标识字段及注意事项
使用@TableField(select = false)来排除字段
@TableLogic @TableField(select = false) private String deleted;
需要注意的是:如果是我们自定义的查询、修改语句,比如自定义xml,那么MP是不会帮我们加delete='0'这个条件的,也就是说需要我们自己去添加where条件不查已经被逻辑删除的记录