【Mybatis异常】nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter

简介: 【Mybatis异常】nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter

一、背景描述

项目说明:Spring boot (v2.0.0 RELEASE) + Mybatis-Plus (v3.1.1 RELEASE)

最近在项目中调试接口的时候遇到这样一个问题,mybatis解析时出错:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'deptId' in 'class java.lang.String',如下图所示:

二、错误原因

正常情况下,我们都知道mybatis在进行参数判断的时候,直接可以用 就可以了,使用entity实体或者Map的时候,下面的代码是正确的:

下面是重点哦:

但是单个参数和多参数的判断有个不同点,当我们的入参为entity实体,或者map的时候,使用 if 参数判断没任何问题。但是当我们的入参为java.lang.Integer 或者 java.lang.String的时候,这时候就需要注意一些事情了。

我们需要使用 if 参数判断 引入参数,会抛异常nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'deptId' in 'class java.lang.String'

<select id="queryList" resultType="com.soft.back.model.OutdevFactory"
      parameterType="java.lang.String">
  select
      <include refid="Base_Column_List"/>
  from op_outdev_factory
  <where>
      <if test="factoryName != null">
    and name like concat('%',#{factoryName},'%')
      </if>
  </where>
</select>

至于原因嘛就是对于这类单个入参然后用 if 判断的,mybatis有自己的内置对象,Mybatis默认采用OGNL解析参数,所以会自动采用对象树的形式取 string.xxx 值,如果没在在方法中定义,则会抛异常报错。

三、解决方案

方案一:把 #{xxx} 修改为 #{_parameter}

<select id="queryList" resultType="com.soft.back.model.OutdevFactory"
      parameterType="java.lang.String">
  select
      <include refid="Base_Column_List"/>
  from op_outdev_factory
  <where>
      <if test="_parameter != null">
    and name like concat('%',#{_parameter},'%')
      </if>
  </where>
</select>

方案二:在方法中提前定义

/**
 * 查询厂家列表
 * @param factoryName
 * @return
 */
List<OutdevFactory> getFactoryList(@Param("factoryName") String factoryName);
<select id="queryList" resultType="com.soft.back.model.OutdevFactory"
      parameterType="java.lang.String">
  select
      <include refid="Base_Column_List"/>
  from op_outdev_factory
  <where>
      <if test="factoryName!= null">
    and name like concat('%',#{factoryName},'%')
      </if>
  </where>
</select>

 

注意事项:其他mybatis的版本不知道有没有这个问题,暂时不知道。

 

 

 

完结!


相关文章
|
12天前
|
SQL Java 数据库连接
Mybatis的Cursor如何避免OOM异常
在 Mybatis 中,`Cursor` 是一个特殊对象,用于避免大量数据查询时导致的 OOM 错误。它通过懒加载和迭代器实现内存友好型数据处理,尤其适用于大规模数据查询。使用时只需将 Mapper 文件中的方法返回值设为 `Cursor&lt;T&gt;`。其原理在于操作原生 `Statement` 并按需获取数据,而非一次性加载所有数据,从而避免内存溢出。
|
2月前
|
Prometheus Cloud Native 关系型数据库
实时计算 Flink版操作报错合集之实时计算 Flink版操作报错合集之当从保存点恢复并添加新的表时,出现了org.apache.flink.util.FlinkRuntimeException异常,该怎么办
在使用实时计算Flink版过程中,可能会遇到各种错误,了解这些错误的原因及解决方法对于高效排错至关重要。针对具体问题,查看Flink的日志是关键,它们通常会提供更详细的错误信息和堆栈跟踪,有助于定位问题。此外,Flink社区文档和官方论坛也是寻求帮助的好去处。以下是一些常见的操作报错及其可能的原因与解决策略。
|
2月前
|
Java 数据库连接 mybatis
Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid
Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid
|
3月前
|
Java
Exception in thread “main“ org.apache.ibatis.exceptions.PersistenceException:
Exception in thread “main“ org.apache.ibatis.exceptions.PersistenceException:
|
3月前
|
IDE Java 应用服务中间件
解决org.apache.jasper.JasperException异常
解决org.apache.jasper.JasperException异常
|
3月前
|
Java 数据库连接 mybatis
【已解决】nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘qcBizname‘ not found
【已解决】nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘qcBizname‘ not found
96 0
|
4月前
|
前端开发 Java 数据库连接
若依 mybatis报错nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘XXX‘ 错误
若依 mybatis报错nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘XXX‘ 错误
95 0
|
4月前
|
Java 关系型数据库 MySQL
整合SpringBoot与MyBatis时报错时区异常
整合SpringBoot与MyBatis时报错时区异常
41 0
|
4月前
|
Java Spring
上传文件出现 aximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.
上传文件出现 aximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.
39 0
|
23天前
|
存储 消息中间件 Java
Apache Flink 实践问题之原生TM UI日志问题如何解决
Apache Flink 实践问题之原生TM UI日志问题如何解决
31 1