首页 > 其他分享> > 解决mybatis@TableField注解默认值赋值,实现MetaObjectHandler后,entity类没有注解也会走insertFill跟updateFill方法赋值
解决mybatis@TableField注解默认值赋值,实现MetaObjectHandler后,entity类没有注解也会走insertFill跟updateFill方法赋值
作者:互联网
package com.bxwell.hj360.pollutionsource.handle; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.toolkit.Constants; import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper; import com.bxwell.hj360.common.base.common.until.BxContext; import com.bxwell.hj360.common.base.common.until.TimeUtil; import com.bxwell.hj360.common.base.common.vo.BxUserInfo; import com.bxwell.hj360.pollutionsource.util.UserUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.SystemMetaObject; import org.springframework.stereotype.Component; import org.springframework.util.ObjectUtils; import java.util.Date; import java.util.Objects; import java.util.Optional; /** * @author hanjq * @create 2021/12/9 * @v1.0.0 */ @Component @Slf4j public class MyMetaObjectHandler implements MetaObjectHandler { /** * 创建人 */ private static final String CREATE_UID = "createUid"; /** * 修改人 */ private static final String MODITY_UID = "modityUid"; private static final String MODIFY_UID = "modifyUid"; /** * 创建时间 */ private static final String CREATE_TIME = "createTime"; /** * 修改时间 */ private static final String MODIFY_TIME = "modifyTime"; /** * 插入元对象字段填充(用于插入时对公共字段的填充) * @param metaObject */ @Override public void insertFill(MetaObject metaObject) { this.setFieldValByNames(CREATE_UID, new StringBuffer(), metaObject,FieldFill.INSERT); this.setFieldValByNames(MODIFY_UID, new StringBuffer(), metaObject,FieldFill.INSERT_UPDATE); this.setFieldValByNames(CREATE_TIME, new Date(), metaObject,FieldFill.INSERT); this.setFieldValByNames(MODIFY_TIME, new Date(), metaObject,FieldFill.INSERT_UPDATE); } /** * 更新元对象字段填充(用于更新时对公共字段的填充) * @param metaObject */ @Override public void updateFill(MetaObject metaObject) { this.setFieldValByNames(MODIFY_TIME, new Date(), metaObject,FieldFill.INSERT_UPDATE); this.setFieldValByNames(MODIFY_UID, new StringBuffer(), metaObject,FieldFill.INSERT_UPDATE); } /** * @Description 重构父级该方法对应字段进行赋值 * @param fieldName * @param fieldVal * @param metaObject * @param fieldFill * @return */ public MetaObjectHandler setFieldValByNames(String fieldName, Object fieldVal, MetaObject metaObject, FieldFill fieldFill) { // FieldFill.INSERT if (metaObject.hasSetter(fieldName) && metaObject.hasGetter(fieldName) && isFills(fieldName, fieldVal, metaObject, fieldFill)) { if (MODITY_UID.equals(fieldName) || MODIFY_UID.equals(fieldName) || CREATE_UID.equals(fieldName)) { metaObject.setValue(fieldName, fieldVal.toString()); } else { metaObject.setValue(fieldName, fieldVal); } } else if (metaObject.hasGetter(Constants.ENTITY)) { // FieldFill.INSERT_UPDATE // 此处是进行修改操作给修改人跟修改时间进行赋值的时候 MetaObject对象的originalObject属性跟保存是结构不一样 具体自己大断点看 Object et = metaObject.getValue(Constants.ENTITY); if (et != null) { MetaObject etMeta = SystemMetaObject.forObject(et); if (etMeta.hasSetter(fieldName) && isFills(fieldName, fieldVal, etMeta, fieldFill)) { if(ObjectUtils.isEmpty(fieldVal)){ etMeta.setValue(fieldName, null); }else if ((MODITY_UID.equals(fieldName) || MODIFY_UID.equals(fieldName) || CREATE_UID.equals(fieldName))) { etMeta.setValue(fieldName, fieldVal.toString()); }else{ etMeta.setValue(fieldName, fieldVal); } } } } return this; } /** * @Description 重构父级该方法 校验是否存在默认值注解 并且对创建人跟修改人赋值 创建时间在一开始就赋值了 * @param fieldName 字段名 * @param fieldVal 字段值 * @param metaObject mybatis对应的entity注解反射类 * @param fieldFill entity注解 * @return */ public boolean isFills(String fieldName, Object fieldVal, MetaObject metaObject, FieldFill fieldFill) { TableInfo tableInfo = metaObject.hasGetter(Constants.MP_OPTLOCK_ET_ORIGINAL) ? TableInfoHelper.getTableInfo(metaObject.getValue(Constants.MP_OPTLOCK_ET_ORIGINAL).getClass()) : TableInfoHelper.getTableInfo(metaObject.getOriginalObject().getClass()); if (Objects.nonNull(tableInfo)) { Optional<TableFieldInfo> first = tableInfo.getFieldList().stream() //v_3.1.1+ 设置子类的值也可以通过 .filter(e -> e.getProperty().equals(fieldName)) .findFirst(); if (first.isPresent()) { //校验是否存在注解 父级方法此处判断无法满足我的需求 所以进行了重构 FieldFill fill = first.get().getFieldFill(); boolean result = fill.equals(fieldFill) || FieldFill.INSERT_UPDATE.equals(fill) || FieldFill.INSERT.equals(fill); if (result) { //当前登录用户赋值 if (StringUtils.isNotBlank(fieldName) && (MODITY_UID.equals(fieldName) || MODIFY_UID.equals(fieldName) || CREATE_UID.equals(fieldName))) { if(fieldVal instanceof StringBuffer){ ((StringBuffer) fieldVal).append(UserUtil.getUserBean().getUid()); } } return true; } } } return false; } }
参考:https://blog.csdn.net/hanjiaqian/article/details/109326009
标签:metaObject,UID,equals,FieldFill,fieldName,import,注解,默认值,赋值 来源: https://www.cnblogs.com/shog808/p/16345152.html