Skip to content

Commit 8a98c8d

Browse files
committed
Fix for http://code.google.com/p/mybatis/issues/detail?id=203 - parameterMap fails with type="map" and no declared javaType
1 parent cafd10c commit 8a98c8d

File tree

4 files changed

+53
-20
lines changed

4 files changed

+53
-20
lines changed

src/main/java/org/apache/ibatis/builder/MapperBuilderAssistant.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ private Class<?> resolveParameterJavaType(Class<?> resultType, String property,
353353
if (javaType == null) {
354354
if (JdbcType.CURSOR.equals(jdbcType)) {
355355
javaType = java.sql.ResultSet.class;
356+
} else if (Map.class.isAssignableFrom(resultType)) {
357+
javaType = Object.class;
356358
} else {
357359
MetaClass metaResultType = MetaClass.forClass(resultType);
358360
javaType = metaResultType.getGetterType(property);

src/test/java/org/apache/ibatis/submitted/sptests/SPMapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public interface SPMapper {
77
Object adder(Parameter parameter);
88
void adder2(Parameter parameter);
9+
void adder3(Map<String, Object> parameter);
910
Name getName(Integer id);
1011
List<Name> getNames(Map<String, Object> parms);
1112
List<Name> getNamesWithArray(Map<String, Object> parms);

src/test/java/org/apache/ibatis/submitted/sptests/SPMapper.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
<result column="LAST_NAME" property="lastName"/>
88
</resultMap>
99

10+
<parameterMap type="map" id="testParameterMap">
11+
<parameter property="addend1" jdbcType="INTEGER" mode="IN"/>
12+
<parameter property="addend2" jdbcType="INTEGER" mode="IN"/>
13+
<parameter property="sum" jdbcType="INTEGER" mode="OUT"/>
14+
</parameterMap>
15+
1016
<!-- Important things for stored procedures:
1117
1. Must set the statement type to CALLABLE
1218
2. Must use the JDBC standard escape sequence for stored procedures:
@@ -37,6 +43,10 @@
3743
)}
3844
</update>
3945

46+
<update id="adder3" parameterMap="testParameterMap" statementType="CALLABLE">
47+
{call sptest.adder(?, ?, ?)}
48+
</update>
49+
4050
<select id="getName" parameterType="java.lang.Integer" statementType="CALLABLE"
4151
resultMap="nameResult">
4252
{call sptest.getname(

src/test/java/org/apache/ibatis/submitted/sptests/SPTest.java

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ public void testAdderAsSelect() {
8282
* in a stored procedure.
8383
* This procedure does not return a result set.
8484
*
85-
* Currently this test will fail without clearing the cache because
86-
* of a MyBatis cache issue.
87-
*
8885
* This test shows using a multi-property parameter.
8986
*/
9087
@Test
@@ -100,9 +97,6 @@ public void testAdderAsSelectDoubleCall1() {
10097
spMapper.adder(parameter);
10198
assertEquals((Integer) 5, parameter.getSum());
10299

103-
// Resolved Output Parameter Caching Issue for Callable statements
104-
// sqlSession.clearCache();
105-
106100
parameter = new Parameter();
107101
parameter.setAddend1(2);
108102
parameter.setAddend2(3);
@@ -119,10 +113,10 @@ public void testAdderAsSelectDoubleCall1() {
119113
* in a stored procedure.
120114
* This procedure does not return a result set.
121115
*
122-
* This test shows using a multi-property parameter.
116+
* This test also demonstrates session level cache for
117+
* output parameters.
123118
*
124-
* This test shows that the cache problem does not manifest if
125-
* the input parameters are different.
119+
* This test shows using a multi-property parameter.
126120
*/
127121
@Test
128122
public void testAdderAsSelectDoubleCall2() {
@@ -149,8 +143,7 @@ public void testAdderAsSelectDoubleCall2() {
149143
}
150144

151145
/**
152-
* This test shows that the cache problem is not in effect if
153-
* stored procedures with out parms are defined as <update> rather
146+
* This test shows how to call a stored procedure defined as <update> rather
154147
* then <select>. Of course, this only works if you are not returning
155148
* a result set.
156149
*
@@ -180,6 +173,36 @@ public void testAdderAsUpdate() {
180173
}
181174
}
182175

176+
/**
177+
* This test shows the use of a declared parameter map.
178+
* We generally prefer inline parameters, because the syntax
179+
* is more intuitive (no pesky question marks), but a parameter map
180+
* will work.
181+
*/
182+
@Test
183+
public void testAdderAsUpdateWithParameterMap() {
184+
SqlSession sqlSession = sqlSessionFactory.openSession();
185+
try {
186+
Map<String, Object> parms = new HashMap<String, Object>();
187+
parms.put("addend1", 3);
188+
parms.put("addend2", 4);
189+
190+
SPMapper spMapper = sqlSession.getMapper(SPMapper.class);
191+
192+
spMapper.adder3(parms);
193+
assertEquals(7, parms.get("sum"));
194+
195+
parms = new HashMap<String, Object>();
196+
parms.put("addend1", 2);
197+
parms.put("addend2", 3);
198+
spMapper.adder3(parms);
199+
assertEquals(5, parms.get("sum"));
200+
201+
} finally {
202+
sqlSession.close();
203+
}
204+
}
205+
183206
/**
184207
* This test shows how to use an input parameter and return a result
185208
* set from a stored procedure.
@@ -229,9 +252,6 @@ public void testCallWithResultSet2() {
229252
* return a result set from a stored procedure.
230253
*
231254
* This test shows using a Map parameter.
232-
*
233-
* This test shows that the cache problem does not manifest if
234-
* the input parameters are different.
235255
*/
236256
@Test
237257
@Ignore("until hsqldb 2.0.1 is released")
@@ -261,9 +281,6 @@ public void testCallWithResultSet3() {
261281
* return a result set from a stored procedure.
262282
*
263283
* This test shows using a Map parameter.
264-
*
265-
* Currently this test will fail without clearing the cache because
266-
* of a MyBatis cache issue.
267284
*/
268285
@Test
269286
@Ignore("until hsqldb 2.0.1 is released")
@@ -278,9 +295,6 @@ public void testCallWithResultSet4() {
278295
assertEquals(2, parms.get("totalRows"));
279296
assertEquals(2, names.size());
280297

281-
// Resolved Output Parameter Caching Issue for Callable statements
282-
// sqlSession.clearCache();
283-
284298
parms = new HashMap<String, Object>();
285299
parms.put("lowestId", 2);
286300
names = spMapper.getNames(parms);
@@ -291,6 +305,12 @@ public void testCallWithResultSet4() {
291305
}
292306
}
293307

308+
/**
309+
* This test shows how to use the ARRAY JDBC type
310+
* with MyBatis.
311+
*
312+
* @throws SQLException
313+
*/
294314
@Test
295315
@Ignore("until hsqldb 2.0.1 is released")
296316
public void testGetNamesWithArray() throws SQLException {

0 commit comments

Comments
 (0)