前言

应用过mybatis-plus的敌人可能会晓得,通过实现元对象处理器接口com.baomidou.mybatisplus.core.handlers.MetaObjectHandler能够实现字段填充性能。但如果在更新实体,应用boolean update(Wrapper<T> updateWrapper)这个办法进行更新时,则主动填充会生效。明天就来聊聊这个话题,本文例子应用的mybatis-plus版本为3.1.2版本

为何应用boolean update(Wrapper<T> updateWrapper),主动填充会生效?

mybatis-plus 3.1.2版本跟踪源码,能够得悉,主动填充的调用代码实现逻辑是由上面的外围代码块实现

 /**     * 自定义元对象填充控制器     *     * @param metaObjectHandler 元数据填充处理器     * @param tableInfo         数据库表反射信息     * @param ms                MappedStatement     * @param parameterObject   插入数据库对象     * @return Object     */    protected static Object populateKeys(MetaObjectHandler metaObjectHandler, TableInfo tableInfo,                                         MappedStatement ms, Object parameterObject, boolean isInsert) {        if (null == tableInfo) {            /* 不解决 */            return parameterObject;        }        /* 自定义元对象填充控制器 */        MetaObject metaObject = ms.getConfiguration().newMetaObject(parameterObject);        // 填充主键        if (isInsert && !StringUtils.isEmpty(tableInfo.getKeyProperty())            && null != tableInfo.getIdType() && tableInfo.getIdType().getKey() >= 3) {            Object idValue = metaObject.getValue(tableInfo.getKeyProperty());            /* 自定义 ID */            if (StringUtils.checkValNull(idValue)) {                if (tableInfo.getIdType() == IdType.ID_WORKER) {                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getId());                } else if (tableInfo.getIdType() == IdType.ID_WORKER_STR) {                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getIdStr());                } else if (tableInfo.getIdType() == IdType.UUID) {                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.get32UUID());                }            }        }        if (metaObjectHandler != null) {            if (isInsert && metaObjectHandler.openInsertFill()) {                // 插入填充                metaObjectHandler.insertFill(metaObject);            } else if (!isInsert) {                // 更新填充                metaObjectHandler.updateFill(metaObject);            }        }        return metaObject.getOriginalObject();    }

从源码剖析咱们能够得悉当tableInfo为null时,是不走主动填充逻辑。而tableInfo又是什么从中央进行取值,持续跟踪源码,咱们得悉tableInfo能够由底下代码获取

 if (isFill) {            Collection<Object> parameters = getParameters(parameterObject);            if (null != parameters) {                List<Object> objList = new ArrayList<>();                for (Object parameter : parameters) {                    TableInfo tableInfo = TableInfoHelper.getTableInfo(parameter.getClass());                    if (null != tableInfo) {                        objList.add(populateKeys(metaObjectHandler, tableInfo, ms, parameter, isInsert));                    } else {                        /*                         * 非表映射类不解决                         */                        objList.add(parameter);                    }                }                return objList;            } else {                TableInfo tableInfo = null;                if (parameterObject instanceof Map) {                    Map<?, ?> map = (Map<?, ?>) parameterObject;                    if (map.containsKey(Constants.ENTITY)) {                        Object et = map.get(Constants.ENTITY);                        if (et != null) {                            if (et instanceof Map) {                                Map<?, ?> realEtMap = (Map<?, ?>) et;                                if (realEtMap.containsKey(Constants.MP_OPTLOCK_ET_ORIGINAL)) {                                    tableInfo = TableInfoHelper.getTableInfo(realEtMap.get(Constants.MP_OPTLOCK_ET_ORIGINAL).getClass());                                }                            } else {                                tableInfo = TableInfoHelper.getTableInfo(et.getClass());                            }                        }                    }                } else {                    tableInfo = TableInfoHelper.getTableInfo(parameterObject.getClass());                }

从源码能够很分明看出,tableInfo 的获取依赖parameterObject.getClass(),则这个parameterObject就是数据库插入或者更新对象。即咱们的实体对象,当实体对象为null时,则tableInfo 的值也是为null,这就会导致主动填充生效

咱们再来看下boolean update(Wrapper<T> updateWrapper)这个代码的底层实现

default boolean update(Wrapper<T> updateWrapper) {        return this.update((Object)null, updateWrapper);    }

通过代码咱们能够晓得,当应用这个办法时,其实体对象是null,导致调用主动填充办法时,失去的tableInfo是null,因此无奈进入主动填充实现逻辑,因而导致填充主动生效

如何解决update(Wrapper<T> updateWrapper),主动填充不失效问题

通过源码剖析咱们得悉,只有tableInfo不为空,则就会进入主动填充逻辑,而tableInfo不为空的前提是更新或者插入的实体不是null对象,因而咱们的思路就是在调用update办法时,要确保实体不为null

计划一:实体更新时,间接应用update(Wrapper<T> updateWrapper)的重载办法boolean update(T entity, Wrapper<T> updateWrapper)

示例:

msgLogService.update(new MsgLog(),lambdaUpdateWrapper)

计划二:重写update(Wrapper<T> updateWrapper)办法

重写update的办法思路有如下

办法一:重写ServiceImpl的update办法

其外围思路如下,重写一个业务基类BaseServiceImpl

public class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T>  {    /**     *      *     * @param updateWrapper     * @return     */    @Override    public boolean update(Wrapper<T> updateWrapper) {        T entity = updateWrapper.getEntity();        if (null == entity) {            try {                entity = this.currentModelClass().newInstance();            } catch (InstantiationException e) {               e.printStackTrace();            } catch (IllegalAccessException e) {                e.printStackTrace();            }        }        return update(entity, updateWrapper);    }}

业务service去继承BaseServiceImpl,形如下

@Servicepublic class MsgLogServiceImpl extends BaseServiceImpl<MsgLogDao, MsgLog> implements MsgLogService {}
办法二:通过动静代理去重写update(Wrapper<T> updateWrapper)

其外围代码如下

@Aspect@Component@Slf4jpublic class UpdateWapperAspect implements ApplicationContextAware {    private ApplicationContext applicationContext;    private  Map<String,Object> entityMap = new HashMap<>();    @Pointcut("execution(* com.baomidou.mybatisplus.extension.service.IService.update(com.baomidou.mybatisplus.core.conditions.Wrapper))")    public void pointcut(){    }    @Around(value = "pointcut()")    public Object around(ProceedingJoinPoint pjp){        Object updateEnityResult = this.updateEntity(pjp);        if(ObjectUtils.isEmpty(updateEnityResult)){            try {                return pjp.proceed();            } catch (Throwable throwable) {                throwable.printStackTrace();            }        }        return updateEnityResult;    }    @Override    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {        this.applicationContext = applicationContext;    }    /**     *重写update(Wrapper<T> updateWrapper), 更新时主动填充不失效问题     * @param pjp     * @return     */    private Object updateEntity(ProceedingJoinPoint pjp){        Object[] args = pjp.getArgs();        if(args != null && args.length == 1){            Object arg = args[0];            if(arg instanceof Wrapper){                Wrapper updateWrapper = (Wrapper)arg;                Object entity = updateWrapper.getEntity();                IService service = (IService) applicationContext.getBean(pjp.getTarget().getClass());                if(ObjectUtils.isEmpty(entity)){                    entity = entityMap.get(pjp.getTarget().getClass().getName());                    if(ObjectUtils.isEmpty(entity)){                        Class entityClz = ReflectionKit.getSuperClassGenericType(pjp.getTarget().getClass(), 1);                        try {                            entity = entityClz.newInstance();                        } catch (InstantiationException e) {                            log.warn("Entity instantiating exception!");                        } catch (IllegalAccessException e) {                            log.warn("Entity illegal access exception!");                        }                        entityMap.put(pjp.getTarget().getClass().getName(),entity);                    }                }                return service.update(entity,updateWrapper);            }        }        return null;    }}

总结

文章结尾始终在指明mybatis-plus版本,是因为我跟过mybatis-plus3.1版本、3.3版本、3.4版本的主动填充的调用源码,其源码的实现各有不同,因为我github上的mybatis-plus援用的版本是3.1.2版本,因而就以3.1.2版本进行剖析。不过其余版本的剖析思路大同小异,都是去跟踪什么中央调用了主动填充的逻辑。

至于解决方案的几种思路,说下我的集体倡议,如果我的项目初期的话,做好宣导,倡议应用计划一,间接应用update(new MsgLog(),lambdaUpdateWrapper)这种写法。如果我的项目开发到肯定水平了,发现很多中央都存在更新主动填充生效,则举荐应用间接底层重写update的计划

demo链接

https://github.com/lyb-geek/springboot-learning/tree/master/springboot-mybatisplus-tenant