您的位置:首页 > 其它

关于mybatis中基本类型条件判断问题

2013-10-18 16:26 561 查看
一:发现问题

sql动态语句中如果 parameterType="int"

<select id="sel_campusinfo" parameterType="int" resultType="Campusinfo">
select cmpid,cmpname from campusinfo where state!='d' and cmpid=#{cmpid}
</select>

是正确的,但是如果加上if test

<select id="sel_campusinfo" parameterType="int" resultType="Campusinfo">
select cmpid,cmpname from campusinfo where state!='d' and cmpid!=0
<if test="cmpid!=0">and cmpid=#{cmpid}</if>
</select>


则报错:
There is no getter for property named 'cmpid' in 'class java.lang.Integer'


出错原因:Mybatis默认采用ONGL解析参数,所以会自动采用对象树的形式取Integer.cmpid。Integer对象没有cmpid属性。如果不解析参数,mybatis自动识别传入的参数,不会报错。

二:解决办法

1.修改select语句

<select id="sel_campusinfo" parameterType="int" resultType="Campusinfo">
select cmpid,cmpname from campusinfo where state!='d' and cmpid!=0
<if test="_parameter!=0">and cmpid=#{_parameter}</if>
</select>


参数名全部改为_parameter。

2.不修改sql,只修改接口

接口类:

Campusinfo sel_campusinfo( int cmpid);


改为:

Campusinfo sel_campusinfo(@Param(value="cmpid") int cmpid);

3.可以将参数包装在hashmap或者对象中作为参数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: