Search in sources :

Example 1 with TableField

use of com.baomidou.mybatisplus.annotation.TableField in project jeecg-boot by jeecgboot.

the class QueryGenerator method getTableFieldName.

/**
 * 获取表字段名
 * @param clazz
 * @param name
 * @return
 */
private static String getTableFieldName(Class<?> clazz, String name) {
    try {
        // 如果字段加注解了@TableField(exist = false),不走DB查询
        Field field = null;
        try {
            field = clazz.getDeclaredField(name);
        } catch (NoSuchFieldException e) {
        // e.printStackTrace();
        }
        // 如果为空,则去父类查找字段
        if (field == null) {
            List<Field> allFields = getClassFields(clazz);
            List<Field> searchFields = allFields.stream().filter(a -> a.getName().equals(name)).collect(Collectors.toList());
            if (searchFields != null && searchFields.size() > 0) {
                field = searchFields.get(0);
            }
        }
        if (field != null) {
            TableField tableField = field.getAnnotation(TableField.class);
            if (tableField != null) {
                if (tableField.exist() == false) {
                    // 如果设置了TableField false 这个字段不需要处理
                    return null;
                } else {
                    String column = tableField.value();
                    // 如果设置了TableField value 这个字段是实体字段
                    if (!"".equals(column)) {
                        return column;
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return name;
}
Also used : java.util(java.util) PropertyUtils(org.apache.commons.beanutils.PropertyUtils) URLDecoder(java.net.URLDecoder) NumberUtils(org.springframework.util.NumberUtils) SimpleDateFormat(java.text.SimpleDateFormat) SysPermissionDataRuleModel(org.jeecg.common.system.vo.SysPermissionDataRuleModel) BigDecimal(java.math.BigDecimal) Matcher(java.util.regex.Matcher) org.jeecg.common.util.oConvertUtils(org.jeecg.common.util.oConvertUtils) ParseException(java.text.ParseException) SqlInjectionUtil(org.jeecg.common.util.SqlInjectionUtil) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) JeecgDataAutorUtils(org.jeecg.common.system.util.JeecgDataAutorUtils) JwtUtil(org.jeecg.common.system.util.JwtUtil) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) DataBaseConstant(org.jeecg.common.constant.DataBaseConstant) TableField(com.baomidou.mybatisplus.annotation.TableField) Slf4j(lombok.extern.slf4j.Slf4j) JSON(com.alibaba.fastjson.JSON) PropertyDescriptor(java.beans.PropertyDescriptor) Pattern(java.util.regex.Pattern) CommonConstant(org.jeecg.common.constant.CommonConstant) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CommonUtils(org.jeecg.common.util.CommonUtils) DateUtils(org.jeecg.common.util.DateUtils) Field(java.lang.reflect.Field) TableField(com.baomidou.mybatisplus.annotation.TableField) TableField(com.baomidou.mybatisplus.annotation.TableField) ParseException(java.text.ParseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with TableField

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

the class FieldColumnRelationMapperFactory method createFieldColumnRelation.

public static FieldColumnRelation createFieldColumnRelation(Field field) {
    Annotation[] annotations = field.getAnnotations();
    boolean isId = false;
    String columnName = null;
    boolean getId = false;
    boolean getColumn = false;
    if (WsListUtils.isNotEmpty(annotations)) {
        for (Annotation annotation : annotations) {
            if (!getColumn && annotation instanceof Column) {
                columnName = ((Column) annotation).name();
                getColumn = true;
            } else if (!getColumn && annotation instanceof TableField) {
                columnName = ((TableField) annotation).value();
                getColumn = true;
            } else if (annotation instanceof TableId) {
                isId = true;
                columnName = ((TableId) annotation).value();
                break;
            } else if (!getId && annotation instanceof Id) {
                isId = true;
                getId = true;
            }
        }
    }
    if (WsStringUtils.isBlank(columnName)) {
        columnName = getChangeColumnName(field.getName());
    }
    return new FieldColumnRelation(isId, field.getName(), field, columnName, field.getType());
}
Also used : TableId(com.baomidou.mybatisplus.annotation.TableId) TableId(com.baomidou.mybatisplus.annotation.TableId) TableField(com.baomidou.mybatisplus.annotation.TableField) Annotation(java.lang.annotation.Annotation)

Example 3 with TableField

use of com.baomidou.mybatisplus.annotation.TableField in project kykms by mahonelau.

the class QueryGenerator method getTableFieldName.

/**
 * 获取表字段名
 * @param clazz
 * @param name
 * @return
 */
private static String getTableFieldName(Class<?> clazz, String name) {
    try {
        // 如果字段加注解了@TableField(exist = false),不走DB查询
        Field field = null;
        try {
            field = clazz.getDeclaredField(name);
        } catch (NoSuchFieldException e) {
        // e.printStackTrace();
        }
        // 如果为空,则去父类查找字段
        if (field == null) {
            List<Field> allFields = getClassFields(clazz);
            List<Field> searchFields = allFields.stream().filter(a -> a.getName().equals(name)).collect(Collectors.toList());
            if (searchFields != null && searchFields.size() > 0) {
                field = searchFields.get(0);
            }
        }
        if (field != null) {
            TableField tableField = field.getAnnotation(TableField.class);
            if (tableField != null) {
                if (tableField.exist() == false) {
                    // 如果设置了TableField false 这个字段不需要处理
                    return null;
                } else {
                    String column = tableField.value();
                    // 如果设置了TableField value 这个字段是实体字段
                    if (!"".equals(column)) {
                        return column;
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return name;
}
Also used : java.util(java.util) PropertyUtils(org.apache.commons.beanutils.PropertyUtils) URLDecoder(java.net.URLDecoder) NumberUtils(org.springframework.util.NumberUtils) SimpleDateFormat(java.text.SimpleDateFormat) SysPermissionDataRuleModel(org.jeecg.common.system.vo.SysPermissionDataRuleModel) BigDecimal(java.math.BigDecimal) Matcher(java.util.regex.Matcher) org.jeecg.common.util.oConvertUtils(org.jeecg.common.util.oConvertUtils) ParseException(java.text.ParseException) SqlInjectionUtil(org.jeecg.common.util.SqlInjectionUtil) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) JeecgDataAutorUtils(org.jeecg.common.system.util.JeecgDataAutorUtils) JwtUtil(org.jeecg.common.system.util.JwtUtil) Field(java.lang.reflect.Field) TableField(com.baomidou.mybatisplus.annotation.TableField) DataBaseConstant(org.jeecg.common.constant.DataBaseConstant) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) JSON(com.alibaba.fastjson.JSON) PropertyDescriptor(java.beans.PropertyDescriptor) CommonConstant(org.jeecg.common.constant.CommonConstant) Pattern(java.util.regex.Pattern) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CommonUtils(org.jeecg.common.util.CommonUtils) DateUtils(org.jeecg.common.util.DateUtils) Field(java.lang.reflect.Field) TableField(com.baomidou.mybatisplus.annotation.TableField) TableField(com.baomidou.mybatisplus.annotation.TableField) ParseException(java.text.ParseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 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 Field field = FieldUtils.findField(entity, name);
    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 5 with TableField

use of com.baomidou.mybatisplus.annotation.TableField in project lamp-util by zuihou.

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)

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