Search in sources :

Example 6 with TableField

use of com.baomidou.mybatisplus.annotation.TableField in project albedo by somowhere.

the class Wraps method getDbField.

/**
 * 根据 bean字段 反射出 数据库字段
 *
 * @param beanField 字段
 * @param clazz     类型
 * @return 数据库字段名
 */
public static String getDbField(String beanField, Class<?> clazz) {
    ArgumentAssert.notNull(clazz, "实体类不能为空");
    ArgumentAssert.notEmpty(beanField, "字段名不能为空");
    Field field = ReflectUtil.getField(clazz, beanField);
    ArgumentAssert.notNull(field, "在类:{}中找不到属性:{}", clazz.getSimpleName(), beanField);
    TableField tf = field.getAnnotation(TableField.class);
    if (tf != null && StrUtil.isNotEmpty(tf.value())) {
        return tf.value();
    }
    TableId ti = field.getAnnotation(TableId.class);
    if (ti != null && StrUtil.isNotEmpty(ti.value())) {
        return ti.value();
    }
    throw BizException.wrap("{}.{} 未标记 @TableField 或 @TableId", clazz.getSimpleName(), beanField);
}
Also used : TableId(com.baomidou.mybatisplus.annotation.TableId) Field(java.lang.reflect.Field) TableField(com.baomidou.mybatisplus.annotation.TableField) TableField(com.baomidou.mybatisplus.annotation.TableField)

Example 7 with TableField

use of com.baomidou.mybatisplus.annotation.TableField in project muses by acgist.

the class FilterQuery method column.

/**
 * 通过MyBatis注解获取数据库列名
 *
 * @param <T> 类型
 *
 * @param entity entity
 * @param name Java字段名称
 *
 * @return 数据库列名
 */
private static final <T> String column(Class<T> entity, final String name) {
    final Map<String, String> map = COLUMN_CACHE.computeIfAbsent(entity, key -> new ConcurrentHashMap<>());
    return map.computeIfAbsent(name, key -> {
        final Field field = FieldUtils.getField(entity, name, true);
        if (field == null) {
            return name;
        }
        final TableField tableField = field.getAnnotation(TableField.class);
        if (tableField != null && StringUtils.isNotEmpty(tableField.value())) {
            return tableField.value();
        }
        final TableId tableId = field.getAnnotation(TableId.class);
        if (tableId != null && StringUtils.isNotEmpty(tableId.value())) {
            return tableId.value();
        }
        return name;
    });
}
Also used : TableId(com.baomidou.mybatisplus.annotation.TableId) Field(java.lang.reflect.Field) TableField(com.baomidou.mybatisplus.annotation.TableField) TableField(com.baomidou.mybatisplus.annotation.TableField)

Example 8 with TableField

use of com.baomidou.mybatisplus.annotation.TableField in project chao-cloud by chaojunzi.

the class MybatisUtil method getColumn.

/**
 * 根据实体获取列名
 *
 * @param func 表达式
 * @return column
 */
static <T, R> String getColumn(SFunction<T, R> func) {
    SerializedLambda lambda = SerializedLambda.resolve(func);
    // 获取实体类型
    Class<?> entityClass = lambda.getImplClass();
    // 获取方法类型
    String fieldName = StrUtil.getGeneralField(lambda.getImplMethodName());
    // 获取属性
    Field field = ReflectUtil.getField(entityClass, fieldName);
    TableField tableField = AnnotationUtil.getAnnotation(field, TableField.class);
    if (tableField != null) {
        String column = tableField.value();
        if (StrUtil.isNotBlank(column)) {
            return column;
        }
    }
    return StrUtil.toUnderlineCase(fieldName);
}
Also used : Field(java.lang.reflect.Field) TableField(com.baomidou.mybatisplus.annotation.TableField) TableField(com.baomidou.mybatisplus.annotation.TableField) SerializedLambda(com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda)

Example 9 with TableField

use of com.baomidou.mybatisplus.annotation.TableField in project diboot by dibo-software.

the class QueryBuilder method dtoToWrapper.

/**
 * 转换具体实现
 *
 * @param dto
 * @return
 */
private static <DTO> QueryWrapper<?> dtoToWrapper(DTO dto, Collection<String> fields) {
    QueryWrapper<?> wrapper;
    // 转换
    LinkedHashMap<String, FieldAndValue> fieldValuesMap = extractNotNullValues(dto, fields);
    if (V.isEmpty(fieldValuesMap)) {
        return new QueryWrapper<>();
    }
    // 只解析有值的
    fields = fieldValuesMap.keySet();
    // 是否有join联表查询
    boolean hasJoinTable = ParserCache.hasJoinTable(dto, fields);
    if (hasJoinTable) {
        wrapper = new DynamicJoinQueryWrapper<>(dto.getClass(), fields);
    } else {
        wrapper = new ExtQueryWrapper<>();
    }
    // 构建 ColumnName
    List<AnnoJoiner> annoJoinerList = ParserCache.getBindQueryAnnos(dto.getClass());
    BiFunction<BindQuery, Field, String> buildColumnName = (bindQuery, field) -> {
        if (bindQuery != null) {
            String key = field.getName() + bindQuery;
            for (AnnoJoiner annoJoiner : annoJoinerList) {
                if (key.equals(annoJoiner.getKey())) {
                    if (V.notEmpty(annoJoiner.getJoin())) {
                        // 获取注解Table
                        return annoJoiner.getAlias() + "." + annoJoiner.getColumnName();
                    } else {
                        return (hasJoinTable ? "self." : "") + annoJoiner.getColumnName();
                    }
                }
            }
        }
        return (hasJoinTable ? "self." : "") + BeanUtils.getColumnName(field);
    };
    // 忽略空字符串"",空集合等
    BiFunction<Object, BindQuery, Boolean> ignoreEmpty = (value, bindQuery) -> bindQuery != null && // 忽略空字符串"",空集合等
    bindQuery.strategy().equals(Strategy.IGNORE_EMPTY) && (// 字符串""
    value instanceof String && S.isEmpty((String) value) || // 空集合
    (value instanceof Collection && ((Collection<?>) value).size() == 0));
    // 查找加密策略
    BiFunction<BindQuery, String, IEncryptStrategy> findEncryptStrategy = (bindQuery, defFieldName) -> {
        if (ENABLE_DATA_PROTECT) {
            Class<?> clazz = bindQuery == null || bindQuery.entity() == NullType.class ? dto.getClass() : bindQuery.entity();
            String fieldName = bindQuery == null || S.isEmpty(bindQuery.field()) ? defFieldName : bindQuery.field();
            return ParserCache.getFieldEncryptorMap(clazz).get(fieldName);
        }
        return null;
    };
    // 构建QueryWrapper
    for (Map.Entry<String, FieldAndValue> entry : fieldValuesMap.entrySet()) {
        FieldAndValue fieldAndValue = entry.getValue();
        Field field = fieldAndValue.getField();
        // 忽略注解 @TableField(exist = false) 的字段
        TableField tableField = field.getAnnotation(TableField.class);
        if (tableField != null && !tableField.exist()) {
            continue;
        }
        // 忽略字段
        BindQuery query = field.getAnnotation(BindQuery.class);
        if (query != null && query.ignore()) {
            continue;
        }
        BindQuery.List queryList = field.getAnnotation(BindQuery.List.class);
        Object value = fieldAndValue.getValue();
        // 构建Query
        if (queryList != null) {
            wrapper.and(queryWrapper -> {
                for (BindQuery bindQuery : queryList.value()) {
                    if (ignoreEmpty.apply(value, bindQuery)) {
                        continue;
                    }
                    IEncryptStrategy encryptor = findEncryptStrategy.apply(bindQuery, entry.getKey());
                    Comparison comparison = encryptor == null ? bindQuery.comparison() : Comparison.EQ;
                    String columnName = buildColumnName.apply(bindQuery, field);
                    buildQuery(queryWrapper.or(), comparison, columnName, encryptor == null ? value : encryptor.encrypt(value.toString()));
                }
            });
        } else {
            if (ignoreEmpty.apply(value, query)) {
                continue;
            }
            IEncryptStrategy encryptor = findEncryptStrategy.apply(query, entry.getKey());
            Comparison comparison = query != null && encryptor == null ? query.comparison() : Comparison.EQ;
            String columnName = buildColumnName.apply(query, field);
            buildQuery(wrapper, comparison, columnName, encryptor == null ? value : encryptor.encrypt(value.toString()));
        }
    }
    return wrapper;
}
Also used : AnnoJoiner(com.diboot.core.binding.query.dynamic.AnnoJoiner) Comparison(com.diboot.core.binding.query.Comparison) java.util(java.util) BiFunction(java.util.function.BiFunction) Cons(com.diboot.core.config.Cons) LoggerFactory(org.slf4j.LoggerFactory) ExtQueryWrapper(com.diboot.core.binding.query.dynamic.ExtQueryWrapper) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) PropertiesUtils(com.diboot.core.util.PropertiesUtils) Method(java.lang.reflect.Method) BeanUtils(com.diboot.core.util.BeanUtils) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Logger(org.slf4j.Logger) NullType(javax.lang.model.type.NullType) TableLogic(com.baomidou.mybatisplus.annotation.TableLogic) Field(java.lang.reflect.Field) TableField(com.baomidou.mybatisplus.annotation.TableField) IEncryptStrategy(com.diboot.core.data.encrypt.IEncryptStrategy) InvocationTargetException(java.lang.reflect.InvocationTargetException) DynamicJoinQueryWrapper(com.diboot.core.binding.query.dynamic.DynamicJoinQueryWrapper) S(com.diboot.core.util.S) V(com.diboot.core.util.V) Modifier(java.lang.reflect.Modifier) ISqlSegment(com.baomidou.mybatisplus.core.conditions.ISqlSegment) NormalSegmentList(com.baomidou.mybatisplus.core.conditions.segments.NormalSegmentList) BindQuery(com.diboot.core.binding.query.BindQuery) ParserCache(com.diboot.core.binding.parser.ParserCache) Strategy(com.diboot.core.binding.query.Strategy) Field(java.lang.reflect.Field) TableField(com.baomidou.mybatisplus.annotation.TableField) BindQuery(com.diboot.core.binding.query.BindQuery) Comparison(com.diboot.core.binding.query.Comparison) IEncryptStrategy(com.diboot.core.data.encrypt.IEncryptStrategy) ExtQueryWrapper(com.diboot.core.binding.query.dynamic.ExtQueryWrapper) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) DynamicJoinQueryWrapper(com.diboot.core.binding.query.dynamic.DynamicJoinQueryWrapper) TableField(com.baomidou.mybatisplus.annotation.TableField) AnnoJoiner(com.diboot.core.binding.query.dynamic.AnnoJoiner) NullType(javax.lang.model.type.NullType)

Example 10 with TableField

use of com.baomidou.mybatisplus.annotation.TableField in project katoumegumi_all by 353259576.

the class FieldColumnRelationMapperFactory method ignoreField.

/**
 * 是否忽略field
 *
 * @param field
 * @return
 */
public static boolean ignoreField(Field field) {
    Transient aTransient = field.getAnnotation(Transient.class);
    if (aTransient != null) {
        return true;
    }
    TableField tableField = field.getAnnotation(TableField.class);
    return tableField != null && !tableField.exist();
}
Also used : TableField(com.baomidou.mybatisplus.annotation.TableField)

Aggregations

TableField (com.baomidou.mybatisplus.annotation.TableField)11 Field (java.lang.reflect.Field)9 TableId (com.baomidou.mybatisplus.annotation.TableId)5 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)4 java.util (java.util)4 JSON (com.alibaba.fastjson.JSON)3 PropertyDescriptor (java.beans.PropertyDescriptor)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 BigDecimal (java.math.BigDecimal)3 URLDecoder (java.net.URLDecoder)3 ParseException (java.text.ParseException)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 Collectors (java.util.stream.Collectors)3 Slf4j (lombok.extern.slf4j.Slf4j)3 PropertyUtils (org.apache.commons.beanutils.PropertyUtils)3 CommonConstant (org.jeecg.common.constant.CommonConstant)3 DataBaseConstant (org.jeecg.common.constant.DataBaseConstant)2 JeecgDataAutorUtils (org.jeecg.common.system.util.JeecgDataAutorUtils)2