Parameter '0' not found.Available parameters are [arg1, arg0, param1, param2]的解決方法


在ssm框架中,mybatis+spring操作數據庫報錯:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'category' not found. Available parameters are [arg1, arg0, param1, param2]

其實就是方法參數名,到xml文件識別不了的問題

出錯的代碼:
mapper

//通過書的種類或者書名模糊查詢,查找相應的圖書總數
  public int queryProductsCount(String name, String category);

mapper.xml

<!--通過書的種類或者書名模糊查詢,查找相應的圖書總數-->
  <select id="queryProductsCount" resultType="int">
      select count(1) as count from itcaststore.products
      <where>
          <if test="category != null">
              category like concat('%',#{category},'%')
          </if>
          <if test="name != null">
              and name like concat('%',#{name},'%')
          </if>
      </where>
  </select>

錯誤原因
mybatis的Mapper接口方法傳入了多個參數

還要注意:parameterType是要去掉的,雖然這里的參數全部都是String類型,如果涉及多個類型那就必須去掉

  • 解決方法一:
    使用#{arg0}和#{arg1}來告訴mybatis,當前變量使用哪個參數的值
 <select id="queryProductsCount" resultType="int">
        select count(1) as count from itcaststore.products
        <where>
            <if test="category != null">
                category like concat('%',#{arg0},'%')
            </if>
            <if test="name != null">
                and name like concat('%',#{arg1},'%')
            </if>
        </where>
    </select>

但是因為這里有:<if test="category != null">和<if test="name != null">,所以此法對於這種特殊情況無解。

  • 解決方法二:(不修改mapper.xml)
    使用注解@Param
public int queryProductsCount(@Param("name") String name, @Param("category")String category);
  • 解決方法三:
    HashMap類型作為傳入類型(不修改mapper.xml)
public int queryProductsCount1(Map<String,String> map);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM