Search in sources :

Example 11 with TableField

use of com.baomidou.mybatisplus.annotation.TableField in project kms 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)

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