I know how to use this function as a black box, aka, what goes in, what comes out, but I cannot figure out how the heck this function actually works.
In short, .eq()
takes in 3 parameters, a condition, a functional interface, and a value. If the condition is true
, the function generates a SQL string similiar to where column_name = value
. However, I am trying to figure out how it infers the column name from the functional interface.
This is where the function is used,
public class MyImpl extends ServiceImpl<MyEntityDao, MyEntity> implements MyService {
// function in question
public MyEntity select(MyEntity condition) {
LambdaQueryWrapper<MyEntity> wrapper = Wrappers.lambdaQuery();
wrapper.eq(Objects.nonNull(condition.getId()), MyEntity::getId, condition.getId());
...
}
}
where
public interface MySerivce extends IService<MyEntity> { }
First of all, getId()
is a simple getter/setter of the form public int getId() { return this.id; }
. How would a static method MyEntity::getId
be allowed when I have only defined getters/setters for instances. In fact, a simple check Arrays.asList(1, 2, 3).forEach(MyEntity::getId);
gives the compile error Non-static method cannot be referenced from a static context
. Apparently, JAVA complains that I am using a non-existant static class method. So, why is there no warning when I used it on the provided .eq()
method?
Similarly, when digging throught the source code, MyBatis tries to parse the functional interface as a string,
protected String columnToString(SFunction<T, ?> column) {...}
this is where I am starting to suspect that MyBatis was generating some methods without my knowledge. It somehow transforms the supplier myinstance.getId()
into a function Y <- MyEntity.getId(X)
. But again, I cannot find a way to test my hypothesis, or I could be completely wrong from the get go.
Anyway, looking for some insights. Thanks beforehand.