introduction
MyBatis-Plus is a MyBatis enhancement tool that performs excellently in simplifying development and improving efficiency. However, when updating objects with MyBatis-Plus, the field value is not updated to null by default. This is because MyBatis-Plus uses a non-empty field strategy (FieldStrategy), which is configured by default as NOT_NULL, that is, only non-empty fields are updated.
If you need to update the value of certain fields to null, there are several ways to do it. This article will introduce several common methods.
Method 1: Use the set method to manually set the field to null
When updating objects, you can manually set the fields that need to be updated to null to null. MyBatis-Plus then includes these fields in the update statement.
User user = new User(); (1L); (null); // Update the name field to null (user);
Method 2: Use UpdateWrapper or LambdaUpdateWrapper
UpdateWrapper
andLambdaUpdateWrapper
Provides more flexible update conditions, you can use them to specify which fields need to be updated tonull
。
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>(); ("id", 1L) .set("name", null); // Update the name field to null (null, updateWrapper);
Or useLambdaUpdateWrapper
:
LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>(); (User::getId, 1L) .set(User::getName, null); // Update the name field to null (null, lambdaUpdateWrapper);
Method 3: Configure global policy (not recommended)
While it is possible to allow updates to null by configuring the global FieldStrategy to IGNORED, this is usually not recommended because it affects all update operations.
Configure in or:
mybatis-plus: global-config: db-config: field-strategy: ignored # Allow fields are null
Or configure it in the Java configuration class:
@Bean public MybatisPlusConfig customMybatisPlusConfig() { MybatisPlusConfig config = new MybatisPlusConfig(); GlobalConfig globalConfig = new GlobalConfig(); (new DbConfig().setFieldStrategy()); (globalConfig); return config; }
Notice: This method affects global field policy and may cause some unexpected update behavior, so it is not recommended.
Method 4: Custom method (advanced usage)
If you need more complex update logic, you can customize the Mapper method and write custom SQL statements in the XML file.
@Mapper public interface UserMapper extends BaseMapper<User> { @Update("UPDATE user SET name = #{name} WHERE id = #{id}") int updateNameById(@Param("id") Long id, @Param("name") String name); }
Then call the custom method:
(1L, null); // Will name The field is updated to null
Summarize
MyBatis-Plus provides a variety of ways to update field values to null, and you can choose the appropriate method according to your specific needs. Manually setting the field to null and using UpdateWrapper/LambdaUpdateWrapper is the most common and recommended method. Although the global configuration policy is simple, it may affect other update operations and should be used with caution. Custom methods provide greater flexibility and are suitable for complex update logic.
The above is the detailed content of common methods for updating field values to null when MyBatis-Plus is updating objects. For more information about updating field values to null, please pay attention to my other related articles!