Mybatis中传参包There is no getter for property named 'XXX' in 'class java.lang.String'

一、发现问题

<select id="queryStudentByNum" resultType="student" parameterType="string"> select num,name,phone from student <where> <if test = " num!=null and num!='' "> AND num = #{num} </if> </where> </select> Mybatis查询传入一个字符串传参数,报There is no getter for property named 'num' in 'class java.lang.String'。

二、解决问题

<select id="queryStudentByNum" resultType="student" parameterType="string"> select num,name,phone from student <where> <if test = " _parameter!=null and_parameter!='' "> AND num = #{_parameter} </if> </where> </select> 无论参数名,都要改成"_parameter"。

三、原因分析

Mybatis默认采用ONGL解析参数,所以会自动采用对象树的形式取string.num值,引起报错。也可以public List methodName(@Param(value="num") String num)的方法说明参数值

Mybatis版本:mybatis-3.0.6.jar

1.当入参为 string类型时 (包括java.lang.String.) 

我们使用#{xxx}引入参数.会抛异常There is no getter for property named 'XXX' in 'class java.lang.String'

<select id="getBookingCount" resultType="int" parameterType="string">

select count(*) from TB_EMPC_BOOKING_ORDER T

where (t.state = '1' or t.state = '2')

and t.appointmenttime = #{state}

</select>

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

<select id="getBookingCount" resultType="int" parameterType="string">

select count(*) from TB_EMPC_BOOKING_ORDER T

where (t.state = '1' or t.state = '2')

and t.appointmenttime = #{_parameter}

</select>

3.解决方法二:可以在方法中提前定义:

public int  methodName(@Param(value="state") String state ){

  ...

}

4.原因:Mybatis默认采用ONGL解析参数,所以会自动采用对象树的形式取 string.xxx 值,如果没在在方法中定义,则会抛异常报错。

5.其他mybatis的版本不知道有没有这个问题,暂时没试过.

猜你喜欢

转载自blog.csdn.net/weixin_42476601/article/details/81900899