Mybatis-Plus通用Mapper CRUD之delete
mybatis-plus提供了4个删除方法:
/** * 根据 ID 删除 * * @param id 主键ID */ int deleteById(Serializable id); /** * 根据 columnMap 条件,删除记录 * * @param columnMap 表字段 map 对象 */ int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap); /** * 根据 entity 条件,删除记录 * * @param wrapper 实体对象封装操作类(可以为 null) */ int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper); /** * 删除(根据ID 批量删除) * * @param idList 主键ID列表(不能为 null 以及 empty) */ int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
我们先讲下deleteById,deleteByMap,deleteBatchIds方法,delete方法我们放条件构造器里一起讲;
实例:
@Test
public void deleteById(){
int affectRows = departmentMapper.deleteById(10);
if(affectRows>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}
@Test
public void deleteByMap(){
Map<String,Object> map=new HashMap<>();
map.put("name","你好部门5");
map.put("remark","xxx");
int affectRows = departmentMapper.deleteByMap(map);
if(affectRows>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}
@Test
public void deleteBatchIds(){
List<Integer> idList=new LinkedList<>();
idList.add(11);
idList.add(12);
idList.add(13);
int affectRows =departmentMapper.deleteBatchIds(idList);
if(affectRows>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}