Mybatis异常There is no getter for property named 'XXX' in class

在集成Mybatis的项目中定义接口并实现映射SQL时,遇到过一个很奇怪的问题,如下:

定义接口:

1
UserCouponBo getUserCouponInfoById(Long userCouponId);

然后对应的sql的xml如下:

1
2
3
4
5
6
7
8
<select id="selectUserCouponInfoById" parameterType="java.lang.Long" resultMap="baseResultMap">
select amount,receive_time from user_coupon
<where>
<if test="userCouponId != null">
id = #{userCouponId,jdbcType=BIGINT}
</if>
</where>
</select>

上面的接口就是通过id查询指定的优惠券信息,个人感觉该sql写的不够优雅。就是这个接口,报错:There is no gettery for property…!!!
这是为什么呢?
原因是在if里面用了Mybatis的内置对象,<if test="id != null">,Mybatis默认采用OGNL解析参数,所以会自动采用对象树的形式取long.xxx值,如果没在方法中定义,则会抛出异常。

解决方案一:把#{xxx}修改为#{_parameter}即可

1
2
3
4
5
6
7
8
<select id="selectUserCouponInfoById" parameterType="java.lang.Long" resultMap="baseResultMap">
select amount,receive_time from user_coupon
<where>
<if test="_parameter != null">
id = #{_parameter}
</if>
</where>
</select>

解决解决方案二:在方法中提前定义。

1
UserCouponBo getUserCouponInfoById(@Param("userCouponId") Long userCouponId);

给userCouponId加@Param注解,定义一下就可以了。

本文标题:Mybatis异常There is no getter for property named 'XXX' in class

文章作者:王洪博

发布时间:2019年03月14日 - 16:03

最后更新:2019年09月12日 - 02:09

原始链接:http://whb1990.github.io/posts/53121faf.html

▄︻┻═┳一如果你喜欢这篇文章,请点击下方"打赏"按钮请我喝杯 ☕
0%