注意:本教程使用的数据库脚本、数据模型和环境信息请参考 “MyBatis Plus环境准备” 章节,点击下载示例源码。
在 BaseMapper 接口中定义了四个 delete 方法,分别如下:
// 根据 wrapper 条件,删除记录 int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper); // 删除(根据ID 批量删除) int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); // 根据 ID 删除 int deleteById(Serializable id); // 根据 columnMap 条件,删除记录 int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
参数说明:
wrapper:删除数据的 Wrapper 条件对象
idList:ID 列表,一次将指定的所有ID记录的数据删除
id:单条数据 ID
columnMap:Map 类型的条件对象
(1)根据ID删除数据,代码如下:
package com.hxstrive.mybatis_plus.delete; import com.hxstrive.mybatis_plus.mapper.SimpleMapper; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest class Delete1Test { @Autowired private SimpleMapper simpleMapper; @Test void contextLoads() { int result = simpleMapper.deleteById(999); System.out.println("result=" + result); } }
(2)使用 Map 类型的条件删除数据,代码如下:
package com.hxstrive.mybatis_plus.delete; import com.hxstrive.mybatis_plus.mapper.SimpleMapper; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.HashMap; import java.util.Map; @RunWith(SpringRunner.class) @SpringBootTest class Delete2Test { @Autowired private SimpleMapper simpleMapper; @Test void contextLoads() { Map<String,Object> map = new HashMap<>(); map.put("sex", "女"); int result = simpleMapper.deleteByMap(map); System.out.println("result=" + result); } }
(3)根据 Wrapper 查询对象删除数据,代码如下:
package com.hxstrive.mybatis_plus.delete; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.hxstrive.mybatis_plus.mapper.SimpleMapper; import com.hxstrive.mybatis_plus.model.UserBean; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest class Delete3Test { @Autowired private SimpleMapper simpleMapper; @Test void contextLoads() { QueryWrapper<UserBean> wrapper = new QueryWrapper<>(); wrapper.le("age", 30); // 小于等于30 int result = simpleMapper.delete(wrapper); System.out.println("result=" + result); } }
(4)一次性删除多个ID数据,代码如下:
package com.hxstrive.mybatis_plus.delete; import com.hxstrive.mybatis_plus.mapper.SimpleMapper; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.Arrays; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest class Delete4Test { @Autowired private SimpleMapper simpleMapper; @Test void contextLoads() { List<Integer> ids = Arrays.asList(2, 4, 6, 7); int result = simpleMapper.deleteBatchIds(ids); System.out.println("result=" + result); } }