mybatisplus 添加 /修改 数据 插入公共字段

@TableField(value = "CREATED_BY", fill = FieldFill.INSERT)
private String createdBy;
@TableField(value = "CREATED_TIME", fill = FieldFill.INSERT)
private Date createdTime;
@TableField(value = "UPDATED_BY", fill = FieldFill.UPDATE)
private String updatedBy;
@TableField(value = "UPDATED_TIME", fill = FieldFill.UPDATE)
private Date updatedTime;


/**
* 自动填充功能
* @return
*/
@Bean
public GlobalConfig globalConfig() {
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setMetaObjectHandler(new MetaHandler());
return globalConfig;
}



import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.vimtech.common.util.ShiroUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
* @Date 2020-02-24 09:57
* @Author sulg
* @Description 公共字段填充
*/
@Component
public class MetaHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createdBy", ShiroUtils.getUserName(), metaObject);
this.setFieldValByName("createdTime", new Date(), metaObject);
}

@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updatedBy", ShiroUtils.getUserName(), metaObject);
this.setFieldValByName("updatedTime", new Date(), metaObject);
}
}
posted @ 2020-11-25 16:58  升级打怪猴  阅读(1332)  评论(0编辑  收藏  举报