3. Mybatis-Plus exception: There is no getter for property named ‘ew‘ in ‘class solution

Project technology used:

SpringBoot2.7.5+MyBatis-plus5.1.2

Project usage scenarios:

The Dao layer mapper inherits the baseMapper under com.baomidou.mybatisplus.core.mapper.BaseMapper</>,
Call the method inherited by the mapper in the Dao layer in the service layer selectOne(), an exception occurred.


Problem Description

在如下代码,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));
    }
    
}

Cause Analysis:

found that in the implementation of the selectOne method in BaseMapper (the code is as follows), the first line is to call the selectList method, because userMapper 重写了selectList方法 ,
and the parameters < /span>. and throws exception没有加@Param(Constants.WRAPPER) , so it causesew找不到 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

The implementation of the selectOne method in BaseMapper is as follows:

	/**
     * 根据 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;
    }

solution:

public interface UserMapper extends BaseMapper<User> {
    
    

    @Override
    @Cacheable(cacheManager = "userCacheManager", value = "queryUsers")
    List<UseProps> selectList(@Param(Constants.WRAPPER) Wrapper<User> queryWrapper);
}

Rewrite the selectList方法 parameter with annotation @Param(Constants.WRAPPER) in front of it.

Guess you like

Origin blog.csdn.net/Java__EE/article/details/128534206