Java 泛型在 MybatisPlus LambdaQueryWrapper 的使用时如何理解?

新手上路,请多包涵

MybatisPlus的使用?能否在父类使用子类泛型中的方法引用?

场景是这样的,我不知道该如何描述问题:

我有一部分持久化实体继承了父类,有一些公用属性。
我想在业务类基类里提供一个公用查询方法,入参使用公用属性。
所以在业务类基类里写了以下代码:

public List<T> getListByPmId(String pmId) {
    LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(T::getPmId, pmId);
    return this.getMapper().selectList(queryWrapper);
}

实际运行时,MybatisPlus抛出了以下异常

com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: can not find lambda cache for this entity [com.xxx.common.base.entity.BaseEntity]

换成这种写法也是一样的

public List<T> getListByPmId(String pmId) {
    return this.getMapper().selectList(Wrappers.<T>lambdaQuery()
            .eq(T::getPmId, pmId));
}

用字符串参数的QueryWrapper肯定是正常的,我已经这样写了。
但是LambdaQueryWrapper这里应该如何理解?
感觉也不是单纯的类型擦除导致的,如果用getClass().getTypeParameters()[0]取到子类泛型后应该如何拿到对应方法的Function呢?
能否避免反射实现呢?

可能和这个类似:
https://segmentfault.com/q/1010000005680647

阅读 1.2k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进