项目使用技术:
SpringBoot2.7.5+MyBatis-plus5.1.2
项目使用场景:
其中Dao层mapper继承com.baomidou.mybatisplus.core.mapper.BaseMapper下的baseMapper</>,
在service层中调用Dao层中mapper继承的方法 selectOne(),出现异常。
问题描述
在如下代码,UserService中调用findUser()方法时,出现异常,报错如下: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'ew' in 'class com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper' at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441) at com.sun.proxy.$Proxy103.selectList(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224) at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForMany(MybatisMapperMethod.java:166) at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:77) at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:148) at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89) at com.sun.proxy.$Proxy107.selectList(Unknown Source) at com.baomidou.mybatisplus.core.mapper.BaseMapper.selectOne(BaseMapper.java:173) at java.base/java.lang.invoke.MethodHandle.invokeWithArguments(Unknown Source) at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$DefaultMethodInvoker.invoke(MybatisMapperProxy.java:162) at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89) at com.sun.proxy.$Proxy107.selectOne(Unknown Source) Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'ew' in 'class com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper' at org.apache.ibatis.reflection.Reflector.getGetInvoker(Reflector.java:387) at org.apache.ibatis.reflection.MetaClass.getGetInvoker(MetaClass.java:164)
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.springframework.cache.annotation.Cacheable;
import java.util.List;
/**
* @author
* @since 2023-01-03
*/
public interface UserMapper extends BaseMapper<User> {
@Override
@Cacheable(cacheManager = "userCacheManager", value = "queryUsers")
List<UseProps> selectList(Wrapper<User> queryWrapper);
}
/**
* @author
* @since 2023-01-03
*/
@Service
public interface UserService{
@Resource
private UserMapper userMapper;
private User findUser(String username) {
return userMapper.selectOne(new LambdaQueryWrapper<User>()
.eq(User::getUsername, username));
}
}
原因分析:
发现BaseMapper中selectOne方法的实现(代码如下)中,第一行是调用selectList方法,由于userMapper
重写了selectList方法
,
而且参数没有加@Param(Constants.WRAPPER)
,所以导致ew找不到
,抛出异常org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'ew' in 'class com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
。
BaseMapper中selectOne方法的实现如下:
/**
* 根据 entity 条件,查询一条记录
* <p>查询一条记录,例如 qw.last("limit 1") 限制取一条记录, 注意:多条数据会报异常</p>
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
default T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
List<T> ts = this.selectList(queryWrapper);
if (CollectionUtils.isNotEmpty(ts)) {
if (ts.size() != 1) {
throw ExceptionUtils.mpe("One record is expected, but the query result is multiple records");
}
return ts.get(0);
}
return null;
}
解决方案:
public interface UserMapper extends BaseMapper<User> {
@Override
@Cacheable(cacheManager = "userCacheManager", value = "queryUsers")
List<UseProps> selectList(@Param(Constants.WRAPPER) Wrapper<User> queryWrapper);
}
重写的 selectList方法
参数前面加上注解 @Param(Constants.WRAPPER)
。