Mybatis plus update time creation time automatic filling failure situation and solution

Problem Description:

When calling update(Wrapper updateWrapper) in the IService interface of mybatisplus, update_time is not automatically filled in.
spuService.update(updateWrapper);

solution:

Instead of using the update(wrapper xxx) method, use the following methods instead:
1.boolean saveOrUpdate(T entity)
2.boolean saveOrUpdate(T entity, Wrapper updateWrapper)
3.update(T entity, Wrapper updateWrapper)
If you use the third method, Don't use update(null, Wrapper updateWrapper) like this

Cause Analysis:

When implementing automatic filling at the bottom layer, you need to obtain the table structure information corresponding to the instance object. When using the update(wrapper xxx) method, the table structure information is empty, so the filling logic is skipped directly.
Insert image description here
So why saveOrUpdate(T entity) works?
Insert image description here
If you like to use Wrapper to update, you can directly use a new object as an input parameter when calling, and the wrapper passes the specific update results and conditions.
eg: update(new Spu(), updateWrapper)

Autofill preconditions

1. Field definition plus auto-fill annotation

    /**
     * 添加时间
     */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private Date createTime;
 
    /**
     * 修改时间
     */
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;
  1. Implement the MetaObjectHandler interface of mybatis and inject it into the spring container.
    Insert image description here

Guess you like

Origin blog.csdn.net/mcband/article/details/128893816