LambdaQueryWrapper或其他Lambda相关的类中,有很多SFunction类,
public final LambdaQueryWrapper<T> select(SFunction<T, ?>... columns) {
if (ArrayUtils.isNotEmpty(columns)) {
this.sqlSelect.setStringValue(columnsToString(false, columns));
}
return typedThis;
}
这个类我们一般使用方法引用 User:: 或者其他数据库实体类,后续跟方法,再跟参数
lambdaQuery().eq(FinancialLoanRecord::getOrgId,1).list();
@Data
@Entity
@Table(name = "financial_loan_record")
@TableName("financial_loan_record")
public class FinancialLoanRecord extends BaseEntity {
private static final long serialVersionUID = -2943947768036673473L;
@ApiModelProperty(value = "机构id")
@Column(columnDefinition = "varchar(24) comment '机构id'")
private String orgId;
@ApiModelProperty(value = "基地id")
@Column(columnDefinition = "varchar(24) comment '基地id'")
private String baseId;
}
那么,为什么明明传值getOrgId的方法引用,实际传的类是SFunction 会识别到是哪个数据库字段呢?
首先,我们要了解一个概念,就是lambda的方法引用在序列化的时候,会序列化为SerializedLambda类,万物皆对象。
该类有类和方法名称信息
public final class SerializedLambda implements Serializable {
private static final long serialVersionUID = 8025925345765570181L;
private final Class<?> capturingClass;
private final String functionalInterfaceClass;
private final String functionalInterfaceMethodName;
private final String functionalInterfaceMethodSignature;
private final String implClass;
private final String implMethodName;
private final String implMethodSignature;
private final int implMethodKind;
private final String instantiatedMethodType;
private final Object[] capturedArgs;
}
于是,我们就能得知 虽然传值的是SFunction,但是实际上的类是SerializedLambda,这也就是为什么SFunction会有序列化接口的原因。
@FunctionalInterface
public interface SFunction<T, R> extends Function<T, R>, Serializable {
}
能找到哪个方法,哪个类,哪就肯定能找到对应的数据库字段名称。