How to create an SQL In Clause in IBatis 2 for an SQL Update or SQL Select Statement with Parameterclass Map

你。 提交于 2019-12-24 11:20:17

问题


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.

If I use only a Java List with the IBatis Iterator tag inside a SQL select statement, it works fine.

If I use only a Java List with the IBatis Iterator tag inside a SQL updatestatement, it doesn´t works.

It is necessary to use a Java Map as parameterClass, since I need more than one parameter inside the SQL statments.

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);

The following java code will be execute the IBatis SQL select statement:

Long selectedValues = (Long) getSqlMapClientTemplate().queryForObject("selectWithMap", myMap)

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>

The following java code will be execute the IBatis SQL update statement:

Integer updatedValues = (Integer) getSqlMapClientTemplate().update("updateWithMap", myMap);

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>

I get this error message:

 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?

Many thanks !


回答1:


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>



来源:https://stackoverflow.com/questions/28343006/how-to-create-an-sql-in-clause-in-ibatis-2-for-an-sql-update-or-sql-select-state

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!