Skip to content

删除(delete)

Mapper 内置方法

  • deleteAll():删除所有
  • deleteById(id):根据ID删除
  • deleteById(ids):多个ID删除
  • delete(T):一个实体类删除
  • delete(List<T>):多个实体类删除
  • delete(Where):动态条件删除

根据ID 删除

java
public class Demo {
    @Autowired
    private SysUserMapper sysUserMapper;
    
    public void delete() {
        sysUserMapper.deleteById(1);
    }
}

根据实体类删除

java
public class Demo {
    @Autowired
    private SysUserMapper sysUserMapper;
    
    public void delete() {
        SysUserModel sysUser = sysUserMapper.getById(1);
        sysUserMapper.delete(sysUser);
    }
}

动态delete

基于DeleteChain(强大)

可构建复杂的场景

java
public class Demo {
    @Autowired
    private SysUserMapper sysUserMapper;

    public void delete() {
        DeleteChain.of(sysUserMapper)
                .eq(SysUser::getId, 1)
                .execute();
    }
}

基于where,(批量)删除

java
public class Demo {
    @Autowired
    private SysUserMapper sysUserMapper;

    public void delete() {
        sysUserMapper.delete(where -> {
            where.gt(SysUser::getId,100);
        });
    }
}