SoFunction
Updated on 2025-04-11

Setting mybatis and mybatis-plus to null does not work and the problem and solution

Mybatis-plus

FieldStrategy function

The role of the Mybatis-Plus field strategy FieldStrategy is mainly to determine whether to judge the value of the entity object based on the configured policy when adding or updating it. If the policy is a field that cannot be empty, the empty field will not be assigned or updated.

Similarly, when conducting where condition query, it is determined whether to make a null value judgment on the field based on the whereStrategy strategy. If the policy is a field that cannot be empty, the empty field will not be assembled into the where condition as a query condition.

Three configurations, corresponding to three usage scenarios:

  • insertStrategy field strategy during insert operation, whether to judge the null value, insert the null value
  • updateStrategy field strategy during update operation, whether to judge the null value, insert the null value
  • WhereStrategy Whether to control the judgment when the where condition is assembled, use the empty value as the query condition

FieldStrategy Type

There are 5 policy types in the source code of FieldStrategy

public enum FieldStrategy {
    IGNORED,
    NOT_NULL,
    NOT_EMPTY,
    DEFAULT,
    NEVER;

    private FieldStrategy() {
    }
}

The role of each strategy

value	                     describe
IGNORED	     忽略空valuejudge,实体对象的字段是什么value就用什么value更新,supportnullvalue更新操作
NOT_NULL	     Perform non-NULLjudge,It's also the default policy,Equivalent toage!=null
NOT_EMPTY	 Perform non-空judge,Mainly for string types,Equivalent toname != null and name != ‘'
NEVER	        Never update,不管字段是否有value,No updates are made
DEFAULT	    Follow the global configuration

Global policy configuration

In the global configuration, the default values ​​of the three are FieldStrategy.NOT_NULL, that is, null value judgment is performed, and NULL value data is not processed.

public DbConfig() {
     = IdType.ASSIGN_ID;
     = true;
     = false;
     = "1";
     = "0";
     = FieldStrategy.NOT_NULL;
     = FieldStrategy.NOT_NULL;
     = FieldStrategy.NOT_NULL;
}

In spring boot, you can modify global field policies by configuring properties

-strategy=not_null
-strategy=not_null
-strategy=not_null

@TableField annotation source code

@Documented
@Retention()
@Target({, ElementType.ANNOTATION_TYPE})
public @interface TableField {
    String value() default "";

    boolean exist() default true;

    String condition() default "";

    String update() default "";

    //Insert policy    FieldStrategy insertStrategy() default ;

    //Update the policy    FieldStrategy updateStrategy() default ;
    
    // where conditional strategy    FieldStrategy whereStrategy() default ;

    FieldFill fill() default ;

    boolean select() default true;

    boolean keepGlobalFormat() default false;

    String property() default "";

    JdbcType jdbcType() default ;

    Class<? extends TypeHandler> typeHandler() default ;

    boolean javaType() default false;

    String numericScale() default "";
}

Among them, the default policies of insertStrategy, updateStrategy and whereStrategy are all, indicating that they follow the global configuration.

Ignore judgment-IGNORED

@TableName(value ="user")
@Data
public class User implements Serializable {
   @TableId
   private Long id;
   private String name;
   private Integer age;
   @TableField(updateStrategy = )
   private String email;
}

Never handled - NEVER (identification specified fields are never updated)

@TableName(value ="user")
@Data
public class User implements Serializable {
   @TableId
   private Long id;
   private String name;
   private Integer age;
   @TableField(updateStrategy = )
   private String email;
}

This indicates that the policy will not only ignore the null value judgment of the field value, but will not perform update operations regardless of whether the identified field has a value.

Characters are not empty -NOT_EMPTY

  • The strategy FieldStrategy.NOT_EMPTY means that the string needs to be judged empty value, and only fields that are not empty strings will participate in data processing.
  • If judgment condition in the xml file equivalent to mybatis: name != null and name != ‘’

Follow the global - DEFAULT

  • A policy represents a field policy that follows the global configuration, which is also the default policy at the field level.
  • The global field strategy is FieldStrategy.NOT_NULL by default.

mybatis

Mybatis will not have this problem, because there is a if tag judgment in the xml file. If you don't need to judge, delete judgment

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.