MybatisPlus的删除delete

删除操作
1、根据id删除一条记录
    @Test
    public void deleteById(){
        int i = userMapper.deleteById(1462266553855746050L);
        if(i > 0){
            System.out.println("删除成功");
        }
    }

输出日志:

JDBC Connection [HikariProxyConnection@1183065103 wrapping com.mysql.cj.jdbc.ConnectionImpl@32120956] will not be managed by Spring
==>  Preparing: DELETE FROM tb_user WHERE user_id=? 
==> Parameters: 1462266553855746050(Long)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7523a3dc]
删除成功
2、根据id批量删除
    @Test
    public void deleteByBatchId(){
        int i = userMapper.deleteBatchIds(Arrays.asList(1462270263956729858L, 1462270263956729859L, 1462270263956729860L));
        if (i > 1){
            System.out.println("批量删除成功");
        }
    }

输出日志信息:

JDBC Connection [HikariProxyConnection@1871617286 wrapping com.mysql.cj.jdbc.ConnectionImpl@77d381e6] will not be managed by Spring
==>  Preparing: DELETE FROM tb_user WHERE user_id IN ( ? , ? , ? ) 
==> Parameters: 1462270263956729858(Long), 1462270263956729859(Long), 1462270263956729860(Long)
<==    Updates: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7523a3dc]
批量删除成功
3、条件删除map
    @Test
    public void deleteByMap(){
        Map<String,Object>map = new HashMap<>();
        map.put("name","张三1");
        int i = userMapper.deleteByMap(map);
        System.out.println(i);
    }

输出日志

JDBC Connection [HikariProxyConnection@579983776 wrapping com.mysql.cj.jdbc.ConnectionImpl@5e26f1ed] will not be managed by Spring
==>  Preparing: DELETE FROM tb_user WHERE name = ? 
==> Parameters: 张三1(String)
<==    Updates: 6
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3f018494]
6
4、逻辑删除

物理删除:从数据库中直接移除
逻辑删除:再数据库中没有被移除,而是通过一个变量让它失效,比如delete = 0 变成 delete =1就是逻辑删除
管理员可以查看删除的记录,防止数据的丢失,类似于回收站!
1)在数据表中增加一个deleted字段并且默认为0(未删除)

2)在实体类中增加属性

   @TableLogic // 逻辑删除
   private int deleted;

3)注册逻辑组件

@MapperScan("sz.kingdom.testmybatisplus.mapper")
@Configuration
public class MyConfiguration {
	@Bean
    public ISqlInjector sqlInjector(){
        return new DefaultSqlInjector();
    }
  }
}

4) 配置逻辑删除

mybatis-plus:
  global-config:
    db-config:
      logic-delete-value: 1
      logic-not-delete-value: 0

逻辑已删除值(默认值为1),逻辑未删除值(默认是0)
5)测试

    @Test
    public void deleteById(){
        int i = userMapper.deleteById(1);
        if(i > 0){
            System.out.println("删除成功");
        }
    }

6)输出日志

JDBC Connection [HikariProxyConnection@1510312933 wrapping com.mysql.cj.jdbc.ConnectionImpl@4eaf7902] will not be managed by Spring
==>  Preparing: UPDATE tb_user SET deleted=1 WHERE user_id=? AND deleted=0 
==> Parameters: 1(Integer)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@53ed80d3]
删除成功

清晰看见:删除的语句变成了update更新语句 UPDATE tb_user SET deleted=1 WHERE user_id=? AND deleted=0

你可能感兴趣的:(Mybatis-Plus,java,mybatis)