3、Mybatis-Plus异常之There is no getter for property named ‘ew‘ in ‘class解决方案

项目使用技术:

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)

引用\[1\]:出现的错误信息是关于MyBatis异常,具体是MyBatisSystemException和ReflectionException。错误信息中提到了一个属性名为'ew'的属性,在com.baomidou.mybatisplus.core.conditions.query.QueryWrapper类中找不到对应的getter方法。这个错误通常发生在使用MyBatis-Plus时,可能是因为在查询条件中使用了错误的属性名或者属性名拼写错误。解决这个问题的方法是检查代码中的查询条件,确保属性名的正确性。 问题:使用mybatis-plus时遇到there is no getter for property named 'ew'的错误,该如何解决? 回答: 出现这个错误通常是因为在使用MyBatis-Plus时,查询条件中使用了错误的属性名或者属性名拼写错误。解决这个问题的方法是检查代码中的查询条件,确保属性名的正确性。请检查您的代码中是否正确使用了属性名'ew',并且确保该属性在com.baomidou.mybatisplus.core.conditions.query.QueryWrapper类中存在对应的getter方法。如果属性名拼写正确且存在对应的getter方法,那么可能是其他原因导致的错误,请进一步检查您的代码和配置是否正确。 #### 引用[.reference_title] - *1* [Mybatis-plus查询问题 There is no getter for property namedew’ in ‘class:query.QueryWrapper](https://blog.csdn.net/rows_com/article/details/120163341)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白de成长之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值