Skip to content

Commit 636ca74

Browse files
committed
Fixes mybatis#239. Support of Set.java as collection
1 parent 7c218b1 commit 636ca74

File tree

7 files changed

+288
-3
lines changed

7 files changed

+288
-3
lines changed

src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSession.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.sql.Connection;
1919
import java.sql.SQLException;
20+
import java.util.Collection;
2021
import java.util.HashMap;
2122
import java.util.List;
2223
import java.util.Map;
@@ -239,10 +240,13 @@ private boolean isCommitOrRollbackRequired(boolean force) {
239240
}
240241

241242
private Object wrapCollection(final Object object) {
242-
if (object instanceof List) {
243+
if (object instanceof Collection) {
243244
StrictMap<Object> map = new StrictMap<Object>();
244-
map.put("list", object);
245-
return map;
245+
map.put("collection", object);
246+
if (object instanceof List) {
247+
map.put("list", object);
248+
}
249+
return map;
246250
} else if (object != null && object.getClass().isArray()) {
247251
StrictMap<Object> map = new StrictMap<Object>();
248252
map.put("array", object);
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2009-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.collectionparameters;
17+
18+
import java.io.Reader;
19+
import java.sql.Connection;
20+
import java.util.ArrayList;
21+
import java.util.HashSet;
22+
import java.util.List;
23+
import java.util.Set;
24+
25+
import org.apache.ibatis.io.Resources;
26+
import org.apache.ibatis.jdbc.ScriptRunner;
27+
import org.apache.ibatis.session.SqlSession;
28+
import org.apache.ibatis.session.SqlSessionFactory;
29+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
30+
import org.junit.Assert;
31+
import org.junit.BeforeClass;
32+
import org.junit.Test;
33+
34+
public class CollectionParametersTest {
35+
36+
private static SqlSessionFactory sqlSessionFactory;
37+
38+
@BeforeClass
39+
public static void setUp() throws Exception {
40+
// create an SqlSessionFactory
41+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/collectionparameters/mybatis-config.xml");
42+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
43+
reader.close();
44+
45+
// populate in-memory database
46+
SqlSession session = sqlSessionFactory.openSession();
47+
Connection conn = session.getConnection();
48+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/collectionparameters/CreateDB.sql");
49+
ScriptRunner runner = new ScriptRunner(conn);
50+
runner.setLogWriter(null);
51+
runner.runScript(reader);
52+
reader.close();
53+
session.close();
54+
}
55+
56+
@Test
57+
public void shouldGetTwoUsersPassingAList() {
58+
SqlSession sqlSession = sqlSessionFactory.openSession();
59+
try {
60+
Mapper mapper = sqlSession.getMapper(Mapper.class);
61+
ArrayList<Integer> list = new ArrayList<Integer>();
62+
list.add(1);
63+
list.add(2);
64+
List<User> users = mapper.getUsersFromList(list);
65+
Assert.assertEquals(2, users.size());
66+
} finally {
67+
sqlSession.close();
68+
}
69+
}
70+
71+
@Test
72+
public void shouldGetTwoUsersPassingAnArray() {
73+
SqlSession sqlSession = sqlSessionFactory.openSession();
74+
try {
75+
Mapper mapper = sqlSession.getMapper(Mapper.class);
76+
Integer[] list = new Integer[2];
77+
list[0]=1;
78+
list[1]=2;
79+
List<User> users = mapper.getUsersFromArray(list);
80+
Assert.assertEquals(2, users.size());
81+
} finally {
82+
sqlSession.close();
83+
}
84+
}
85+
86+
@Test
87+
public void shouldGetTwoUsersPassingACollection() {
88+
SqlSession sqlSession = sqlSessionFactory.openSession();
89+
try {
90+
Mapper mapper = sqlSession.getMapper(Mapper.class);
91+
Set<Integer> list = new HashSet<Integer>();
92+
list.add(1);
93+
list.add(2);
94+
List<User> users = mapper.getUsersFromCollection(list);
95+
Assert.assertEquals(2, users.size());
96+
} finally {
97+
sqlSession.close();
98+
}
99+
}
100+
101+
102+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--
2+
-- Copyright 2009-2012 the original author or authors.
3+
--
4+
-- Licensed under the Apache License, Version 2.0 (the "License");
5+
-- you may not use this file except in compliance with the License.
6+
-- You may obtain a copy of the License at
7+
--
8+
-- http://www.apache.org/licenses/LICENSE-2.0
9+
--
10+
-- Unless required by applicable law or agreed to in writing, software
11+
-- distributed under the License is distributed on an "AS IS" BASIS,
12+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
-- See the License for the specific language governing permissions and
14+
-- limitations under the License.
15+
--
16+
17+
drop table users if exists;
18+
19+
create table users (
20+
id int,
21+
name varchar(20)
22+
);
23+
24+
insert into users (id, name) values(1, 'User1');
25+
insert into users (id, name) values(2, 'User2');
26+
insert into users (id, name) values(3, 'User3');
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2009-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.collectionparameters;
17+
18+
import java.util.List;
19+
import java.util.Set;
20+
21+
public interface Mapper {
22+
23+
List<User> getUsersFromList(List<Integer> id);
24+
List<User> getUsersFromArray(Integer[] id);
25+
List<User> getUsersFromCollection(Set<Integer> id);
26+
27+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2009-2013 the original author or authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!DOCTYPE mapper
18+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
19+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
20+
21+
<mapper namespace="org.apache.ibatis.submitted.collectionparameters.Mapper">
22+
23+
<select id="getUsersFromList"
24+
resultType="org.apache.ibatis.submitted.collectionparameters.User">
25+
select * from users where id in
26+
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
27+
#{item}
28+
</foreach>
29+
</select>
30+
31+
<select id="getUsersFromArray"
32+
resultType="org.apache.ibatis.submitted.collectionparameters.User">
33+
select * from users where id in
34+
<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
35+
#{item}
36+
</foreach>
37+
</select>
38+
39+
<select id="getUsersFromCollection"
40+
resultType="org.apache.ibatis.submitted.collectionparameters.User">
41+
select * from users where id in
42+
<foreach item="item" index="index" collection="collection" open="(" separator="," close=")">
43+
#{item}
44+
</foreach>
45+
</select>
46+
47+
48+
</mapper>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2009-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.collectionparameters;
17+
18+
public class User {
19+
20+
private Integer id;
21+
private String name;
22+
23+
public Integer getId() {
24+
return id;
25+
}
26+
27+
public void setId(Integer id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
Copyright 2009-2013 the original author or authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!DOCTYPE configuration
18+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
19+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
20+
21+
<configuration>
22+
23+
<environments default="development">
24+
<environment id="development">
25+
<transactionManager type="JDBC">
26+
<property name="" value="" />
27+
</transactionManager>
28+
<dataSource type="UNPOOLED">
29+
<property name="driver" value="org.hsqldb.jdbcDriver" />
30+
<property name="url" value="jdbc:hsqldb:mem:collectionparameters" />
31+
<property name="username" value="sa" />
32+
</dataSource>
33+
</environment>
34+
</environments>
35+
36+
<mappers>
37+
<mapper class="org.apache.ibatis.submitted.collectionparameters.Mapper" />
38+
</mappers>
39+
40+
</configuration>

0 commit comments

Comments
 (0)