-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow referencing single parameter using actual method argument name when compile with -parameters #1237
Comments
@zerda Thanks for your reporting! Probably, we can fix this issue as follow: @harawata WDYT? public class ParamNameResolver {
// ...
public Object getNamedParams(Object[] args) {
final int paramCount = names.size();
if (args == null || paramCount == 0) {
return null;
} else if (!hasParamAnnotation && paramCount == 1) {
- return args[names.firstKey()];
+ return wrapCollection(args[names.firstKey()], names.get(0));
} else {
final Map<String, Object> param = new ParamMap<Object>();
int i = 0;
for (Map.Entry<Integer, String> entry : names.entrySet()) {
param.put(entry.getValue(), args[entry.getKey()]);
// add generic param names (param1, param2, ...)
final String genericParamName = GENERIC_NAME_PREFIX + String.valueOf(i + 1);
// ensure not to overwrite parameter named with @Param
if (!names.containsValue(genericParamName)) {
param.put(genericParamName, args[entry.getKey()]);
}
i++;
}
return param;
}
}
+ private Object wrapCollection(final Object object, final String name) {
+ if (object instanceof Collection) {
+ ParamMap<Object> map = new ParamMap<Object>();
+ map.put(name, object);
+ map.put("collection", object);
+ if (object instanceof List) {
+ map.put("list", object);
+ }
+ return map;
+ } else if (object != null && object.getClass().isArray()) {
+ ParamMap<Object> map = new ParamMap<Object>();
+ map.put(name, object);
+ map.put("array", object);
+ return map;
+ }
+ return object;
+ }
} |
How about making the new method |
Thank you for the quick response. I see the solution is specified with collection and array type, it may not working when the first parameter is primitive or other class. <mapper>
<select id="getUserName" resultType="String">
select u.name
from users u
<where>
<if test="id != null">
u.id = #{id}
</if>
</where>
limit 1
</select>
</mapper> interface Mapper {
String getUserName(Integer id);
} |
@zerda thanks for quick reaction. I've tried it, ognl error occurred as follow:
Probably, a workaround to fix this behavior is as follows:
- <if test="id != null">
+ <if test="value != null"> Probably, it is not easy to fix this issue while keeping a backward compatibility. |
Not sure. I'll look into it when I have time. |
I uploaded a Unit Test to reproduce the issue, which can be accessible via https://github.com/xtuer/MyBatis-Issue-Unittest.git |
Sorry for the belated reply. The original report is about a statement with a single But this comment is about a statement with single non-list parameter. I might have answered @xtuer 's case somewhere, but |
About a your additional comment, Since 3.5.2 we allow any variable name on OGNL expression when specify single value object as parameter object via gh-1487. |
Allow using actual argument name as bind parameter on a single collection
We've fixed via #1487(already released at 3.5.2) and #1856. https://github.com/mybatis/mybatis-3/wiki/Maven Thanks! |
Hi Team, How to convert oracle object to java object using typehandler which we received as OUT parameter from Stored procedure using mybatis, java, sprinboot. CURSOR pension_pmt_cur IS
END Poll_File_Transmission; Response: 2020-12-13 15:23:47.089 INFO 1216 --- [pool-2-thread-1] c.p.h.s.b.d.PollFileTransmissionImpl : Poll File Transmission procedure call completed for PMT mapping in xml : |
MyBatis version
3.4.6
Database vendor and version
hsqldb
Test case or example project
https://github.com/zerda/mybatis-3/tree/param-name-resolve/src/test/java/org/apache/ibatis/submitted/param_name_resolve
Steps to reproduce
See test case.
-parameters
.Expected result
Get the correct result
3
.Actual result
An Exception raised.
Quick debug
At
ParamNameResolver.getNamedParams
, it may not correctly handle the case when parameter come with name, but not annotated.The text was updated successfully, but these errors were encountered: