mp一共提供了8个注解,这些注解是用在Java的实体类上面的
- @TableName 数据库表相关
- @TableId 表主键标识
- @TableField(exist = false):表示该属性不为数据库表字段,但又是必须使用的。
- @TableField(exist = true):表示该属性为数据库表字段。
- @Version 乐观锁注解
- @EnumValue 注解在枚举字段上
- @TableLogic 表字段逻辑处理注解(逻辑删除)
- KeySequence 序列主键策略
- InterceptorIgnore 插件过滤规则
ID主键策略
AUTO
数据库ID自增,依赖于数据库。在插入操作生成SQL语句时,不会插入主键这一列
NONE
未设置主键类型。若在代码中没有手动设置主键,则会根据主键的全局策略自动生成(默认的主键全局策略是基于雪花算法的自增ID)
INPUT
需要手动设置主键,若不设置。插入操作生成SQL语句时,主键这一列的值会是null。oracle的序列主键需要使用这种方式
ASSIGN_ID
当没有手动设置主键,即实体类中的主键属性为空时,才会自动填充,使用雪花算法
ASSIGN_UUID
当实体类的主键属性为空时,才会自动填充,使用UUID
注解实例
# 枚举包扫描
mybatis-plus.type-enums-package=com.md.enums
# 没有删除为0,删除了为1
mybatis-plus.global-config.db-config.logic-not-delete-value=0
mybatis-plus.global-config.db-config.logic-delete-value=1
@Data
@TableName(value = "student")
public class Student {
@TableId
private Long id;
@TableField(value = "name")
private String name;
@TableField(select = false)
private Integer age;
@TableField(exist = false)
private String gender;
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
@Version
private Integer version;
private StatusEnum status;
@TableLogic
private Integer deleted;
}