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