mybatis there is no getter named for"Integer"

JAVA就业套餐课:https://edu.csdn.net/combo/detail/1230

一:发现问题

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或者对象中作为参数
---------------------
作者:cclovett
来源:CSDN
原文:https://blog.csdn.net/cclovett/article/details/12855505
版权声明:本文为博主原创文章,转载请附上博文链接!


上一篇:个人总结---Where there's a will there's a way!


下一篇:1035 Password (20 分)