Java Code Examples for com.baomidou.mybatisplus.annotation.TableField

The following examples show how to use com.baomidou.mybatisplus.annotation.TableField. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: summerframework   Source File: MyBatisUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static <T> String getFieldName(EntityGetterMethod<T, Object> expression) {
    if (expression == null)
        throw new IllegalArgumentException("Expression should not be null");
    try {
        Method method = expression.getClass().getDeclaredMethod("writeReplace");
        method.setAccessible(Boolean.TRUE);
        SerializedLambda serializedLambda = (SerializedLambda)method.invoke(expression);
        String fieldName = StringUtils.resolveFieldName(serializedLambda.getImplMethodName());
        String className = serializedLambda.getImplClass().replace("/", ".");
        Field field = ReflectionUtils.findField(Class.forName(className), fieldName);
        String columnName = field.getName();
        TableField[] tableField = field.getAnnotationsByType(TableField.class);
        if (null != tableField && tableField.length == 1) {
            if (!StringUtils.isEmpty(tableField[0].value())) {
                columnName = tableField[0].value();
            }
        }
        String ret = StringUtils.camelToUnderline(columnName);
        return ret;
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException("This will never happen!", e);
    }
}
 
Example 2
Source Project: jstarcraft-core   Source File: MyBatisMetadata.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 构造方法
 * 
 * @param metadata
 */
MyBatisMetadata(Class<? extends BaseMapper<?>> clazz) {
    ormClass = Class.class.cast(ParameterizedType.class.cast(clazz.getGenericInterfaces()[0]).getActualTypeArguments()[0]);
    ormName = ormClass.getName();
    ReflectionUtility.doWithFields(ormClass, (field) -> {
        if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers())) {
            return;
        }
        TableField annotation = field.getAnnotation(TableField.class);
        if (annotation != null) {
            if (!annotation.exist()) {
                return;
            }
            String columnName = annotation.value();
            if (!StringUtility.isEmpty(columnName)) {
                columnNames.put(field.getName(), columnName);
            }
        }
        if (field.isAnnotationPresent(Version.class)) {
            versionName = field.getName();
            return;
        }
        Class<?> type = ClassUtility.primitiveToWrapper(field.getType());
        if (String.class == type) {
            fields.put(field.getName(), type);
        } else if (type.isEnum()) {
            fields.put(field.getName(), type);
        } else if (Collection.class.isAssignableFrom(type) || type.isArray()) {
            fields.put(field.getName(), List.class);
        } else if (Date.class.isAssignableFrom(type)) {
            fields.put(field.getName(), Date.class);
        } else {
            fields.put(field.getName(), Map.class);
        }
        if (field.isAnnotationPresent(TableId.class)) {
            primaryName = field.getName();
            primaryClass = type;
        }
    });
    mapperClass = clazz;
}