Skip to content

Commit 120928a

Browse files
committed
1 parent 5fb8958 commit 120928a

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

doc/en/MyBatis-3-User-Guide.doc

1 KB
Binary file not shown.

doc/en/MyBatis-3-User-Guide.pdf

2.74 KB
Binary file not shown.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.apache.ibatis.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.METHOD)
10+
public @interface ResultMap {
11+
public abstract String value();
12+
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.ibatis.annotations.InsertProvider;
2727
import org.apache.ibatis.annotations.Options;
2828
import org.apache.ibatis.annotations.Result;
29+
import org.apache.ibatis.annotations.ResultMap;
2930
import org.apache.ibatis.annotations.Results;
3031
import org.apache.ibatis.annotations.Select;
3132
import org.apache.ibatis.annotations.SelectKey;
@@ -251,6 +252,14 @@ private void parseStatement(Method method) {
251252
resultSetType = options.resultSetType();
252253
}
253254

255+
ResultMap resultMapAnnotation = method.getAnnotation(ResultMap.class);
256+
String resultMapId;
257+
if (resultMapAnnotation == null) {
258+
resultMapId = generateResultMapName(method);
259+
} else {
260+
resultMapId = resultMapAnnotation.value();
261+
}
262+
254263
assistant.addMappedStatement(
255264
mappedStatementId,
256265
sqlSource,
@@ -260,7 +269,7 @@ private void parseStatement(Method method) {
260269
timeout,
261270
null, // ParameterMapID
262271
getParameterType(method),
263-
generateResultMapName(method), // ResultMapID
272+
resultMapId, // ResultMapID
264273
getReturnType(method),
265274
resultSetType,
266275
flushCache,

src/test/java/org/apache/ibatis/submitted/complex_column/ComplexColumnTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public void testWithoutComplex() {
6161
Assert.assertEquals("Smith", parent.getLastName());
6262
sqlSession.close();
6363
}
64+
6465
@Test
6566
public void testWithComplex() {
6667
SqlSession sqlSession = sqlSessionFactory.openSession();
@@ -76,4 +77,36 @@ public void testWithComplex() {
7677
sqlSession.close();
7778

7879
}
80+
81+
@Test
82+
public void testWithComplex2() {
83+
SqlSession sqlSession = sqlSessionFactory.openSession();
84+
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
85+
Person person = personMapper.getWithComplex2(2l);
86+
Assert.assertNotNull("person must not be null", person);
87+
Assert.assertEquals("Christian", person.getFirstName());
88+
Assert.assertEquals("Poitras", person.getLastName());
89+
Person parent = person.getParent();
90+
Assert.assertNotNull("parent must not be null", parent);
91+
Assert.assertEquals("John", parent.getFirstName());
92+
Assert.assertEquals("Smith", parent.getLastName());
93+
sqlSession.close();
94+
95+
}
96+
97+
@Test
98+
public void testWithComplex3() {
99+
SqlSession sqlSession = sqlSessionFactory.openSession();
100+
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
101+
Person person = personMapper.getWithComplex3(2l);
102+
Assert.assertNotNull("person must not be null", person);
103+
Assert.assertEquals("Christian", person.getFirstName());
104+
Assert.assertEquals("Poitras", person.getLastName());
105+
Person parent = person.getParent();
106+
Assert.assertNotNull("parent must not be null", parent);
107+
Assert.assertEquals("John", parent.getFirstName());
108+
Assert.assertEquals("Smith", parent.getLastName());
109+
sqlSession.close();
110+
111+
}
79112
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
package org.apache.ibatis.submitted.complex_column;
22

3+
import org.apache.ibatis.annotations.ResultMap;
4+
import org.apache.ibatis.annotations.Select;
5+
36
public interface PersonMapper {
47

58
public Person getWithoutComplex(Long id);
69
public Person getWithComplex(Long id);
10+
11+
@Select({
12+
"SELECT id, firstName, lastName, parent_id, parent_firstName, parent_lastName",
13+
"FROM Person",
14+
"WHERE id = #{id,jdbcType=INTEGER}"
15+
})
16+
@ResultMap("personMapComplex")
17+
public Person getWithComplex2(Long id);
18+
19+
@Select({
20+
"SELECT id, firstName, lastName, parent_id, parent_firstName, parent_lastName",
21+
"FROM Person",
22+
"WHERE id = #{id,jdbcType=INTEGER}"
23+
})
24+
@ResultMap("org.apache.ibatis.submitted.complex_column.PersonMapper.personMapComplex")
25+
public Person getWithComplex3(Long id);
726
}

0 commit comments

Comments
 (0)