Mybatisplus更新某个字段为null
使用@TableField(updateStrategy = FieldStrategy.IGNORED)注解要更新的字段。
@TableField注解是 Mybatisplus 框架中提供的一个注解,主要用于实体类(Entity)的字段上,帮助开发者更灵活地映射 Java 对象属性与数据库表字段之间的关系
主要功能:
1、字段映射:当实体类和数据库字段不一致时,可以是使用value属性指定数据库字段名
@TableField(value = "db_column_name")
private String entityFieldName;
2、忽略非表字段:
若实体类中存在与数据库表无关的属性(如临时变量),需标记 exist = false
,避免 MyBatis-Plus 将其误认为表字段。
@TableField(exist = false)
private String tempValue;
3、自动填充策略
配合 fill
属性实现字段的自动填充(如创建时间、更新时间)。
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
4、字段条件处理
通过 condition
属性指定该字段在 SQL 条件中的生成规则(较少用)。
@TableField(condition = SqlCondition.LIKE)
private String name; // 生成 SQL 时会使用 `name LIKE ?`
5、相关策略
@TableField注解有三种策略,
insertStrategy(新增)、updateStrategy(更新)、whereStrategy(查询)
每种策略有
IGNORED(忽略), NOT_NULL(不为null), NOT_EMPTY(不为空), DEFAULT(默认), NEVER(从不) 五种类型