在 mybatis foreach 标签中获取元素的索引?
Get element's index in mybatis foreach tag ?
我正在尝试遍历 mybatis 中的 arraylist 并想将元素的位置传递给(如 a[i],然后我想要每个元素的 i 值)有没有办法在mybatis ?
我使用以下查询来测试这个...
<select id="test" resultType="Long" parameterType="something.Testy">
<foreach item="item" index="index" collection="list" open="" separator=" UNION " close="">
SELECT #{index} FROM DUAL
</foreach>
</select>
其中 something.Testy
是一个简单的对象:
public class Testy {
public List<Integer> getList() {
return Arrays.asList( 4, 5, 6 );
}
}
这个结果是一个列表 [ 0, 1, 2 ]
,所以 #{index}
get 是根据当前的 MyBatis 版本 (3.3.0) 正确确定的。 MyBatis 创建的查询看起来像这样...
SELECT ? FROM DUAL UNION SELECT ? FROM DUAL UNION SELECT ? FROM DUAL
Parameters: 0(Integer), 1(Integer), 2(Integer)
也许您需要更新您的 MyBatis 版本?可能是旧版本中的错误...
我正在尝试遍历 mybatis 中的 arraylist 并想将元素的位置传递给(如 a[i],然后我想要每个元素的 i 值)有没有办法在mybatis ?
我使用以下查询来测试这个...
<select id="test" resultType="Long" parameterType="something.Testy">
<foreach item="item" index="index" collection="list" open="" separator=" UNION " close="">
SELECT #{index} FROM DUAL
</foreach>
</select>
其中 something.Testy
是一个简单的对象:
public class Testy {
public List<Integer> getList() {
return Arrays.asList( 4, 5, 6 );
}
}
这个结果是一个列表 [ 0, 1, 2 ]
,所以 #{index}
get 是根据当前的 MyBatis 版本 (3.3.0) 正确确定的。 MyBatis 创建的查询看起来像这样...
SELECT ? FROM DUAL UNION SELECT ? FROM DUAL UNION SELECT ? FROM DUAL
Parameters: 0(Integer), 1(Integer), 2(Integer)
也许您需要更新您的 MyBatis 版本?可能是旧版本中的错误...