Search in sources :

Example 1 with SerializedLambda

use of com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda in project JBM by numen06.

the class EntityUtils method toFieldName.

// public static <T> String setDefault(T bean, boolean criteria, SFunction<T, ?> func) {
// if (criteria) {
// BeanUtil.getProperty(bean, toFieldName(func));
// }
// 
// }
/**
 * 转换方法引用为属性名
 * @param func
 * @return
 */
public static <T> String toFieldName(SFunction<T, ?> func) {
    SerializedLambda lambda = LambdaUtils.resolve(func);
    // 获取方法名
    String methodName = lambda.getImplMethodName();
    String prefix = null;
    if (methodName.startsWith("get")) {
        prefix = "get";
    } else if (methodName.startsWith("set")) {
        prefix = "set";
    } else if (methodName.startsWith("is")) {
        prefix = "is";
    }
    if (prefix == null) {
        log.info("无效的getter方法: {}", methodName);
    }
    String field = StrUtil.subAfter(methodName, prefix, false);
    return StrUtil.lowerFirst(field);
}
Also used : SerializedLambda(com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda)

Example 2 with SerializedLambda

use of com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda in project chao-cloud by chaojunzi.

the class FunctionUtil method getFieldName.

static <T, R> String getFieldName(SFunction<T, R> func) {
    SerializedLambda lambda = LambdaUtils.resolve(func);
    String methodName = lambda.getImplMethodName();
    return StrUtil.getGeneralField(methodName);
}
Also used : SerializedLambda(com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda)

Example 3 with SerializedLambda

use of com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda in project chao-cloud by chaojunzi.

the class FunctionUtil method getColumn.

static <T, R> String getColumn(SFunction<T, R> func) {
    SerializedLambda lambda = LambdaUtils.resolve(func);
    String fieldName = StrUtil.getGeneralField(lambda.getImplMethodName());
    Class<?> beanClass = lambda.getImplClass();
    TableInfo info = buildTableInfo(beanClass);
    Assert.notNull(info, "无效的数据实体 beanClass={}", beanClass.getName());
    Map<String, String> propColumnMap = info.getFieldList().stream().collect(Collectors.toMap(TableFieldInfo::getProperty, TableFieldInfo::getColumn));
    String column = propColumnMap.get(fieldName);
    Assert.notBlank(column, "无效的列名映射 fieldName={}", fieldName);
    return column;
}
Also used : TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) SerializedLambda(com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda)

Example 4 with SerializedLambda

use of com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda 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)

Aggregations

SerializedLambda (com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda)4 TableField (com.baomidou.mybatisplus.annotation.TableField)1 TableInfo (com.baomidou.mybatisplus.core.metadata.TableInfo)1 Field (java.lang.reflect.Field)1