mybatis-plus逻辑删除
官方文档
# 步骤1:
配置com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig
- 例: application.yml
mybatis-plus:
global-config:
db-config:
logic-delete-field: flag # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
1
2
3
4
5
6
2
3
4
5
6
# 步骤2:
实体类字段上加上@TableLogic
注解
@TableLogic
private Integer deleted;
1
2
2
如果某张表,逻辑删除跟该微服务的全局逻辑删除配置相反,还可以自定义。
源码:
/*
* Copyright (c) 2011-2020, baomidou (jobob@qq.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.annotation;
import java.lang.annotation.*;
/**
* 表字段逻辑处理注解(逻辑删除)
*
* @author hubin
* @since 2017-09-09
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
public @interface TableLogic {
/**
* 默认逻辑未删除值(该值可无、会自动获取全局配置)
*/
String value() default "";
/**
* 默认逻辑删除值(该值可无、会自动获取全局配置)
*/
String delval() default "";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
也就是说,问只需要在字段上指明已删除和未删除的值
/**
* 是否显示[0-不显示,1显示]
*/
@TableLogic(delval = "0",value = "1")
private Integer showStatus;
1
2
3
4
5
2
3
4
5
帮我改善此页面 (opens new window)
上次更新: 2021/02/16, 12:29:08