Skip to content

Commit 87b2c88

Browse files
committed
fixes #155 Allow naming a result map in java mapper interface.
1 parent 1ccb988 commit 87b2c88

File tree

12 files changed

+427
-0
lines changed

12 files changed

+427
-0
lines changed

src/main/java/org/apache/ibatis/annotations/Results.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,9 @@
2626
@Retention(RetentionPolicy.RUNTIME)
2727
@Target(ElementType.METHOD)
2828
public @interface Results {
29+
/**
30+
* The name of the result map.
31+
*/
32+
String id() default "";
2933
Result[] value() default {};
3034
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ private String parseResultMap(Method method) {
196196
}
197197

198198
private String generateResultMapName(Method method) {
199+
Results results = method.getAnnotation(Results.class);
200+
if (results != null && !results.id().isEmpty()) {
201+
return type.getName() + "." + results.id();
202+
}
199203
StringBuilder suffix = new StringBuilder();
200204
for (Class<?> c : method.getParameterTypes()) {
201205
suffix.append("-");
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2009-2015 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+
package org.apache.ibatis.submitted.results_id;
18+
19+
import java.util.List;
20+
21+
import org.apache.ibatis.annotations.ResultMap;
22+
import org.apache.ibatis.annotations.Select;
23+
24+
public interface AnotherMapper {
25+
26+
@ResultMap("org.apache.ibatis.submitted.results_id.Mapper.userResult")
27+
@Select("select * from users order by uid")
28+
List<User> getUsers();
29+
30+
User getUser(Integer id);
31+
32+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2009-2015 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.results_id.AnotherMapper">
22+
<select id="getUser" resultMap="org.apache.ibatis.submitted.results_id.Mapper.userResult">
23+
select * from users where uid = #{id}
24+
</select>
25+
</mapper>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--
2+
-- Copyright 2009-2015 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+
uid int,
21+
name varchar(20)
22+
);
23+
24+
insert into users (uid, name) values(1, 'User1');
25+
insert into users (uid, name) values(2, 'User2');
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright 2009-2015 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.results_id;
17+
18+
import org.apache.ibatis.annotations.Result;
19+
import org.apache.ibatis.annotations.Results;
20+
import org.apache.ibatis.annotations.Select;
21+
22+
public interface IdConflictMapper {
23+
24+
@Results(id = "userResult", value = { @Result(id = true, column = "uid", property = "id") })
25+
@Select("select * from users where uid = #{id}")
26+
User getUserById(Integer id);
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2009-2015 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.results_id.IdConflictMapper">
22+
<resultMap type="org.apache.ibatis.submitted.results_id.User"
23+
id="userResult">
24+
<id property="id" column="uid" />
25+
</resultMap>
26+
</mapper>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2009-2015 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.results_id;
17+
18+
import org.apache.ibatis.session.Configuration;
19+
import org.junit.Rule;
20+
import org.junit.Test;
21+
import org.junit.rules.ExpectedException;
22+
23+
public class IdConflictTest {
24+
25+
@Rule
26+
public ExpectedException ex = ExpectedException.none();
27+
28+
@Test
29+
public void shouldFailOnDuplicatedId() throws Exception {
30+
ex.expect(RuntimeException.class);
31+
ex.expectMessage("Result Maps collection already contains value for org.apache.ibatis.submitted.results_id.IdConflictMapper.userResult");
32+
33+
Configuration configuration = new Configuration();
34+
configuration.addMapper(IdConflictMapper.class);
35+
configuration.getMappedStatements();
36+
}
37+
38+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright 2009-2015 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.results_id;
17+
18+
import org.apache.ibatis.annotations.Arg;
19+
import org.apache.ibatis.annotations.ConstructorArgs;
20+
import org.apache.ibatis.annotations.Result;
21+
import org.apache.ibatis.annotations.ResultMap;
22+
import org.apache.ibatis.annotations.Results;
23+
import org.apache.ibatis.annotations.Select;
24+
25+
public interface Mapper {
26+
27+
@Results(id = "userResult", value = {
28+
@Result(id = true, column = "uid", property = "id"),
29+
@Result(column = "name", property = "name")
30+
})
31+
@Select("select * from users where uid = #{id}")
32+
User getUserById(Integer id);
33+
34+
@ResultMap("userResult")
35+
@Select("select * from users where name = #{name}")
36+
User getUserByName(String name);
37+
38+
@Results(id = "userResultConstructor")
39+
@ConstructorArgs({
40+
@Arg(id = true, column = "uid", javaType = Integer.class),
41+
@Arg(column = "name", javaType = String.class)
42+
})
43+
@Select("select * from users where uid = #{id}")
44+
User getUserByIdConstructor(Integer id);
45+
46+
@ResultMap("userResultConstructor")
47+
@Select("select * from users where name = #{name}")
48+
User getUserByNameConstructor(String name);
49+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* Copyright 2009-2015 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.results_id;
17+
18+
import static org.junit.Assert.*;
19+
20+
import java.io.Reader;
21+
import java.sql.Connection;
22+
import java.util.List;
23+
24+
import org.apache.ibatis.io.Resources;
25+
import org.apache.ibatis.jdbc.ScriptRunner;
26+
import org.apache.ibatis.session.SqlSession;
27+
import org.apache.ibatis.session.SqlSessionFactory;
28+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
32+
public class ResultsIdTest {
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/results_id/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/results_id/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 testNamingResults() {
56+
SqlSession sqlSession = sqlSessionFactory.openSession();
57+
try {
58+
Mapper mapper = sqlSession.getMapper(Mapper.class);
59+
User user = mapper.getUserByName("User2");
60+
assertEquals(Integer.valueOf(2), user.getId());
61+
assertEquals("User2", user.getName());
62+
} finally {
63+
sqlSession.close();
64+
}
65+
}
66+
67+
@Test
68+
public void testResultsOnlyForNaming() {
69+
SqlSession sqlSession = sqlSessionFactory.openSession();
70+
try {
71+
Mapper mapper = sqlSession.getMapper(Mapper.class);
72+
User user = mapper.getUserByNameConstructor("User2");
73+
assertEquals(Integer.valueOf(2), user.getId());
74+
assertEquals("User2", user.getName());
75+
} finally {
76+
sqlSession.close();
77+
}
78+
}
79+
80+
@Test
81+
public void testReuseNamedResultsFromAnotherMapper() {
82+
SqlSession sqlSession = sqlSessionFactory.openSession();
83+
try {
84+
AnotherMapper mapper = sqlSession.getMapper(AnotherMapper.class);
85+
List<User> users = mapper.getUsers();
86+
assertEquals(2, users.size());
87+
assertEquals(Integer.valueOf(1), users.get(0).getId());
88+
assertEquals("User1", users.get(0).getName());
89+
assertEquals(Integer.valueOf(2), users.get(1).getId());
90+
assertEquals("User2", users.get(1).getName());
91+
} finally {
92+
sqlSession.close();
93+
}
94+
}
95+
96+
@Test
97+
public void testReuseNamedResultsFromXmlMapper() {
98+
SqlSession sqlSession = sqlSessionFactory.openSession();
99+
try {
100+
AnotherMapper mapper = sqlSession.getMapper(AnotherMapper.class);
101+
User user = mapper.getUser(1);
102+
assertEquals(Integer.valueOf(1), user.getId());
103+
assertEquals("User1", user.getName());
104+
} finally {
105+
sqlSession.close();
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)