Search in sources :

Example 1 with TableFieldInfo

use of com.baomidou.mybatisplus.core.metadata.TableFieldInfo in project diboot by dibo-software.

the class ServiceAdaptor method optimizeSelect.

/**
 * 基于VO提取最小集select字段
 * @param queryWrapper
 * @param voClass
 */
public static <T> Wrapper<T> optimizeSelect(Wrapper<T> queryWrapper, Class<T> entityClass, Class<?> voClass) {
    if (!(queryWrapper instanceof QueryWrapper) || queryWrapper.getSqlSelect() != null) {
        return queryWrapper;
    }
    List<TableFieldInfo> allColumns = TableInfoHelper.getTableInfo(entityClass).getFieldList();
    if (V.isEmpty(allColumns)) {
        return queryWrapper;
    }
    List<String> columns = new ArrayList<>();
    String pk = ContextHelper.getIdColumnName(entityClass);
    if (V.notEmpty(pk)) {
        columns.add(pk);
    }
    Map<String, Field> fieldsMap = BindingCacheManager.getFieldsMap(voClass);
    for (TableFieldInfo col : allColumns) {
        if (fieldsMap.containsKey(col.getField().getName()) && V.notEmpty(col.getColumn()) && !col.isLogicDelete()) {
            columns.add(col.getSqlSelect());
        }
    }
    // select全部列,不特殊处理
    if (allColumns.size() <= columns.size()) {
        return queryWrapper;
    }
    return ((QueryWrapper) queryWrapper).select(S.toStringArray(columns));
}
Also used : Field(java.lang.reflect.Field) TableFieldInfo(com.baomidou.mybatisplus.core.metadata.TableFieldInfo) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) ArrayList(java.util.ArrayList)

Example 2 with TableFieldInfo

use of com.baomidou.mybatisplus.core.metadata.TableFieldInfo 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)

Example 3 with TableFieldInfo

use of com.baomidou.mybatisplus.core.metadata.TableFieldInfo in project solon by noear.

the class OptimisticLockerInnerInterceptor method doOptimisticLocker.

protected void doOptimisticLocker(Map<String, Object> map, String msId) {
    // updateById(et), update(et, wrapper);
    Object et = map.getOrDefault(Constants.ENTITY, null);
    if (Objects.nonNull(et)) {
        // version field
        TableFieldInfo fieldInfo = this.getVersionFieldInfo(et.getClass());
        if (null == fieldInfo) {
            return;
        }
        try {
            Field versionField = fieldInfo.getField();
            // 旧的 version 值
            Object originalVersionVal = versionField.get(et);
            if (originalVersionVal == null) {
                if (null != exception) {
                    /**
                     * 自定义异常处理
                     */
                    throw exception;
                }
                return;
            }
            String versionColumn = fieldInfo.getColumn();
            // 新的 version 值
            Object updatedVersionVal = this.getUpdatedVersionVal(fieldInfo.getPropertyType(), originalVersionVal);
            String methodName = msId.substring(msId.lastIndexOf(StringPool.DOT) + 1);
            if ("update".equals(methodName)) {
                AbstractWrapper<?, ?, ?> aw = (AbstractWrapper<?, ?, ?>) map.getOrDefault(Constants.WRAPPER, null);
                if (aw == null) {
                    UpdateWrapper<?> uw = new UpdateWrapper<>();
                    uw.eq(versionColumn, originalVersionVal);
                    map.put(Constants.WRAPPER, uw);
                } else {
                    aw.apply(versionColumn + " = {0}", originalVersionVal);
                }
            } else {
                map.put(Constants.MP_OPTLOCK_VERSION_ORIGINAL, originalVersionVal);
            }
            versionField.set(et, updatedVersionVal);
        } catch (IllegalAccessException e) {
            throw ExceptionUtils.mpe(e);
        }
    } else // update(LambdaUpdateWrapper) or update(UpdateWrapper)
    if (wrapperMode && map.entrySet().stream().anyMatch(t -> Objects.equals(t.getKey(), Constants.WRAPPER))) {
        setVersionByWrapper(map, msId);
    }
}
Also used : Field(java.lang.reflect.Field) TableFieldInfo(com.baomidou.mybatisplus.core.metadata.TableFieldInfo) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) AbstractWrapper(com.baomidou.mybatisplus.core.conditions.AbstractWrapper)

Example 4 with TableFieldInfo

use of com.baomidou.mybatisplus.core.metadata.TableFieldInfo in project solon by noear.

the class InsertBatchSomeColumn method injectMappedStatement.

@SuppressWarnings("Duplicates")
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
    KeyGenerator keyGenerator = NoKeyGenerator.INSTANCE;
    SqlMethod sqlMethod = SqlMethod.INSERT_ONE;
    List<TableFieldInfo> fieldList = tableInfo.getFieldList();
    String insertSqlColumn = tableInfo.getKeyInsertSqlColumn(true, false) + this.filterTableFieldInfo(fieldList, predicate, TableFieldInfo::getInsertSqlColumn, EMPTY);
    String columnScript = LEFT_BRACKET + insertSqlColumn.substring(0, insertSqlColumn.length() - 1) + RIGHT_BRACKET;
    String insertSqlProperty = tableInfo.getKeyInsertSqlProperty(true, ENTITY_DOT, false) + this.filterTableFieldInfo(fieldList, predicate, i -> i.getInsertSqlProperty(ENTITY_DOT), EMPTY);
    insertSqlProperty = LEFT_BRACKET + insertSqlProperty.substring(0, insertSqlProperty.length() - 1) + RIGHT_BRACKET;
    String valuesScript = SqlScriptUtils.convertForeach(insertSqlProperty, "list", null, ENTITY, COMMA);
    String keyProperty = null;
    String keyColumn = null;
    // 表包含主键处理逻辑,如果不包含主键当普通字段处理
    if (tableInfo.havePK()) {
        if (tableInfo.getIdType() == IdType.AUTO) {
            /* 自增主键 */
            keyGenerator = Jdbc3KeyGenerator.INSTANCE;
            keyProperty = tableInfo.getKeyProperty();
            keyColumn = tableInfo.getKeyColumn();
        } else {
            if (null != tableInfo.getKeySequence()) {
                keyGenerator = TableInfoHelper.genKeyGenerator(this.methodName, tableInfo, builderAssistant);
                keyProperty = tableInfo.getKeyProperty();
                keyColumn = tableInfo.getKeyColumn();
            }
        }
    }
    String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), columnScript, valuesScript);
    SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
    return this.addInsertMappedStatement(mapperClass, modelClass, getMethod(sqlMethod), sqlSource, keyGenerator, keyProperty, keyColumn);
}
Also used : SqlScriptUtils(com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils) SqlMethod(com.baomidou.mybatisplus.core.enums.SqlMethod) Setter(lombok.Setter) Accessors(lombok.experimental.Accessors) Predicate(java.util.function.Predicate) NoKeyGenerator(org.apache.ibatis.executor.keygen.NoKeyGenerator) AbstractMethod(com.baomidou.mybatisplus.core.injector.AbstractMethod) Jdbc3KeyGenerator(org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator) KeyGenerator(org.apache.ibatis.executor.keygen.KeyGenerator) TableInfoHelper(com.baomidou.mybatisplus.core.metadata.TableInfoHelper) IdType(com.baomidou.mybatisplus.annotation.IdType) List(java.util.List) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) MappedStatement(org.apache.ibatis.mapping.MappedStatement) SqlSource(org.apache.ibatis.mapping.SqlSource) TableFieldInfo(com.baomidou.mybatisplus.core.metadata.TableFieldInfo) TableFieldInfo(com.baomidou.mybatisplus.core.metadata.TableFieldInfo) SqlSource(org.apache.ibatis.mapping.SqlSource) NoKeyGenerator(org.apache.ibatis.executor.keygen.NoKeyGenerator) Jdbc3KeyGenerator(org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator) KeyGenerator(org.apache.ibatis.executor.keygen.KeyGenerator) SqlMethod(com.baomidou.mybatisplus.core.enums.SqlMethod)

Example 5 with TableFieldInfo

use of com.baomidou.mybatisplus.core.metadata.TableFieldInfo in project mybaits-plus-join by Createsequence.

the class AbstractDynamicResultWrapper method initLogicDelete.

/**
 * 若主表不为逻辑表,则获取其逻辑删除字段并添加到条件中
 *
 * @author huangchengxing
 * @date 2022/2/10 9:21
 */
protected void initLogicDelete() {
    if (isLogic || !tableInfo.isWithLogicDelete()) {
        return;
    }
    TableFieldInfo logicDeleteFieldInfo = tableInfo.getLogicDeleteFieldInfo();
    String notDeleteValue = tableInfo.getLogicDeleteFieldInfo().getLogicDeleteValue();
    whereIfNotNull(new TableColumn(this, logicDeleteFieldInfo.getColumn()), Condition.EQ, notDeleteValue);
}
Also used : TableFieldInfo(com.baomidou.mybatisplus.core.metadata.TableFieldInfo) TableColumn(top.xiajibagao.mybatis.plus.join.wrapper.column.TableColumn)

Aggregations

TableFieldInfo (com.baomidou.mybatisplus.core.metadata.TableFieldInfo)10 SqlMethod (com.baomidou.mybatisplus.core.enums.SqlMethod)5 SqlSource (org.apache.ibatis.mapping.SqlSource)5 AbstractWrapper (com.baomidou.mybatisplus.core.conditions.AbstractWrapper)3 TableInfo (com.baomidou.mybatisplus.core.metadata.TableInfo)3 IdType (com.baomidou.mybatisplus.annotation.IdType)2 AbstractMethod (com.baomidou.mybatisplus.core.injector.AbstractMethod)2 TableInfoHelper (com.baomidou.mybatisplus.core.metadata.TableInfoHelper)2 SqlScriptUtils (com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils)2 Field (java.lang.reflect.Field)2 List (java.util.List)2 Predicate (java.util.function.Predicate)2 Setter (lombok.Setter)2 Accessors (lombok.experimental.Accessors)2 Jdbc3KeyGenerator (org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator)2 KeyGenerator (org.apache.ibatis.executor.keygen.KeyGenerator)2 NoKeyGenerator (org.apache.ibatis.executor.keygen.NoKeyGenerator)2 MappedStatement (org.apache.ibatis.mapping.MappedStatement)2 ISqlSegment (com.baomidou.mybatisplus.core.conditions.ISqlSegment)1 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)1