Skip to content

Commit 177665d

Browse files
committed
fixes #18 When a Map is passed to foreach, ${index} is replaced with number instead of map key. This should also be the right fix for #10.
1 parent b5efe33 commit 177665d

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

src/main/java/org/apache/ibatis/scripting/xmltags/ForEachSqlNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public boolean apply(DynamicContext context) {
7070
applyIndex(context, mapEntry.getKey(), uniqueNumber);
7171
applyItem(context, mapEntry.getValue(), uniqueNumber);
7272
} else {
73-
applyIndex(context, uniqueNumber, i);
73+
applyIndex(context, i, uniqueNumber);
7474
applyItem(context, o, uniqueNumber);
7575
}
7676
contents.apply(new FilteredDynamicContext(configuration, context, index, item, uniqueNumber));
@@ -84,7 +84,7 @@ public boolean apply(DynamicContext context) {
8484

8585
private void applyIndex(DynamicContext context, Object o, int i) {
8686
if (index != null) {
87-
context.bind(index, i);
87+
context.bind(index, o);
8888
context.bind(itemizeItem(index, i), o);
8989
}
9090
}

src/test/java/org/apache/ibatis/submitted/foreach_map/CreateDB.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,15 @@ create table nested_bean (
3939
valuea integer,
4040
valueb boolean
4141
);
42+
43+
drop table key_cols if exists;
44+
45+
create table key_cols (
46+
id identity,
47+
col_a integer,
48+
col_b integer
49+
);
50+
51+
insert into key_cols (id, col_a, col_b) values (1, 11, 222);
52+
insert into key_cols (id, col_a, col_b) values (2, 22, 222);
53+
insert into key_cols (id, col_a, col_b) values (3, 22, 333);

src/test/java/org/apache/ibatis/submitted/foreach_map/ForEachMapTest.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@
1515
*/
1616
package org.apache.ibatis.submitted.foreach_map;
1717

18+
import java.io.Reader;
19+
import java.sql.Connection;
20+
import java.util.List;
21+
1822
import org.apache.ibatis.io.Resources;
1923
import org.apache.ibatis.jdbc.ScriptRunner;
2024
import org.apache.ibatis.session.SqlSession;
2125
import org.apache.ibatis.session.SqlSessionFactory;
2226
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
23-
import org.junit.*;
24-
25-
import java.io.Reader;
26-
import java.sql.Connection;
27-
import java.util.List;
27+
import org.junit.After;
28+
import org.junit.Assert;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
2832

2933
public class ForEachMapTest {
3034

@@ -93,6 +97,15 @@ public void shouldGetNestedBeanKeyValueEntries() throws Exception {
9397
Assert.assertEquals(new NestedBeanMapEntry(12345, true, 54321, false), entries.get(0));
9498
Assert.assertEquals(new NestedBeanMapEntry(67890, true, 9876, false), entries.get(1));
9599
}
100+
101+
@Test
102+
public void shouldSubstituteIndexWithKey() throws Exception {
103+
MapParam mapParam = new MapParam();
104+
mapParam.getMap().put("col_a", 22);
105+
mapParam.getMap().put("col_b", 222);
106+
int count = sqlSession.selectOne("sel_key_cols", mapParam);
107+
Assert.assertEquals(1, count);
108+
}
96109

97110
private SqlSession sqlSession;
98111
}

src/test/java/org/apache/ibatis/submitted/foreach_map/Mapper.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,11 @@
4949
<select id="sel_nested_bean" resultType="org.apache.ibatis.submitted.foreach_map.NestedBeanMapEntry">
5050
select * from nested_bean order by id
5151
</select>
52+
53+
<select id="sel_key_cols" resultType="int">
54+
select count(*) from key_cols where
55+
<foreach item="item" index="key" collection="map"
56+
open="" separator="AND" close="">${key} = #{item}</foreach>
57+
</select>
58+
5259
</mapper>

0 commit comments

Comments
 (0)