Skip to content

Commit 4b465eb

Browse files
committed
fixes #481 Looks for a matching statement defined in super interfaces recursively. Related to #35 .
1 parent 4306af4 commit 4b465eb

File tree

7 files changed

+119
-19
lines changed

7 files changed

+119
-19
lines changed

src/main/java/org/apache/ibatis/binding/MapperMethod.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2016 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -212,22 +212,17 @@ public static class SqlCommand {
212212
private final SqlCommandType type;
213213

214214
public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
215-
String statementName = mapperInterface.getName() + "." + method.getName();
216-
MappedStatement ms = null;
217-
if (configuration.hasStatement(statementName)) {
218-
ms = configuration.getMappedStatement(statementName);
219-
} else if (!mapperInterface.equals(method.getDeclaringClass())) { // issue #35
220-
String parentStatementName = method.getDeclaringClass().getName() + "." + method.getName();
221-
if (configuration.hasStatement(parentStatementName)) {
222-
ms = configuration.getMappedStatement(parentStatementName);
223-
}
224-
}
215+
final String methodName = method.getName();
216+
final Class<?> declaringClass = method.getDeclaringClass();
217+
MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass,
218+
configuration);
225219
if (ms == null) {
226-
if(method.getAnnotation(Flush.class) != null){
220+
if (method.getAnnotation(Flush.class) != null) {
227221
name = null;
228222
type = SqlCommandType.FLUSH;
229223
} else {
230-
throw new BindingException("Invalid bound statement (not found): " + statementName);
224+
throw new BindingException("Invalid bound statement (not found): "
225+
+ mapperInterface.getName() + "." + methodName);
231226
}
232227
} else {
233228
name = ms.getId();
@@ -245,6 +240,26 @@ public String getName() {
245240
public SqlCommandType getType() {
246241
return type;
247242
}
243+
244+
private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName,
245+
Class<?> declaringClass, Configuration configuration) {
246+
String statementId = mapperInterface.getName() + "." + methodName;
247+
if (configuration.hasStatement(statementId)) {
248+
return configuration.getMappedStatement(statementId);
249+
} else if (mapperInterface.equals(declaringClass)) {
250+
return null;
251+
}
252+
for (Class<?> superInterface : mapperInterface.getInterfaces()) {
253+
if (declaringClass.isAssignableFrom(superInterface)) {
254+
MappedStatement ms = resolveMappedStatement(superInterface, methodName,
255+
declaringClass, configuration);
256+
if (ms != null) {
257+
return ms;
258+
}
259+
}
260+
}
261+
return null;
262+
}
248263
}
249264

250265
public static class MethodSignature {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright 2009-2017 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.mapper_extend;
17+
18+
public interface ChildMapper extends ParentMapper {
19+
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2009-2017 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.mapper_extend;
18+
19+
public interface GrandpaMapper {
20+
21+
User getUserByName(String name);
22+
23+
User noMappedStatement();
24+
25+
}

src/test/java/org/apache/ibatis/submitted/mapper_extend/MapperExtendTest.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,20 +15,28 @@
1515
*/
1616
package org.apache.ibatis.submitted.mapper_extend;
1717

18+
import static org.hamcrest.CoreMatchers.*;
19+
1820
import java.io.Reader;
1921
import java.sql.Connection;
2022

23+
import org.apache.ibatis.binding.BindingException;
2124
import org.apache.ibatis.io.Resources;
2225
import org.apache.ibatis.jdbc.ScriptRunner;
2326
import org.apache.ibatis.session.SqlSession;
2427
import org.apache.ibatis.session.SqlSessionFactory;
2528
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
2629
import org.junit.Assert;
2730
import org.junit.BeforeClass;
31+
import org.junit.Rule;
2832
import org.junit.Test;
33+
import org.junit.rules.ExpectedException;
2934

3035
public class MapperExtendTest {
3136

37+
@Rule
38+
public ExpectedException expectedException = ExpectedException.none();
39+
3240
private static SqlSessionFactory sqlSessionFactory;
3341

3442
@BeforeClass
@@ -97,5 +105,32 @@ public void shouldGetAUserWithAnOverloadedAnnotatedMethod() {
97105
sqlSession.close();
98106
}
99107
}
100-
108+
109+
@Test
110+
public void shouldFindStatementInSubInterfaceOfDeclaringClass() {
111+
SqlSession sqlSession = sqlSessionFactory.openSession();
112+
try {
113+
ChildMapper mapper = sqlSession.getMapper(ChildMapper.class);
114+
User user = mapper.getUserByName("User1");
115+
Assert.assertNotNull(user);
116+
} finally {
117+
sqlSession.close();
118+
}
119+
}
120+
121+
@Test
122+
public void shouldThrowExceptionIfNoMatchingStatementFound() {
123+
expectedException.expect(BindingException.class);
124+
expectedException.expectMessage(is("Invalid bound statement (not found): "
125+
+ Mapper.class.getName() + ".noMappedStatement"));
126+
127+
SqlSession sqlSession = sqlSessionFactory.openSession();
128+
try {
129+
Mapper mapper = sqlSession.getMapper(Mapper.class);
130+
User user = mapper.noMappedStatement();
131+
Assert.assertNotNull(user);
132+
} finally {
133+
sqlSession.close();
134+
}
135+
}
101136
}

src/test/java/org/apache/ibatis/submitted/mapper_extend/ParentMapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@
1717

1818
import org.apache.ibatis.annotations.Select;
1919

20-
public interface ParentMapper {
20+
public interface ParentMapper extends GrandpaMapper {
2121

2222
User getUserXML();
2323

src/test/java/org/apache/ibatis/submitted/mapper_extend/ParentMapper.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2009-2016 the original author or authors.
4+
Copyright 2009-2017 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -22,6 +22,10 @@
2222

2323
<mapper namespace="org.apache.ibatis.submitted.mapper_extend.ParentMapper">
2424

25+
<select id="getUserByName" resultType="org.apache.ibatis.submitted.mapper_extend.User">
26+
select * from users where name = #{name}
27+
</select>
28+
2529
<select id="getUserXML" resultType="org.apache.ibatis.submitted.mapper_extend.User">
2630
select * from users where id = 1
2731
</select>

src/test/java/org/apache/ibatis/submitted/mapper_extend/mybatis-config.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<!--
33
4-
Copyright 2009-2016 the original author or authors.
4+
Copyright 2009-2017 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@
3939
<mapper class="org.apache.ibatis.submitted.mapper_extend.ParentMapper" />
4040
<mapper class="org.apache.ibatis.submitted.mapper_extend.Mapper" />
4141
<mapper class="org.apache.ibatis.submitted.mapper_extend.MapperOverload" />
42+
<mapper class="org.apache.ibatis.submitted.mapper_extend.ChildMapper" />
4243
</mappers>
4344

4445
</configuration>

0 commit comments

Comments
 (0)