这一节我们来演示一下Mybatis Plus的删除操作。
Mybatis Plus的删除操作内置了4种方法。
1、根据主键ID删除指定单条数据
方法:
int deleteById(Serializable id);
代码:
@Test
public void testDeleteById() {
System.out.println(("----- deleteById method test ------"));
List<User> userList = userMapper.selectList(null);
System.out.println("-----删除前数据:");
userList.forEach(System.out::println);
userMapper.deleteById(5L);
System.out.println("-----删除后剩余数据:");
List<User> userList2 = userMapper.selectList(null);
userList2.forEach(System.out::println);
}
前面已经输出了较多sql,我们这里简洁一些,只输出打印的数据
-----删除前数据:
User(id=1, name=Jone, age=18, email=test1@baomidou.com)
User(id=2, name=Jack, age=20, email=test2@baomidou.com)
User(id=3, name=Tom, age=28, email=test3@baomidou.com)
User(id=4, name=Sandy, age=21, email=test4@baomidou.com)
User(id=5, name=Billie, age=24, email=test5@baomidou.com)
-----删除后剩余数据:
User(id=1, name=Jone, age=18, email=test1@baomidou.com)
User(id=2, name=Jack, age=20, email=test2@baomidou.com)
User(id=3, name=Tom, age=28, email=test3@baomidou.com)
User(id=4, name=Sandy, age=21, email=test4@baomidou.com)
2、基于map中设置的key-value,删除一条或多条数据
基于map的删除,可以设置多个列作为条件,被匹配的数据都将被删除。
方法:
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
代码:
@Test
public void testDeleteByMap() {
System.out.println(("----- deleteByMap method test ------"));
List<User> userList = userMapper.selectList(null);
System.out.println("-----删除前数据:");
userList.forEach(System.out::println);
Map<String, Object> columnMap = new HashMap<>();
columnMap.put("Name","Tom");
userMapper.deleteByMap(columnMap);
System.out.println("-----删除后剩余数据:");
List<User> userList2 = userMapper.selectList(null);
userList2.forEach(System.out::println);
}
输出:
-----删除前数据:
User(id=1, name=Jone, age=18, email=test1@baomidou.com)
User(id=2, name=Jack, age=20, email=test2@baomidou.com)
User(id=3, name=Tom, age=28, email=test3@baomidou.com)
User(id=4, name=Sandy, age=21, email=test4@baomidou.com)
User(id=5, name=Billie, age=24, email=test5@baomidou.com)
-----删除后剩余数据:
User(id=1, name=Jone, age=18, email=test1@baomidou.com)
User(id=2, name=Jack, age=20, email=test2@baomidou.com)
User(id=4, name=Sandy, age=21, email=test4@baomidou.com)
User(id=5, name=Billie, age=24, email=test5@baomidou.com)
3、基于一个实体作为条件,删除一条或多条数据
方法:
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
如果方法的参数设置为null,那么相当于全表删除
代码:
输出结果:
-----删除前数据:
User(id=1, name=Jone, age=18, email=test1@baomidou.com)
User(id=2, name=Jack, age=20, email=test2@baomidou.com)
User(id=3, name=Tom, age=28, email=test3@baomidou.com)
User(id=4, name=Sandy, age=21, email=test4@baomidou.com)
User(id=5, name=Billie, age=24, email=test5@baomidou.com)
==> 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<User> userList = userMapper.selectList(null);
System.out.println("-----删除前数据:");
userList.forEach(System.out::println);
List<Long> list = new ArrayList();
list.add(4L);
list.add(5L);
userMapper.deleteBatchIds(list);
System.out.println("-----删除后剩余数据:");
List<User> userList2 = userMapper.selectList(null);
userList2.forEach(System.out::println);
}
输出:
-----删除前数据:
User(id=1, name=Jone, age=18, email=test1@baomidou.com)
User(id=2, name=Jack, age=20, email=test2@baomidou.com)
User(id=3, name=Tom, age=28, email=test3@baomidou.com)
User(id=4, name=Sandy, age=21, email=test4@baomidou.com)
User(id=5, name=Billie, age=24, email=test5@baomidou.com)
==> 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, test1@baomidou.com
<== Row: 2, Jack, 20, test2@baomidou.com
<== Row: 3, Tom, 28, test3@baomidou.com
<== Total: 3
User(id=1, name=Jone, age=18, email=test1@baomidou.com)
User(id=2, name=Jack, age=20, email=test2@baomidou.com)
User(id=3, name=Tom, age=28, email=test3@baomidou.com)