搜索
首页
笔记
案例
关于
课程列表
MyBatis-Plus
CURD
自动填充
条件构造器
ActiveRecord
代码生成器
插件
逻辑删除
课程导航
计算机基础知识
C
Linux
linux常用软件
计算机网络
程序员修养
设计模式
工具
Git
composer
vim
IntelliJ IDEA
wireshark
laravel
Spring
SpringMVC
Maven
数据库
MySQL
Redis
MongoDB
JDBC
Mybatis
MyBatis-Plus
服务端编程
PHP
Java
shell script
JavaWeb
HTML / CSS
HTML
CSS
HTML5
CSS3
BOOTSTRAP
JavaScript
JavaScript
JQuery
layui
插件
逻辑删除
逻辑删除作为一个常见需求,MyBatisPlus为我们提供了简便的方法实现逻辑删除功能(通过MyBatisPlus的逻辑删除插件实现)。 **注意:逻辑删除插件只对自动注入的sql起效** 步骤如下: - 配置逻辑删除插件 - 在实体类字段上加入`@TableLogic`注解 ## 准备数据表 ```sql CREATE TABLE `t_country` ( `country_id` int(10) NOT NULL AUTO_INCREMENT, `country_name` varchar(16) NOT NULL DEFAULT '' , `is_delete` tinyint(1) NOT NULL DEFAULT 0 , PRIMARY KEY (`coutry_id`) ) ; INSERT INTO `t_country`(country_name) VALUES ("中国"),("美国"),('英国'),('澳大利亚'); ``` ## 定义Mapper以及实体类 CountryMapper定义如下: ```java public interface CountryMapper extends BaseMapper
{ } ``` 实体类定义如下: ```java public class Country { @TableId private Integer countryId; private String countryName; @TableLogic private Integer isDelete; public Country() { } public Country(Integer countryId, String countryName) { this.countryId = countryId; this.countryName = countryName; } public Integer getCountryId() { return countryId; } public void setCountryId(Integer countryId) { this.countryId = countryId; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } @Override public String toString() { return "Country{" + "countryId=" + countryId + ", countryName='" + countryName + '\'' + '}'; } } ``` ## 配置逻辑删除插件  ## 测试逻辑删除功能 ```java int america = 2; mapper.deleteById(america); // UPDATE t_country SET is_delete=1 WHERE country_id=? mapper.selectById(america); // SELECT country_id AS countryId,country_name AS countryName,is_delete AS isDelete // FROM t_country WHERE country_id=? AND is_delete=0 ``` ## SpringBoot中使用逻辑删除 - 添加逻辑删除字段 ```sql ALTER TABLE `t_employee` ADD COLUMN `is_delete` tinyint(1) NOT NULL DEFAULT 0 AFTER `update_time`; ``` * 配置 ``` mybatis-plus: configuration: map-underscore-to-camel-case: true mapper-locations: classpath:mapper/*Mapper.xml global-config: db-config: table-prefix: t_ id-type: auto logic-delete-field: is_delete # 逻辑删除字段 logic-delete-value: 1 # 删除时字段值 logic-not-delete-value: 0 # 未删除时字段值 ``` * 在实体类中添加isDelete字段,并添加@TableLogic注解。 - 测试 ```java @Test void test6 () { int i = employeeMapper.deleteById(9); // UPDATE t_employee SET is_delete=1 WHERE emp_id=? AND is_delete=0 employeeMapper.selectById(9); // SELECT emp_id,age,emp_name,email,sex,add_time,update_time,is_delete FROM t_employee WHERE emp_id=? AND is_delete=0 } ```
插件
文章目录