Introduction
MetaObjectHandler
It is a very useful component used to handle field filling logic in entity objects, such as automatically filling fields such as creating time, updating time, creating person, modifying person, etc.
Design concept
In modern applications, the maintenance of database records often involves public fields such as "create time" and "update time". If you manually process these fields in each insert or update operation, it is not only prone to errors, but also adds redundancy to the code. MetaObjectHandler was designed to simplify this process so that the processing of these common fields becomes automated and unaware.
Advantages of automatic filling
Automatic filling not only reduces code redundancy, but also improves data consistency and integrity. With automatic fill, developers can ensure that each database operation follows the same rules, avoiding human errors. In addition, automatic filling can improve development efficiency and enable developers to focus on the implementation of business logic.
Component introduction
MetaObjectHandler
The interface allows fields in the entity class to be automatically filled without modifying the business code. This is usually used to record metadata information such as creation time, update time, create person, modify person, etc. For example, the creation time is automatically set when the user registers, and the last modification time is automatically updated when the user information is updated.
This implements automatic filling of the creator and modifying person
Leave the creation time, modification time, etc. toMySQL
Automatic filling
MetaObjectHandler provides the following core methods:
- void insertFill(MetaObject metaObject): Automatically fill the field when processing insertion.
- void updateFill(MetaObject metaObject): Automatically fill the fields when processing updates.
- MetaObjectHandler setFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject): Used to set the value of the specified field.
- Object getFieldValByName(String fieldName, MetaObject metaObject): Gets the value of the specified field.
- TableInfo findTableInfo(MetaObject metaObject): Get the corresponding table information based on MetaObject.
Default implementation
MyBatis-Plus provides the default implementation class for MetaObjectHandler. Developers can customize the field fill logic when inserting and updating by inheriting this class.
Strict filling strategy
By default, MetaObjectHandler follows a strict fill policy, which means that the value is not overwritten if the field already has a value, but is only filled when the field is empty. This strategy ensures data consistency and prevents unnecessary overwriting.
use
accomplishMetaObjectHandler
Interface:
First, you need to create a class implementationMetaObjectHandler
Interface and override the methods in it.
package ; import ; import ; import ; import ; /** * @program: ZK * @description: Custom mubatisplust fill * @author: zk * @create: 2024-07-17 11:30 **/ @Component public class ZKMetaObjectHandler implements MetaObjectHandler { /** * Create a person field */ private static final String CREATE_USER_ID = "createUserId"; /** * Modify person field */ private static final String UPDATE_USER_ID = "updateUserId"; @Override public void insertFill(MetaObject metaObject) { (metaObject, true); } @Override public void updateFill(MetaObject metaObject) { (metaObject, false); } private void setUserId(MetaObject metaObject, boolean isInsert) { Long userId = (); if (userId > 0) { if (isInsert) { (CREATE_USER_ID, userId, metaObject); } (UPDATE_USER_ID, userId, metaObject); } } }
Automatically fill in new/update via insertFill and updateFill
UserThreadLocalUtil is ThreadLocal, which mainly stores user information of the current thread.
This is the article about the use of the MetaObjectHandler component in Mybatis-Plus. For more related contents of Mybatis-Plus, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!