mybatis |
您所在的位置:网站首页 › mybatisplus联合主键怎么映射 › mybatis |
Mybatis Plus的删除操作内置了4种方法,我们来演示一下Mybatis Plus的删除操作。 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: Preparing: SELECT id,name,age,email FROM user ==> Parameters: Preparing: DELETE FROM user ==> Parameters: 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) Preparing: SELECT id,name,age,email FROM user ==> Parameters: |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |