mybatis there is no getter named for"Integer"

JAVA就業套餐課:https://edu.csdn.net/combo/detail/1230

一:發現問題java

sql動態語句中若是 parameterType="int"sql

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

是正確的,可是若是加上if testide

    <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>spa


則報錯:.net

There is no getter for property named 'cmpid' in 'class java.lang.Integer'orm


出錯緣由:Mybatis默認採用ONGL解析參數,因此會自動採用對象樹的形式取Integer.cmpid。Integer對象沒有cmpid屬性。若是不解析參數,mybatis自動識別傳入的參數,不會報錯。對象


二:解決辦法
1.修改select語句blog

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

相關文章
相關標籤/搜索