mybatisPlus踩坑之--自动填充

这两天在做接口,数据库层采用的是mubatisPlus,在插入数据时,正常逻辑是只是插入create等信息,但是updateTime也自动插入。琢磨了两个小时,还是没找到问题所在,避免影响工期,最后手写SQL。

今天又有类似的功能开发,所以我还是想偷懒的用mybatisPlus,结果还是同样的问题,我已开始我以为是 fill填充不对

@TableField(fill = FieldFill.INSERT_UPDATE)
protected Date updateTime; // 更新日期

将 @TableField(fill = FieldFill.INSERT_UPDATE) 改成  @TableField(fill = FieldFill.UPDATE)

也可以解决问题,但是需要重写公司框架的DataEntity。

后面使用 FieldFill 查找了mybatisPlus 官方文档,终于发现问题:MetaObjectHandler 实现元对象处理器接口 里面配置有问题,在insert时也配置了update字段,所以导致数据插入异常。

直接上代码:

/**
 * mybatisplus自定义填充公共字段 ,即没有传的字段自动填充
 * @author updateBy 
 * @version 1.0.2 执行insert操作,不自动插入更新字段
 */
@Component
public class MyMetaObjectHandler extends MetaObjectHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyMetaObjectHandler.class);

    //新增填充
    @Override
    public void insertFill(MetaObject metaObject) {
    	UserInfo userInfo = ContextUtils.getUserInfo();
        String currentUser = "超级管理员";
        String currentUserId = "system";
        if(userInfo == null) {
    		ServiceContext context = ServiceContext.getContext();
    		if(context != null) {
    			currentUserId = StringUtils.isNotEmpty(context.getUserId()) ? context.getUserId() : "system";
    			currentUser = StringUtils.isNotEmpty(context.getUserName()) ? context.getUserName() : "system";
    		}
    	}else {
    		currentUser = userInfo.getNickname();
            currentUserId = userInfo.getUsername();
    	}
        //LOGGER.info("insert公共字段填充Start");
        Date now = new Date();
        Object id = getFieldValByName("id", metaObject);
        Object createUserId = getFieldValByName("createUserId", metaObject);
        Object createTime = getFieldValByName("createTime", metaObject);
        Object createUser = getFieldValByName("createUser", metaObject);
//        Object updateUser = getFieldValByName("updateUser", metaObject);
//        Object updateUserId = getFieldValByName("updateUserId", metaObject);
//        Object updateTime = getFieldValByName("updateTime", metaObject);
        //SysUser user = (SysUser)SecurityUtils.getSubject().getPrincipal();
        if (id == null) {
            setFieldValByName("id", UuidUtil.get32UUID(), metaObject); //mybatis-plus版本2.0.9+
        }
        if (createUserId == null) {
            setFieldValByName("createUserId", currentUserId, metaObject); //mybatis-plus版本2.0.9+
        }
        if (createTime == null) {
            setFieldValByName("createTime", now, metaObject); //mybatis-plus版本2.0.9+
        }
        if (createUser == null) {
            setFieldValByName("createUser", currentUser, metaObject); //mybatis-plus版本2.0.9+
        }
//        if (updateUser == null) {
//            setFieldValByName("updateUser", currentUser, metaObject); //mybatis-plus版本2.0.9+
//        }
//        if (updateUserId == null) {
//            setFieldValByName("updateUserId", currentUserId, metaObject); //mybatis-plus版本2.0.9+
//        }
//        if (updateTime == null) {
//            setFieldValByName("updateTime", now, metaObject); //mybatis-plus版本2.0.9+
//        }
        LOGGER.info("公共字段填充end");
    }

    //更新填充
    @Override
    public void updateFill(MetaObject metaObject) {
    	UserInfo userInfo = ContextUtils.getUserInfo();
    	if(userInfo == null) {
    		ServiceContext context = ServiceContext.getContext();
    		userInfo = new UserInfo();
    		userInfo.setUsername(context.getUserId());
    		userInfo.setNickname(context.getUserName());
    	}
    	String currentUser = userInfo != null ? userInfo.getNickname() : "system";
        String currentUserId = userInfo != null ? userInfo.getUsername() : "system";
        //LOGGER.info("update公共字段填充Start");
        Object updateUser = getFieldValByName("updateUser", metaObject);
        Object updateUserId = getFieldValByName("updateUserId", metaObject);
        Object updateTime = getFieldValByName("updateTime", metaObject);
        if (updateUser == null) {
            setFieldValByName("updateUser", currentUser, metaObject); //mybatis-plus版本2.0.9+
        }
        if (updateUserId == null) {
            setFieldValByName("updateUserId", currentUserId, metaObject); //mybatis-plus版本2.0.9+
        }
        if (updateTime == null) {
            setFieldValByName("updateTime", new Date(), metaObject); //mybatis-plus版本2.0.9+
        }
        //LOGGER.info("update公共字段填充end");
    }
}

注释掉update字段,完美解决问题。

结语:

1.解决问题,搜对关键字很重要

2. INSERT_UPDATE 和 UPDATE 到底有啥区别,请各位大佬帮忙解释下。

你可能感兴趣的:(数据库,mybatis)