Mybatis单个参数的if判断(针对异常:There is no getter for property..)

我们都知道mybatis在进行参数判断的时候,直接可以用<if test=""></if> 就可以了,如下:

1、常规代码

<update id="update" parameterType="com.cq2022.zago.order.entity.Test" >
    update t_test_l
    <set >
      <if test="trnsctWayId != null" >
        trnsct_way_id = #{trnsctWayId,jdbcType=TINYINT},
      </if>
      <if test="langId != null" >
        lang_id = #{langId,jdbcType=INTEGER},
      </if>
    </set>
    where trnsct_way_l_id = #{trnsctWayLId,jdbcType=INTEGER}
  </update>

但是单个参数和多参数的判断有个不同点,当我们的入参为entity实体,或者map的时候,使用if 参数判断没任何问题。

但是当我们的入参为java.lang.Integer  或者 java.lang.String的时候,这时候就需要注意一些事情了

具体代码如下(咱们看着代码说,先展示错误代码):

2、错误代码

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

上述代码存在一些问题,首先入参是java.lang.Integer, 而不是map或者实体的入参方式,对于这类单个入参然后用if判断的,mybatis有自己的内置对象,

如果你在if判断里面 写的是你的入参的对象名,那就报异常:Internal error : nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'langId' in 'class java.lang.Integer'


3、正确代码:


这里就涉及到mybatis的内置对象_parameter,单个参数判断的时候,就不像1、 2那样直接用参数对象名判断了。还有就是数据类型最好加上




  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值