Search in sources :

Example 1 with Update

use of com.baomidou.mybatisplus.core.conditions.update.Update in project solon by noear.

the class OptimisticLockerInnerInterceptor method setVersionByWrapper.

private void setVersionByWrapper(Map<String, Object> map, String msId) {
    Object ew = map.get(Constants.WRAPPER);
    if (null != ew && ew instanceof AbstractWrapper && ew instanceof Update) {
        Class<?> entityClass = ENTITY_CLASS_CACHE.get(msId);
        if (null == entityClass) {
            try {
                final String className = msId.substring(0, msId.lastIndexOf('.'));
                entityClass = GenericTypeUtil.getSuperClassGenericType(Class.forName(className), Mapper.class, 0);
                ENTITY_CLASS_CACHE.put(msId, entityClass);
            } catch (ClassNotFoundException e) {
                throw ExceptionUtils.mpe(e);
            }
        }
        final TableFieldInfo versionField = getVersionFieldInfo(entityClass);
        if (null == versionField) {
            return;
        }
        final String versionColumn = versionField.getColumn();
        final FieldEqFinder fieldEqFinder = new FieldEqFinder(versionColumn, (Wrapper<?>) ew);
        if (!fieldEqFinder.isPresent()) {
            return;
        }
        final Map<String, Object> paramNameValuePairs = ((AbstractWrapper<?, ?, ?>) ew).getParamNameValuePairs();
        final Object originalVersionValue = paramNameValuePairs.get(fieldEqFinder.valueKey);
        if (originalVersionValue == null) {
            return;
        }
        final Object updatedVersionVal = getUpdatedVersionVal(originalVersionValue.getClass(), originalVersionValue);
        if (originalVersionValue == updatedVersionVal) {
            return;
        }
        // 拼接新的version值
        paramNameValuePairs.put(UPDATED_VERSION_VAL_KEY, updatedVersionVal);
        ((Update<?, ?>) ew).setSql(String.format("%s = #{%s.%s}", versionColumn, "ew.paramNameValuePairs", UPDATED_VERSION_VAL_KEY));
    }
}
Also used : Mapper(com.baomidou.mybatisplus.core.mapper.Mapper) TableFieldInfo(com.baomidou.mybatisplus.core.metadata.TableFieldInfo) AbstractWrapper(com.baomidou.mybatisplus.core.conditions.AbstractWrapper) Update(com.baomidou.mybatisplus.core.conditions.update.Update)

Aggregations

AbstractWrapper (com.baomidou.mybatisplus.core.conditions.AbstractWrapper)1 Update (com.baomidou.mybatisplus.core.conditions.update.Update)1 Mapper (com.baomidou.mybatisplus.core.mapper.Mapper)1 TableFieldInfo (com.baomidou.mybatisplus.core.metadata.TableFieldInfo)1