解决:nested exception is org.apache.ibatis.reflection.ReflectionException

解决方法

  • 先将问题翻译
  • 分析Mybatis
  • 其他解决办法

nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘name’ in ‘class java.lang.String’

先将问题翻译

嵌套异常是 org.apache.ibatis.reflection.ReflectionException:“class java.lang.String”中名为“name”的属性没有 getter 方法

面对这个问题的时候我们可能不知道入手,但是我们细细分析来看,我们肯定在编写代码的时候会将实体类用@Data或者getter setter来修饰,所以问题没有出现在getter方法上.

分析Mybatis

MyBatis在进行参数判断的时候,直接可以用<if></if> 就可以了,如下:

<update id="update" parametertype="java.lang.string">
    update test
    <set>
        <if test="name != null">
            id = #{Id,jdbcType=TINYINT},
        </if>
        <if test="langId != null">
            lang_id = #{langId,jdbcType=INTEGER},
        </if>
    </set>
    where id = #{Id,jdbcType=INTEGER}
</update>

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

\color{#FF0000}{错误实例}

<select id="LangId" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select
        trnsct_id
        from  t_trnsct_way_l where
        <if test="Id != null" >
            and id = #{Id}
        </if>
</select>

上述代码存在一些问题,首先入参是java.lang.Integer, 而不是map或者实体的入参方式,对于这类单个入参然后用if判断的,mybatis有自己的内置对象,那么本来Mybatis有着自己的getter setter方法,这里又指定了传入类型,所以在指定类型里面获取不到gettet方法也就可以理解了。

\color{#FF0000}{正确实例}

<select id="LangId" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select
        trnsct_id
        from  t_trnsct_way_l where
        <if test="parameter != null" >
            and id = #{Id,jdbcType=INTEGER}
        </if>
</select>

其他解决办法

1、在mapper接口上面添加@Param用来给传入参数命名,那么参数就被转化为Mybatis内置对象

相关文章

暂无评论

暂无评论...