MyBatis-Plus删除操作
# 删除
# 根据 id 删除记录
@Test
public void testDeleteById() {
// 返回影响的行数
int result = userMapper.deleteById(5L);
System.out.println(result);
}
1
2
3
4
5
6
2
3
4
5
6
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@77cb9cd1] was not registered for synchronization because synchronization is not active
2022-02-23 22:07:50.206 INFO 96832 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-02-23 22:07:50.491 INFO 96832 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@677329142 wrapping com.mysql.cj.jdbc.ConnectionImpl@4e1459ea] will not be managed by Spring
==> Preparing: DELETE FROM user WHERE id=?
==> Parameters: 1(Long)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@77cb9cd1]
1
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
DELETE FROM user WHERE id=1
1
# 批量删除
和前面查询多个 id 的是一样的,传入一个id
集合即可进行批量删除
@Test
public void testDeleteBatchIds() {
int result = userMapper.deleteBatchIds(Arrays.asList(8, 9, 10));
System.out.println(result);
}
1
2
3
4
5
2
3
4
5
# 简单条件删除
和前面简单条件查询差不多,无非就是查询变成了删除,构建查询条件的过程是一样的。
@Test
public void testDeleteByMap() {
HashMap<String, Object> map = new HashMap<>();
map.put("name", "张三");
map.put("age", 20);
int result = userMapper.deleteByMap(map);
System.out.println(result);
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 物理删除和逻辑删除
- 物理删除:真实删除,将对应数据从数据库中删除,之后查询不倒此条被删除数据
- 逻辑删除:假删除,将对应数据中代表是否被删除字段修改为”被删除状态“,之后在数据库中仍旧能看到此条数据记录
逻辑删除的使用场景:
- 可以进行数据恢复
- 有关联数据,不便删除
操作:
表里添加字段,对应实体类添加属性,作为逻辑删除标志
添加deleted
字段
alter table user add column int(11) default 0;
1
然后再实体类里加上属性
private Integer deleted;
1
在属性上添加注解
@TableLogic
@TableField(fill = FieldFill.INSERT)
private Integer deleted;
1
2
3
2
3
添加上自动填充之后,也要在对应的元数据处理类中添加,给deleted
属性默认值为 0
/**
* MP 执行添加操作的时候这个方法就会执行
*
* @param metaObject
*/
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime", new Date(), metaObject);
this.setFieldValByName("updateTime", new Date(), metaObject);
this.setFieldValByName("version", 1, metaObject);
this.setFieldValByName("deleted", 0, metaObject);
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
我们做完上面步骤之后来进行测试,先使用之前添加的添加测试方法添加数据,然后再用上面的根据id
删除数据的测试方法进行测试
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c6a6c1d] was not registered for synchronization because synchronization is not active
2022-02-23 22:24:26.402 INFO 97350 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-02-23 22:24:26.666 INFO 97350 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@1472494238 wrapping com.mysql.cj.jdbc.ConnectionImpl@642505c7] will not be managed by Spring
==> Preparing: UPDATE user SET deleted=1 WHERE id=? AND deleted=0
==> Parameters: 1496490958768881666(Long)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c6a6c1d]
1
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
可以看到对应的 SQL 为
UPDATE user SET deleted=1 WHERE id=1496490958768881666 AND deleted=0
1
最终它的标志为从 0 变成了 1,即完成了逻辑删除。
此时我们再来进行查询最初的查询所有的数据的测试方法
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@453d496b] was not registered for synchronization because synchronization is not active
2022-02-23 22:26:52.211 INFO 97405 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-02-23 22:26:52.468 INFO 97405 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@1043882455 wrapping com.mysql.cj.jdbc.ConnectionImpl@5e2a3040] will not be managed by Spring
==> Preparing: SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0
==> Parameters:
<== Columns: id, name, age, email, create_time, update_time, version, deleted
<== Row: 2, jack, 19, dwq@qq.com, null, null, null, 0
<== Row: 3, tom, 20, tom@qq.com, null, null, null, 0
<== Row: 1494318945161748482, 无解的修改, 20, 12@qq.com, null, null, null, 0
<== Row: 1495319145393180674, 无解的修改测试时间, 20, 12@qq.com, 2022-02-20 16:47:21, 2022-02-20 16:48:54, null, 0
<== Row: 1495378144188751873, 张三, 20, 12@qq.com, 2022-02-20 20:41:47, 2022-02-20 20:52:14, 2, 0
<== Total: 5
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@453d496b]
[User(id=2, name=jack, age=19, email=dwq@qq.com, createTime=null, updateTime=null, version=null, deleted=0), User(id=3, name=tom, age=20, email=tom@qq.com, createTime=null, updateTime=null, version=null, deleted=0), User(id=1494318945161748482, name=无解的修改, age=20, email=12@qq.com, createTime=null, updateTime=null, version=null, deleted=0), User(id=1495319145393180674, name=无解的修改测试时间, age=20, email=12@qq.com, createTime=Sun Feb 20 16:47:21 CST 2022, updateTime=Sun Feb 20 16:48:54 CST 2022, version=null, deleted=0), User(id=1495378144188751873, name=张三, age=20, email=12@qq.com, createTime=Sun Feb 20 20:41:47 CST 2022, updateTime=Sun Feb 20 20:52:14 CST 2022, version=2, deleted=0)]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
对应的 SQL 为:
SELECT id,name,age,email,create_time,update_time,version,deleted FROM user WHERE deleted=0
1
我们配置了逻辑删除的标志位,我们进行查询时,会自动加上标志位过滤。
编辑 (opens new window)
上次更新: 2022/02/23, 22:31:06