使用LambdaQuery()有感-通过函数式接口获取字段名称
1.背景
在平时使用mybatis-plus
的时候,通常会使用下面两种方式
lambdaQuery()
方式:
List<Person> persons = lambdaQuery()
.eq(Person::getName,"zhangsan")
.eq(Person::getAge,11)
.list();
- 普通
QueryWrapper
的方式
List<Person> persons = baseMapper.selectList(new QueryWrapper<Person>()
.eq("name","zhangsan")
.eq("age",11));
这两种方式对比我可能更喜欢使用lambdaQuery()
那种方式,因为如果实体类里面的字段有更改的话,不需要单独取维护分布在各个地方的字符串,只需要字段本身就可以。那么就想到去定义一个工具类,让这种函数式接口方式取代很多直接定义字符串名称的场景。
2.工具类定义
定义一个实现Serializable
的函数式接口(不实现Serializable
的话,后面getDeclaredMethod("writeReplace")
会获取不到)
import net.oschina.j2cache.util.Serializer;
import java.io.Serializable;
import java.util.function.Function;
/**
* 序列化的function函数式接口
*
*/
@FunctionalInterface
public interface SerializedFunction<T, R> extends Function<T, R>, Serializable {
}
定义LambdaUtil
import java.beans.Introspector;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Locale;
/**
* lambda工具类
*
*/
public class LambdaUtil {
/**
* 传入lambda表达式获取其字段名称
* <pre>
* 例如传入Person::getName 返回:name
* 传入Person::getAge 返回: age
* </pre>
*
* @param sFunction
* @param <T>
* @return
*/
public static <T> String getFiledName(SerializedFunction<T, ?> sFunction) {
try {
Method method = sFunction.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(Boolean.TRUE);
//调用writeReplace()方法,返回一个SerializedLambda对象
SerializedLambda serializedLambda = (SerializedLambda) method.invoke(sFunction);
//得到lambda表达式中调用的方法名,如 "User::getSex",则得到的是"getSex"
String getterMethod = serializedLambda.getImplMethodName();
//去掉”get"前缀,最终得到字段名“sex"
return Introspector.decapitalize(methodToProperty(getterMethod));
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new IllegalArgumentException("Cannot get filedName by function", e);
}
}
/**
* 通过readMethod名称获取字段名称
*
* @param fieldName
* @return
*/
public static String methodToProperty(String fieldName) {
if (fieldName.startsWith("is")) {
fieldName = fieldName.substring(2);
} else if (fieldName.startsWith("get") || fieldName.startsWith("set")) {
fieldName = fieldName.substring(3);
} else {
throw new IllegalArgumentException("Error parsing property name '" + fieldName + "'. Didn't start with 'is', 'get' or 'set'.");
}
if (fieldName.length() == 1 || (fieldName.length() > 1 && !Character.isUpperCase(fieldName.charAt(1)))) {
fieldName = fieldName.substring(0, 1).toLowerCase(Locale.ENGLISH) + fieldName.substring(1);
}
return fieldName;
}
}
3.测试
import lombok.Data;
/**
* 用户类
*/
@Data
public class Person {
private String name;
private Integer age;
private String alias;
}
定义main方法运行
public static void main(String[] args) {
System.out.println(LambdaUtil.getFiledName(Person::getName));
System.out.println(LambdaUtil.getFiledName(Person::getAge));
System.out.println(LambdaUtil.getFiledName(Person::getAlias));
}
运行结果如下