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

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内置对象

  • 25
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

敏姐儿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值