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

来源:互联网 发布:php strcamp == 编辑:程序博客网 时间:2024/05/16 18:03

一:发现问题

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或者对象中作为参数

原创粉丝点击