Skip to content

Commit e9106e3

Browse files
committed
fixes #669 When @select returns an array, use its component type as the return type.
1 parent 3176cb8 commit e9106e3

File tree

7 files changed

+273
-0
lines changed

7 files changed

+273
-0
lines changed

src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java

+3
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ private Class<?> getReturnType(Method method) {
381381
Type resolvedReturnType = TypeParameterResolver.resolveReturnType(method, type);
382382
if (resolvedReturnType instanceof Class) {
383383
returnType = (Class<?>) resolvedReturnType;
384+
if (returnType.isArray()) {
385+
returnType = returnType.getComponentType();
386+
}
384387
// gcode issue #508
385388
if (void.class.equals(returnType)) {
386389
ResultType rt = method.getAnnotation(ResultType.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* Copyright 2009-2016 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.array_result_type;
17+
18+
import static org.junit.Assert.*;
19+
20+
import java.io.Reader;
21+
import java.sql.Connection;
22+
23+
import org.apache.ibatis.io.Resources;
24+
import org.apache.ibatis.jdbc.ScriptRunner;
25+
import org.apache.ibatis.session.SqlSession;
26+
import org.apache.ibatis.session.SqlSessionFactory;
27+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28+
import org.junit.BeforeClass;
29+
import org.junit.Ignore;
30+
import org.junit.Test;
31+
32+
public class ArrayResultTypeTest {
33+
34+
private static SqlSessionFactory sqlSessionFactory;
35+
36+
@BeforeClass
37+
public static void setUp() throws Exception {
38+
// create an SqlSessionFactory
39+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/array_result_type/mybatis-config.xml");
40+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41+
reader.close();
42+
43+
// populate in-memory database
44+
SqlSession session = sqlSessionFactory.openSession();
45+
Connection conn = session.getConnection();
46+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/array_result_type/CreateDB.sql");
47+
ScriptRunner runner = new ScriptRunner(conn);
48+
runner.setLogWriter(null);
49+
runner.runScript(reader);
50+
reader.close();
51+
session.close();
52+
}
53+
54+
@Test
55+
public void shouldGetUserArray() {
56+
SqlSession sqlSession = sqlSessionFactory.openSession();
57+
try {
58+
Mapper mapper = sqlSession.getMapper(Mapper.class);
59+
User[] users = mapper.getUsers();
60+
assertEquals("User1", users[0].getName());
61+
assertEquals("User2", users[1].getName());
62+
} finally {
63+
sqlSession.close();
64+
}
65+
}
66+
67+
@Test
68+
public void shouldGetUserArrayXml() {
69+
SqlSession sqlSession = sqlSessionFactory.openSession();
70+
try {
71+
Mapper mapper = sqlSession.getMapper(Mapper.class);
72+
User[] users = mapper.getUsersXml();
73+
assertEquals("User1", users[0].getName());
74+
assertEquals("User2", users[1].getName());
75+
} finally {
76+
sqlSession.close();
77+
}
78+
}
79+
80+
@Test
81+
public void shouldGetSimpleTypeArray() {
82+
SqlSession sqlSession = sqlSessionFactory.openSession();
83+
try {
84+
Mapper mapper = sqlSession.getMapper(Mapper.class);
85+
Integer[] ids = mapper.getUserIds();
86+
assertEquals(Integer.valueOf(1), ids[0]);
87+
} finally {
88+
sqlSession.close();
89+
}
90+
}
91+
92+
@Ignore("primitive array return type is not supported. #555")
93+
@Test
94+
public void shouldGetPrimitiveArray() {
95+
SqlSession sqlSession = sqlSessionFactory.openSession();
96+
try {
97+
Mapper mapper = sqlSession.getMapper(Mapper.class);
98+
int[] ids = mapper.getUserIdsPrimitive();
99+
assertEquals(1, ids[0]);
100+
} finally {
101+
sqlSession.close();
102+
}
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--
2+
-- Copyright 2009-2016 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');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2009-2016 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.array_result_type;
17+
18+
import org.apache.ibatis.annotations.Select;
19+
20+
public interface Mapper {
21+
22+
@Select("select * from users")
23+
User[] getUsers();
24+
25+
User[] getUsersXml();
26+
27+
@Select("select id from users")
28+
Integer[] getUserIds();
29+
30+
@Select("select id from users")
31+
int[] getUserIdsPrimitive();
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2016 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE mapper
20+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
22+
23+
<mapper namespace="org.apache.ibatis.submitted.array_result_type.Mapper">
24+
25+
<select id="getUsersXml" resultType="org.apache.ibatis.submitted.array_result_type.User">
26+
select * from users
27+
</select>
28+
29+
</mapper>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2009-2016 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.array_result_type;
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2009-2016 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE configuration
20+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
22+
23+
<configuration>
24+
25+
<environments default="development">
26+
<environment id="development">
27+
<transactionManager type="JDBC">
28+
<property name="" value="" />
29+
</transactionManager>
30+
<dataSource type="UNPOOLED">
31+
<property name="driver" value="org.hsqldb.jdbcDriver" />
32+
<property name="url" value="jdbc:hsqldb:mem:arrayresulttype" />
33+
<property name="username" value="sa" />
34+
</dataSource>
35+
</environment>
36+
</environments>
37+
38+
<mappers>
39+
<mapper class="org.apache.ibatis.submitted.array_result_type.Mapper" />
40+
</mappers>
41+
42+
</configuration>

0 commit comments

Comments
 (0)