如何在IBATIS迭代中使用select语句?

时间:2014-10-30 07:35:05

标签: java mysql spring spring-mvc ibatis

MySQL查询如下

update  group_entity set deleted = 1  where entity_id in (select entity_id from entity where entity_row_id in ('1-424g','1-242T') and entity_type='Data');

此查询在mysql中运行。

我的Ibatis查询更改如下

<update id="updateData" parameterClass="abc.data.updateDataParameters">
    update group_entity set deleted = 1 where entity_id in
    <iterate open="(" close=")" conjunction=",">
        select entity_id from entity where entity_row_id in
         <iterate property="parentIds" open="(" close=")" conjunction=",">
            #parentIds[]#
        </iterate>
        and entity_type = #parentType#
    </iterate>
</update>

但Ibatis查询无效,错误 ParameterObject或属性不是Collection,Array或Iterator。 错误:

--- Cause: com.ibatis.sqlmap.client.SqlMapException: ParameterObject or property was not a Collection, Array or Iterator.; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:

请告诉我如何在<iterate>Select id from table</iterate>等迭代中使用select语句返回id列表。

我的updateDataParameters

class updateDataParameters
{
List<String> parentId;
string parentType;
// with getter and setter and receptive constructor  
}

1 个答案:

答案 0 :(得分:1)

不需要第一个迭代元素。 您的请求应该是:

<update id="updateData" parameterClass="abc.data.updateDataParameters">
    update group_entity set deleted = 1 where entity_id in (
        select entity_id from entity where entity_row_id in
        <iterate property="parentId" open="(" close=")" conjunction=",">
            #parentId[]#
        </iterate>
        ) and entity_type = #parentType#
</update>

还有一个拼写错误:parentIds应该是parentId以匹配类属性。