说起来这个异常很坑爹啊,调试了一下午,数据一点都没有问题,可以走到最后一步总是出这个异常,
程序是这样的,是要分页查两关联表的一些字段,、
这是原来的代码是这样 的,
这是条件的参数配置
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>