How to create an SQL In Clause in IBatis 2 for an

2019-07-28 01:34发布

问题:

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>



标签: java sql ibatis