
正文
Mybatis基本类型参数非空判断(异常:There is no getter for property...)
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
先看一小段代码
<select id="queryByPhone" parameterType="java.lang.String" resultType="com.ccnc.bean.user.QuickUser">
select
*
from quick_user where
<if test="phone != null" >
and phone = #{phone}
</if>
</select>
粗看并没有什么问题,执行会报There is no getter for property phone ......
原因是mybatis在对<if>解析时,会去parameterType指定的对象里获取相应属性,如果指定的对象是复杂对象,运行正常。这里是基本类型String,就会去String类找phone的get和set方法,所以报错。
正确代码,去掉parameterType,同时在对应的dao加入@Praram注解
<select id="queryByPhone" resultType="com.ccnc.bean.user.QuickUser">
select
*
from quick_user where
<if test="phone != null" >
and phone = #{phone}
</if>
</select>
public interface quickUserDao{QuickUser queryByPhone(@Praram("phone") String phone);
}







