Confirm the version
First, confirm whether the mybatis-plus version used in your project is 3.3.0 or 3.3.0 or above (including 3.3.0). The insertFill and updateFill of the MetaObjectHandler method are written differently in the lower and higher versions, such as the following example:
@Bean public MetaObjectHandler metaObjectHandler(){ return new MetaObjectHandler() { @Override public void insertFill(MetaObject metaObject) { ("createdDate", new Date(), metaObject); ("updatedDate", new Date(), metaObject); } @Override public void updateFill(MetaObject metaObject) { ("updatedDate", new Date(), metaObject); } }; }
This is a customization using earlier versions of MyBatis-Plus (before 3.3.0)MetaObjectHandler
accomplish. It usessetFieldValByName
Method to setcreatedDate
andupdatedDate
The value of the field. This method usestype.
@Bean public MetaObjectHandler metaObjectHandler(){ return new MetaObjectHandler() { @Override public void insertFill(MetaObject metaObject) { (metaObject, "createdDate", , new Date()); (metaObject, "updatedDate", , new Date()); } @Override public void updateFill(MetaObject metaObject) { (metaObject, "updatedDate", , ()); } }; }
This is a more advanced fill strategy introduced in MyBatis-Plus 3.3.0 and above. It usesstrictInsertFill
andstrictUpdateFill
Methods, these methods are more type-safe and also provide more flexible fill options. This version is recommendedType, which conforms to the time API introduced in Java 8 and later, providing better date and time processing capabilities.
Detailed comparison:
Type safety:
In the second way of writing,strictInsertFill
andstrictUpdateFill
Methods allow you to specify the data type of the target field (e.g.), thus avoiding type conversion errors.
In the first way of writing,setFieldValByName
The method requires you to ensure the consistency of the field type and the assignment type yourself.
Default value is provided:
In the second way of writing, you can directly pass one()
Expressions, or use lambda expressions() -> ()
to provide a default value for delayed execution. This may be more useful in some cases, such as when your default values depend on some business logic.
The first writing method is used directlynew Date()
Date instances are created, and no mechanism for delayed execution is provided.
Compatibility and bug fixes:
The second way to write it is the latest practice recommended by MyBatis-Plus, which usually means it has better compatibility and fixes for known issues.
The first writing method may no longer be supported in the new version, or there are unresolved bugs, such asfillStrategy
The method has a bug in version 3.3.0.
Check configuration
First, introduce a higher version of pom dependency:
<dependency> <groupId></groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.0</version> </dependency>
Write mybatis-plus configuration class
/** * MyBatis-Plus configuration class */ @MapperScan("") // Define the mapper path in the project@Configuration public class MybatisPlusConfig { /** * mybatis-plus autofill configuration */ @Bean public MetaObjectHandler metaObjectHandler(){ return new MetaObjectHandler() { @Override public void insertFill(MetaObject metaObject) { /** * The first parameter metaObject contains the entity class you passed in * The second parameter must be consistent with the attribute name in the entity class * The third parameter must be consistent with the attribute type in the entity class. * The fourth parameter is the padding default value you set */ (metaObject, "createdDate", , new Date()); (metaObject, "updatedDate", , new Date()); } @Override public void updateFill(MetaObject metaObject) { (metaObject, "updatedDate", , new Date()); } }; } }
In entity classes, you need to use@TableField
Annotation marks which fields need to be automatically filled and specifies the filling policy.
public class User { @TableField(fill = ) private String createTime; @TableField(fill = ) private String updateTime; // Other fields...}
The difference between delete and update methods
In MyBatis-Plus, the difference in behavior between the `delete` method and the `update` method mainly stems from their different timing and way of calling the autofill logic defined in `MetaObjectHandler`.
Forupdate
method:
When you use the update method, MyBatis-Plus will call the updateFill method of MetaObjectHandler. This is because the update method is essentially an update operation, which triggers the autofill logic, especially those fields marked as or FieldFill.INSERT_UPDATE.
Fordelete
method:
When using the delete method, the situation is different. By default, the delete method does not trigger the updateFill method of MetaObjectHandler, even if you are using logical deletion (i.e., the delete method actually executes a UPDATE statement to modify a logical deletion flag). This is because in the design of MyBatis-Plus, the purpose of the delete method is to delete records. Whether it is physical or logical deletion, it will not trigger the automatic filling logic of the update field.
However, when you use the @TableLogic annotation in the entity class and configure logical deletion, the MyBatis-Plus delete method will execute a UPDATE statement to change the value of the logical delete field, but it will not execute the updateFill method to update other fields, such as updatedDate. This is the design decision of MyBatis-Plus, which treats delete and update as different operations, and the delete method does not trigger the regular update field fill even in the case of logical deletion.
Things to note
- If you manually set the autofill value, then the MyBatis-Plus autofill policy will not overwrite this value.
- Fields must be declared
@TableField
Annotation and setfill
Properties to select the fill policy. - The fill processor needs to be declared as
@Component
or@Bean
。 - exist
update(T entity, Wrapper<T> updateWrapper)
hour,entity
It cannot be empty, otherwise the automatic filling will fail. - exist
update(Wrapper<T> updateWrapper)
It will not be automatically filled and field conditions need to be assigned manually.
This is the article about solving the problem of Mybatis-Plus autofill failure. For more related content about Mybatis-Plus autofill failure, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!