首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

iBatis使用parameterMap时的Parameter index out of range异常

2012-10-08 
iBatis使用parameterMap时的Parameter index out of range错误?? ?先说下应用背景,我想实现一个小小的搜索

iBatis使用parameterMap时的Parameter index out of range错误

?? ?先说下应用背景,我想实现一个小小的搜索,从MySQL数据库里面对三个字段进行模糊查询然后使用limit得到指定的记录数。

?? ?我最初的做法是在DaoImpl里面创建一个Map,里面包含有查询需要的searchValue,start,number三个值,然后在SqlMap里面创建一个parameterMap对应前面的Map,再在<select/>里面使用这三个参数。实现主要代码如下:

调用sqlmap的java代码:

Map map = new HashMap();map.put("value", searchValue);map.put("start", start);map.put("number", number);System.out.println(map.get("value") + "," + map.get("start") + "," + map.get("number"));return (List<Works>) getSqlMapClientTemplate().queryForList("Works.getPageSearchWorks", map);

SqlMap里面与该查询相关的代码:

<parameterMap id="searchMap"><parameter property="value"/><parameter property="start"/><parameter property="number"/></parameterMap><select id="Works.getPageSearchWorks" parameterMap="searchMap" resultMap="worksResultMap">select * from works where title like '%$value$%'or tag like '%$value$%' or description like '%$value$%'limit #start#,#number#</select>

??worksResultMap已经在前面定义了,并且该SqlMap里面还有很多个其他的<select/><delete/><update/>,都可以正常执行。

?? ?然后写了个jUnit方法进行测试,失败,错误提示如下:

org.springframework.dao.TransientDataAccessResourceException: SqlMapClient operation; SQL []; ??

--- The error occurred in WorksSqlMap.xml. ?

--- The error occurred while applying a parameter map. ?

--- Check the Works.searchMap. ?

--- Check the parameter mapping for the 'value' property. ?

--- Cause: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException: ??

--- The error occurred in WorksSqlMap.xml. ?

--- The error occurred while applying a parameter map. ?

--- Check the Works.searchMap. ?

--- Check the parameter mapping for the 'value' property. ?

--- Cause: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

?

?? ?其中那句“Parameter index out of rang”怎么都没弄明白。开始我以为是我的<select/>语句写错了,于是把那条语句分成两部分,一部分只做模糊查询,一部分只做limit操作,确认都可以正常运行,没有问题。

?? ?按照前面这句错误提示,应该是自己写的parameterMap没法被SqlMap识别了,但我怎么都没有想清楚原因。。。后来在某人的博客上发现了同样的问题,他的解决方法是把parameterMap改成parameterClass,里面也没有写什么原因。本来我也以为这样应该是没什么用的,但后来在某位网友的影响下,我抱着试试看的心态把代码修改了一下,结果真的把这个问题解决了。。。可我依然没有想清楚是什么原因。。。。把官方文档拿出来又找着看了下,也没发现parameterMap跟parameterClass有什么大的差别。。。。

修改之后的SqlMap代码:

<select id="Works.getPageSearchWorks" parameterresultMap="worksResultMap">select * from works where title like '%$value$%'or tag like '%$value$%' or description like '%$value$%'limit #start#,#number#</select>
?期待有高手能够帮我解释下这其中到底是什么原因。。。。

?

?

?

?

1 楼 love1990xie 2011-09-23   请问,你这个原因现在了解了吗。我今天也遇到这个问题,找了一天啊,都没有解决。东西之前都是好好的,今天突然再跑就出错了。。。啊啊。筒子你一定要回我啊。 2 楼 love1990xie 2011-09-23   另外想我传入的参数都不是一个类里面的啊,难道要为了这个每个map参数都去写一个类啊 3 楼 love1990xie 2011-09-23   博主。这个出错估计是因为模糊查询那里的各种符号有关,我改成这种形式就好了like concat('%',#shopName#,'%') 
传参方式不需要改变。
另外,友情提醒,直接like会有注入问题的。

热点排行