When the default mybatic-plus performs updateById and other operations, if the relevant field content is null, it will not be updated automatically. This is a pit and you need to pay attention to it.
Cause of the problem
There are three strategies for mybatis-plus FieldStrategy:
- IGNORED: 0 Ignored
- NOT_NULL: 1 Non NULL, default policy
- NOT_EMPTY: 2 Non-empty
andThe default update policy is NOT_NULL
: Non-NULL; that is, when the data is updated through the interface, it will not be updated into the database when the data is NULL value.
Solution
1. Set the global field-strategy
properties file format:
-strategy=ignored
yml file format:
mybatis-plus: global-config: #Field Strategy 0: "Ignore judgment", 1: "Non-NULL judgment", 2: "Non-null judgment" field-strategy: 0
This is a global configuration, and all fields will be ignored. If some fields do not want to be modified but are not passed when passing values, they will be updated to null, which may affect the accuracy of other business data.
2. Set a separate field-strategy for a field
Depending on the specific situation, adjust the verification annotation in the fields that need to be updated, such as verification is not empty:
@TableField(strategy=FieldStrategy.NOT_EMPTY)
In this way, we only need to set the ignorance policy on the fields that need to be updated to null, as follows:
@TableField(strategy = ) private String dutyJson;
In the update code, we can update successfully using the updateById method in mybatis-plus, as follows:
/** * updateById update field is null * @param id * @return */ @Override public boolean updateProductById(Integer id) { InsuranceProduct insuranceProduct = ((id)).orElseThrow(RuntimeException::new); (null); (insuranceProduct); }
Using the above method, if there are many fields that need to be processed in this way, it will need to add this annotation to each field, which seems a bit troublesome. Then, you can consider using the third method, which can be updated successfully without adding annotations to the fields.
3. Update using UpdateWrapper (recommended)
In mybatis-plus, in addition to the updateById method, an update method is also provided. You can also set the field to null directly using the update method. The code is as follows:
/** * Dutyjson for updating product responsibility based on product unique code */ public int updateProduct(String productCode) { InsuranceProduct old = lambdaQuery().eq(InsuranceProduct::getProductCode, productCode).one(); UpdateWrapper<InsuranceProduct> wrapper = new UpdateWrapper<>(); ().eq(InsuranceProduct::getProductCode, productCode) .set(InsuranceProduct::getDutyJson, null) .eq(InsuranceProduct::getDeleted, 0); return getBaseMapper().update(old, wrapper); }
This method does not affect other methods, does not require modifying the global configuration, nor does it require individual annotations on the fields, so this method is recommended.
This is the article about solving the problem of Mybatis-plus null value update not taking effect. For more related content related to Mybatis-plus null value update not taking effect, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!