Mybatis单个参数的if判断报异常There is no getter for property named \’xxx\’ in \’class java.lang.Integer\’的解决方案

2025-05-29 0 84

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

1、常规代码

?

1

2

3

4

5

6

7

8

9

10

11

12
<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、错误代码

?

1

2

3

4

5

6

7

8
<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单个参数的if判断报异常There is no getter for property named \'xxx\' in \'class java.lang.Integer\'的解决方案

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/moneyshi/article/details/45914467

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Mybatis单个参数的if判断报异常There is no getter for property named \’xxx\’ in \’class java.lang.Integer\’的解决方案 https://www.kuaiidc.com/110813.html

相关文章

发表评论
暂无评论