mybatis-3
mybatis-3 copied to clipboard
Mybatis-3.5.10 ParamNameResolver bug
org.apache.ibatis.reflection.ParamNameResolver#getNamedParams
public Object getNamedParams(Object[] args) {
final int paramCount = names.size();
if (args == null || paramCount == 0) {
return null;
} else if (!hasParamAnnotation && paramCount == 1) {
Object value = args[names.firstKey()];
return wrapToMapIfCollection(value, useActualParamName ? names.get(0) : null); //--------$1
} else {
......
}
}
code line $1 'names.get(0)' should be replaced by 'names.get(names.firstKey())' . because when 'paramCount == 1' names the only key is not necessarily 0,for example:
// when useActualParamName==true
public interface UserMapper {
List<User> selectByStartingWithName(RowBounds rb, List<String> nameList);
}
<select id="selectByStartingWithName" resultMap="baseMap">
select * from users where
<foreach collection="nameList" separator="or" item="item" open="(" close=")">
`name` LIKE concat('%',#{item},'%')
</foreach>
</select>
This test case 'names.size()==1' names contains key:1 --> value: "nameList"
ParamNameResolver构造方法中解析方法参数名时,就已经判断了是否为特殊参数(RowBounds || ResultHandler),特殊参数不会放到names集合中.
ParamNameResolver构造方法中解析方法参数名时,就已经判断了是否为特殊参数(RowBounds || ResultHandler),特殊参数不会放到names集合中.
是这样的逻辑,但是我上面的例子中names中存的key - value 是 1 -> "nameList" , 在上面源码$1行里 names.get(0)是获取不到
pom文件maven-compiler-plugin -> configuration标签中,加上这个 <compilerArgument>-parameters</compilerArgument>
https://github.com/mybatis/mybatis-3/releases/tag/mybatis-3.4.1
Hello @liuzongliang0202 @619123555 ,
I don't fully understand the Chinese discussion, but the reported issue should be fixed in the latest 3.5.12-SNAPSHOT. Thank you for your contributions!