关于ibatis的parameter index out of range (1 > number parameters which is 0).异常

说起来这个异常很坑爹啊,调试了一下午,数据一点都没有问题,可以走到最后一步总是出这个异常,

程序是这样的,是要分页查两关联表的一些字段,、

这是原来的代码是这样 的,

这是条件的参数配置

dao层传参

<span style="font-size:18px;">//分页查询包含故障代码的故障报告
	public List<FaultReport> listFaultReportByfaultcode(String faultcode,
			Map<String, Object> map) {
		Map<String, Object> parameterMap = new HashMap<String, Object>();
		parameterMap.put("faultcode", faultcode);
		if (map.size() > 0) {
		parameterMap.putAll(map);
		}
		return this.sqlMapClientTemplate.queryForList("listFaultReportByfaultcode", parameterMap);
	}</span>



<span style="font-size:18px;"><parameterMap class="java.util.HashMap" id="faultreport2_map">
		<parameter property="faultcode" />
                <parameter property="offset" />
                <parameter property="count" /> 
 </parameterMap></span>
这是查询语句
<span style="font-size:18px;"> <select id="listFaultReportByfaultcode"  parameterMap="faultreport2_map" resultClass="faultreport">

    select  fr.id as id ,fr.reportname as reportname , fr.importdate as importdate ,fr.savepath as savepath 
    from faultreport as fr 
    where fr.id 
    in ( SELECT ar.reportid from analysereport as ar 
     WHERE 
     faultcode =  #faultcode#
     
                order by ar.reportid ASC ) LIMIT $offset$,$count$
                
                
    </select></span>

看似没有错,但是一直报这两个错误




错误原因,我分析就是映射的faultcode没有传进去,


然后各种找资料,最后将 parameterMap,修改为 parameterClass="java.util.HashMap"。没有问题了。

在dao层传参的时候就是构造了一个map,因为这个时候有faultcode,offset,count 三个参数,虽然在faultreport2_map

中构造了这三个参数,但是与到曾传进去的参数其实分离的,dao层的 参数parameterMap,作为一个map对象传进去但是在map.xml

中并没有接收到这个值,而faultreport2_map只是一个假象,其实他并没有值,所以后面会报参数索引越界,应该是1个参数,

现在为0个。其实在ibatis,或mybatis中有记住一点很关键,要么传单个的值int ,string,(这个最简单),要么传对象(实体类的对象)

要么传一个map(其实这个比较常见,有分页,或者是对表是构造各种条件进去,不需要修改map,xml文件个实体类)。

这个错误算是解决了,对ibatis有个更深的认识。爽

这个是正确的代码,只需要修改

<span style="font-size:18px;">parameterMap="faultreport2_map"</span>

<span style="font-size:18px;">parameterClass="java.util.HashMap"</span>


<span style="font-size:18px;"> <select id="listFaultReportByfaultcode" parameterClass="java.util.HashMap" resultClass="faultreport">

    select  fr.id as id ,fr.reportname as reportname , fr.importdate as importdate ,fr.savepath as savepath 
    from faultreport as fr 
    where fr.id 
    in ( SELECT ar.reportid from analysereport as ar 
     WHERE 
     faultcode =  #faultcode#
     
                order by ar.reportid ASC ) LIMIT $offset$,$count$
  </select></span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值