import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* 拦截处理公共字段
*/
@Primary
@Component
public class CommonColumnHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
if(metaObject.getOriginalObject() instanceof CommonBean){
String userId = SessionUtil.currentUserId();
CommonBean bean = (CommonBean) metaObject.getOriginalObject();
bean.setCreateTime(new Date());
bean.setUpdateTime(bean.getCreateTime());
if(StringUtils.isNotBlank(userId)){
bean.setCreateUser(userId);
bean.setUpdateUser(userId);
}
if(StringUtils.isBlank(bean.getId())){
bean.setId(StringTool.uuid());
}
}
}
@Override
public void updateFill(MetaObject metaObject) {
if(metaObject.getOriginalObject() instanceof CommonBean){
CommonBean bean = (CommonBean) metaObject.getOriginalObject();
bean.setUpdateTime(new Date());
String userId = SessionUtil.currentUserId();
if(StringUtils.isNotBlank(userId)){
bean.setUpdateUser(userId);
}
}
}
}
上述实现类,程序根本就没有进去,保存方法如下
整个上面的写法只更新了实体的两个属性,所以没有触发实现类的填充代码,下面是整个实体的更新,就能够进入实现类。
因此只有整个实体更新或保存才能触发填充代码