First, the public field is automatically populated Profile
As the name suggests: that is, when inserting or updating data, if the data item is not set, the default values will be filled
Here it will involve metadata processing interface and its two methods:
com.baomidou.mybatisplus.mapper.MetaObjectHandler
insertFill (MetaObject as Metaobject) This method is inserted in the data automatically populated when the logical method
updateFill (MetaObject metaObject) This method is to update the data logic automatically populated when the method of
And MetaObject This class is a wrapper object MyBatis provides for better access to information related to the object.
Second, the common field is automatically populated configured as follows
2.1, add annotations for the entity attributes required to automatically fill
public class User { @TableId(type=IdType.AUTO) private Integer id; @TableField (Fill = FieldFill.INSERT_UPDATE) // when inserted and will update the field that filling Private String name; @TableLogic // flag is a logical symbol for Private Integer deleteFlag; // set methods // get methods }
@TableFiled annotation fill this property offers three Autofill settings
2.2, create a class that inherits MetaObjectHandler
The method to achieve the appropriate, how to automatically fill in the insert and update data logic
public class MyMetaObjectHandler extends MetaObjectHandler{ // logical method for automatically filling insertion @Override public void insertFill (MetaObject as Metaobject) { // the TODO Auto-Generated Stub Method System.out.println ( "Method ### calls insertFill ###" ); Object obj = getFieldValByName("name", metaObject); if (obj == null) { setFieldValByName ( "name", "filling insert Test" , as Metaobject); } } // logical method for automatically updating the filling @Override public void updateFill (MetaObject as Metaobject) { // the TODO Auto-Generated Stub Method System.out.println ( "Method ### calls updateFill ###" ); Object obj = getFieldValByName("name", metaObject); if (obj == null) { setFieldValByName ( "name", "Update fill Test" , as Metaobject); } } }
2.3, is configured in the configuration file applicationContext.xml
<-! MyBatis-PLUS global configuration policies, thus avoiding the repeated use of annotations in each entity configuration -> < the bean ID = "globalConfiguration" class = "com.baomidou.mybatisplus.entity.GlobalConfiguration" > < Property name = "dbColumnUnderline" value = "to true" > </ Property > <! - the default configuration database underscore version 2.3 -> <! - Specifies the database ID generation strategy 0: increment database -> < Property name = " idtype " value =" 0 " > </ Property > <-! specified database table prefix -> <property name="tablePrefix" value="tbl_"></property> <!-- 注入处理器方法 --> <property name="metaObjectHandler" ref="myMetaObjectHandler"></property> </bean> <!-- 注入处理器的bean --> <bean id="myMetaObjectHandler" class="cn.hjj.mp.handler.MyMetaObjectHandler"></bean>
2.4, the following test code
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:applicationContext.xml"}) public class TestMetaObjectHandler { @Autowired private UserMapper userMapper; @Test public void testInsert() { User user = new User(); user.setDeleteFlag(1); userMapper.insert(user); } @Test public void testUpdate () { User user = new User(); user.setId(4); user.setDeleteFlag(1); userMapper.updateById(user); } }