1. Problem description
Recently, I was on vacation at home. I suddenly received a call and said that it was because of business feedback that a certain field should be empty in production. After the operation, the information returned was successful, but there was no response. So I immediately turned on the computer to see what was going on.
public Result<CstGrpInfoDto> edit(@Valid @RequestBody CstGrpInfoDto cstGrpInfoDto) { Result<CstGrpInfoDto> result = new Result<CstGrpInfoDto>(); CstGrpInfo cstGrpInfoEo = (()); if (cstGrpInfoEo == null) { result.error400("No corresponding entity found"); } else { (cstGrpInfoDto, cstGrpInfoEo); try { (cstGrpInfoEo); ("The modification was successful!"); } catch (Exception e) { ((), e); result.error400("The modification failed!"); } } return result; }
The general logic is like this, very simple code.
At first I wondered if it was BeanUtils's fault? So when DEBUG checked, I found that there was no problem with the attribute value of EO, and the problem could only be in the updateById method of mybatisPlus.
By calling to view the log, I found that the SQL spliced with the updateById method does not contain fields with the null part of the property value. So I speculated that mybatisPlus will not be modified to null fields, and it is true that it is true when searching online.
2. Solution
Method 1. Update using UpdateWrapper
This method is suitable for explicit fields that need to be modified.
Example:
public int updateProduct(String productCode) { UpdateWrapper<Product> wrapper = new UpdateWrapper<>(); ().eq(Product::getProductCode, productCode) .set(Product::getName, null); return getBaseMapper().update(null, wrapper); }
Method 2. Set a separate field-strategy for a field
According to the specific situation, adjust the verification annotations in the fields that need to be updated, as follows
@TableField(strategy = ) private String name;
Method 3. Set the global field-strategy (use with caution)
import ; import ; import ; import ; import ; @Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // Add paging plugin (new PaginationInnerInterceptor()); return interceptor; } @Bean public GlobalConfig globalConfig() { GlobalConfig globalConfig = new GlobalConfig(); // Set global update policy dbConfig = new (); (); // Set to ignore null value (dbConfig); return globalConfig; } }
properties file format:
-strategy=ignored
yml file format:
mybatis-plus: global-config: db-config: update-strategy: ignored # Set to Ignore null value
Recommendations for use: It is recommended to choose which method to use according to the actual business situation. For data with default values and cannot be null, it is recommended to useDefault policy。
This is the end of this article about the issue of ignoring null values for Mybatis-Plus update data. For more related content about ignoring null values for Mybatis-Plus update data, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!