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

一:发现问题

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);  

原文:http://blog.csdn.net/cclovett/article/details/12855505

原文地址:https://www.cnblogs.com/zhuhc/p/4250493.html