MyBatis Plus-9-Automatic Filling of Common Fields

Keywords: Programming Mybatis Attribute Spring

Automatic Filling of Common Fields

1.1 Metadata Processor Interface

com.baomidou.mybatisplus.mapper.MetaObjectHandler

insertFill(MetaObject metaObject)
updateFill(MetaObject metaObject)

metaobject: metaobject. It is an object provided by Mybatis for accessing the properties of objects more conveniently and elegantly, setting values for the properties of objects. It can also be used for wrapping objects. It supports wrapping objects such as Object, Map, Collection, etc.

Essentially, metaObject obtains the attribute value of the object or sets the value of the attribute of the object. Finally, it obtains the Invoker of the corresponding method of the attribute through the Reflector, and finally invoke s.

2.1 Development Steps

  1. Annotation Fill Field @TableFile(fill = FieldFill.INSERT) View FieldFill
  2. Custom Common Field Filling Processor
  3. MP global injection custom public field filling processor

3.1 examples

  • Implementing meta-object processor interface: com.baomidou.mybatisplus.mapper.IMetaObjectHandler
  • The annotation fill field @TableField(.. fill = FieldFill.INSERT) Generator Policy section can also be configured!
public class User {

    /**
     * DEFAULT(0, "Default does not process ",
     * INSERT(1, "Insert the fill field ",
     * UPDATE(2, "Update Fill Field ",
     * INSERT_UPDATE(3, "Insert and update the fill field ";
     */
     
    // Be careful! This needs to be marked as a fill in field
    @TableField(.. fill = FieldFill.INSERT)
    private String fillField;

    ....
}
  • Custom Implementation Class MyMetaObjectHandler
/**
 * Custom Common Field Filling Processor
 *
 * @author dyz
 * @program mybatis-plus
 * @create 2019-09-05 13:17
 */
public class MyMetaObjectHandler extends MetaObjectHandler {

    /**
     * Insert operation automatic filling
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        //Gets the value of the field that needs to be filled
        Object fieldValue = getFieldValByName("name", metaObject);
        if (fieldValue == null) {
            System.out.println("*******Insert operation satisfies filling condition*********");
            setFieldValByName("name", "don", metaObject);
        }
    }

    /**
     * Automatic Filling of Modification Operations
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        Object fieldValue = getFieldValByName("name", metaObject);
        if (fieldValue == null) {
            System.out.println("*******Modification operation satisfies filling condition*********");
            setFieldValByName("name", "don", metaObject);
        }
    }

}

Special note: ** One point to fill in the field annotations, see the problem section of the document! * *

  • spring starts injecting MyMetaObjectHandler configuration
    <!-- MyBatis SqlSessionFactoryBean To configure -->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
        <property name="globalConfig" ref="globalConfig"></property>
    </bean>
    
    <bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
        <!-- Common Field Filling Processor -->
        <property name="metaObjectHandler" ref="myMetaObjectHandler" />
    </bean>
    
    <!-- Custom Processor -->
    <bean id="myMetaObjectHandler" class="com.don.mp.metaObjectHandler.MyMetaObjectHandler"/>
  • test
    /**
     * Test common field filling
     */
    @Test
    public void testMetaObjectHandler() {

        User user = new User();

        user.setId(1);
        user.setLogicFlag(1);

        userMapper.updateById(user);

    }
  • Result
    UPDATE tbl_user SET `name`=?, logic_flag=? WHERE id=? AND logic_flag=1

Posted by MsShelle on Thu, 03 Oct 2019 21:41:22 -0700