如何在IBatis 2中为带有Parameterclass Map的SQL更新或SQL选择语句创建一个SQL In条款[英] How to create an SQL In Clause in IBatis 2 for an SQL Update or SQL Select Statement with Parameterclass Map

本文是小编为大家收集整理的关于如何在IBatis 2中为带有Parameterclass Map的SQL更新或SQL选择语句创建一个SQL In条款的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我使用IBATIS框架有一些问题.我使用IBATIS 2,现在尝试执行SQL Select和SQL Update语句,该语句可与Parameterclass" Java.util.map"和Ibatis Iterator标签一起使用.但是现在看来,这两个组成部分无法一起使用.

如果我仅在SQL Select语句中使用Ibatis Iterator标签的Java列表,则可以正常工作.

如果我仅在sql UpdateTatement中使用IBATIS ITERATOR标签的Java列表,则它不起作用.

有必要使用Java映射作为parameterclass,因为我需要SQL Statments中的多个参数.

现在,我创建了以下arraylist" filterdata",将包含在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选择语句:

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

这是我的ibatis sql select语句:

<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 UPDATE语句:

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

这是我的IBATIS SQL 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>

我收到此错误消息:

 Check the parameter map.  
 Cause: com.ibatis.sqlmap.client.SqlMapException: ParameterObject or property was not a Collection, Array or Iterator.

我该如何解决这个问题?

非常感谢!

推荐答案

看着Ibatis XML,看起来您的FilterData标签不正确.应该是:

<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://itbaoku.cn/post/2125677.html

问题描述

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 !

推荐答案

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>