mybatisplus Lambda函数转属性名

import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda;
import lombok.Data;
import org.apache.ibatis.reflection.property.PropertyNamer;

import java.io.IOException;

public class JSONTest{
    public static <T> String getFieldName(SFunction<T, Object> function) {
        SerializedLambda resolve = LambdaUtils.resolve(function);
        String field = PropertyNamer.methodToProperty(resolve.getImplMethodName());
        return  field;
    }
    public static void main(String[] args) throws IOException {
        String fieldName = getFieldName(User::getName);
        System.out.println(fieldName);
    }
}
@Data
class User{
    private String name;
}

你可能感兴趣的:(JavaEE,java)