如何在IBatis 2中为带有参数类映射的SQL Update或SQL Select语句创建SQL In子句
使用IBatis框架存在一些问题.我使用IBatis 2,现在尝试执行SQL select和SQL更新语句,该语句可与parameterClass"java.util.Map"和IBatis迭代器标记一起使用.但是现在看来,这两个组件无法协同工作.
I have some problemd by using the IBatis framework. I use IBatis 2 and now I tried to execute an SQL select and SQL update statement, which works with the parameterClass "java.util.Map" and an IBatis iterator tag. But now it seems, that these two component doesn´t work together.
如果我仅在SQL select语句中使用带有IBatis Iterator标记的Java列表,则可以正常工作.
If I use only a Java List with the IBatis Iterator tag inside a SQL select statement, it works fine.
如果我只在SQL updatestatement中使用带有IBatis Iterator标记的Java列表,则它不起作用.
If I use only a Java List with the IBatis Iterator tag inside a SQL updatestatement, it doesn´t works.
有必要使用Java Map作为parameterClass,因为我在SQL语句中需要多个参数.
It is necessary to use a Java Map as parameterClass, since I need more than one parameter inside the SQL statments.
现在,我创建了以下ArrayList"filterData",它将包含在HashMap"myMap"中:
Now I have created the following ArrayList "filterData", which will be included in the HashMap "myMap":
List<Long> filterData = new ArrayList<Long>();
filterData.add(11L);
filterData.add(22L);
filterData.add(33L);
HashMap<String, Object> myMap = new HashMap<String, Object>();
myMap.put("mySchema", "Schema1");
myMap.put("filterData", filterData);
以下Java代码将执行IBatis SQL select语句:
The following java code will be execute the IBatis SQL select statement:
Long selectedValues = (Long) getSqlMapClientTemplate().queryForObject("selectWithMap", myMap)
这是我的IBatis SQL选择语句:
Here is my IBatis SQL select statement:
<select id="selectWithMap" resultClass="long" parameterClass="java.util.Map">
SELECT COUNT(*)
FROM $mySchema$.myTable
WHERE myTable.id IN
<iterate open="(" close=")" conjunction=",">
#[filterData]#
</iterate>
</select>
以下Java代码将执行IBatis SQL更新语句:
The following java code will be execute the IBatis SQL update statement:
Integer updatedValues = (Integer) getSqlMapClientTemplate().update("updateWithMap", myMap);
这是我的IBatis SQL更新语句:
Here is my IBatis SQL update statement:
<update id="updateWithMap" parameterClass="java.util.Map">
UPDATE $mySchema$.myTable
SET myTable.name = "Something!!",
WHERE myTable.id IN
<iterate open="(" close=")" conjunction=",">
#[filterData]#
</iterate>
</update>
我收到此错误消息:
Check the parameter map.
Cause: com.ibatis.sqlmap.client.SqlMapException: ParameterObject or property was not a Collection, Array or Iterator.
我该如何解决这个问题?
How can I solve this problem?
非常感谢!
查看iBatis xml,看来您的filterData标记不正确.应该是:
Looking at the iBatis xml it looks like your filterData tag is incorrect. It should be:
<update id="updateWithMap" parameterClass="java.util.Map">
UPDATE $mySchema$.myTable
SET myTable.name = "Something!!",
WHERE myTable.id IN
<iterate open="(" close=")" conjunction=",">
#filterData[]#
</iterate>
</update>
<update id="updateWithMap" parameterClass="java.util.Map">
UPDATE $mySchema$.myTable
SET myTable.name = "Something!!",
WHERE myTable.id IN
<iterate open="(" close=")" conjunction=",">
#filterData[]#
</iterate>
</update>