mybatis-plus批量删除_Mybatis Plus mapper的多种姿势删除数据

Mybatis Plus的删除操作内置了4种方法,我们来演示一下Mybatis Plus的删除操作。

mybatis-plus批量删除_Mybatis Plus mapper的多种姿势删除数据_第1张图片

Mybatis Plus mapper的删除操作

1、根据主键ID删除指定单条数据

方法:

int deleteById(Serializable id);

代码:

    @Test    public void testDeleteById() {        System.out.println(("----- deleteById method test ------"));        List userList = userMapper.selectList(null);        System.out.println("-----删除前数据:");        userList.forEach(System.out::println);        userMapper.deleteById(5L);        System.out.println("-----删除后剩余数据:");        List userList2 = userMapper.selectList(null);        userList2.forEach(System.out::println);    }

前面已经输出了较多sql,我们这里简洁一些,只输出打印的数据

-----删除前数据:User(id=1, name=Jone, age=18, [email protected])User(id=2, name=Jack, age=20, [email protected])User(id=3, name=Tom, age=28, [email protected])User(id=4, name=Sandy, age=21, [email protected])User(id=5, name=Billie, age=24, [email protected])-----删除后剩余数据:User(id=1, name=Jone, age=18, [email protected])User(id=2, name=Jack, age=20, [email protected])User(id=3, name=Tom, age=28, [email protected])User(id=4, name=Sandy, age=21, [email protected])

2、基于map中设置的key-value,删除一条或多条数据

基于map的删除,可以设置多个列作为条件,被匹配的数据都将被删除。

方法:

int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap);

代码:

    @Test    public void testDeleteByMap() {        System.out.println(("----- deleteByMap method test ------"));        List userList = userMapper.selectList(null);        System.out.println("-----删除前数据:");        userList.forEach(System.out::println);        Map columnMap = new HashMap<>();        columnMap.put("Name","Tom");        userMapper.deleteByMap(columnMap);        System.out.println("-----删除后剩余数据:");        List userList2 = userMapper.selectList(null);        userList2.forEach(System.out::println);    }

输出:

-----删除前数据:User(id=1, name=Jone, age=18, [email protected])User(id=2, name=Jack, age=20, [email protected])User(id=3, name=Tom, age=28, [email protected])User(id=4, name=Sandy, age=21, [email protected])User(id=5, name=Billie, age=24, [email protected])-----删除后剩余数据:User(id=1, name=Jone, age=18, [email protected])User(id=2, name=Jack, age=20, [email protected])User(id=4, name=Sandy, age=21, [email protected])User(id=5, name=Billie, age=24, [email protected])

3、基于一个实体作为条件,删除一条或多条数据

方法:

int delete(@Param(Constants.WRAPPER) Wrapper wrapper);

如果方法的参数设置为null,那么相当于全表删除

代码:

输出结果:

-----删除前数据:User(id=1, name=Jone, age=18, [email protected])User(id=2, name=Jack, age=20, [email protected])User(id=3, name=Tom, age=28, [email protected])User(id=4, name=Sandy, age=21, [email protected])User(id=5, name=Billie, age=24, [email protected])==>  Preparing: DELETE FROM user ==> Parameters: <==    Updates: 5-----删除后剩余数据:==>  Preparing: SELECT id,name,age,email FROM user ==> Parameters: <==      Total: 0

我们从输出的日志看出:

==>  Preparing: DELETE FROM user ==> Parameters: <==    Updates: 5

sql是全表删除。

如果方法设置了实体作为参数,那就按照实体匹配,删除对应数据。

4、根据主键ID集合,批量删除数据

方法:

int deleteBatchIds(@Param(Constants.COLLECTION) Collection extends Serializable> idList);

代码:

    @Test    public void testDeleteBatchIds() {        System.out.println(("----- deleteBatchIds method test ------"));        List userList = userMapper.selectList(null);        System.out.println("-----删除前数据:");        userList.forEach(System.out::println);        List list = new ArrayList();        list.add(4L);        list.add(5L);        userMapper.deleteBatchIds(list);        System.out.println("-----删除后剩余数据:");        List userList2 = userMapper.selectList(null);        userList2.forEach(System.out::println);    }

输出:

-----删除前数据:User(id=1, name=Jone, age=18, [email protected])User(id=2, name=Jack, age=20, [email protected])User(id=3, name=Tom, age=28, [email protected])User(id=4, name=Sandy, age=21, [email protected])User(id=5, name=Billie, age=24, [email protected])==>  Preparing: DELETE FROM user WHERE id IN ( ? , ? ) ==> Parameters: 4(Long), 5(Long)<==    Updates: 2-----删除后剩余数据:==>  Preparing: SELECT id,name,age,email FROM user ==> Parameters: <==    Columns: ID, NAME, AGE, EMAIL<==        Row: 1, Jone, 18, [email protected]<==        Row: 2, Jack, 20, [email protected]<==        Row: 3, Tom, 28, [email protected]<==      Total: 3User(id=1, name=Jone, age=18, [email protected])User(id=2, name=Jack, age=20, [email protected])User(id=3, name=Tom, age=28, [email protected])

你可能感兴趣的:(mybatis-plus批量删除_Mybatis Plus mapper的多种姿势删除数据)