mybatis-3 icon indicating copy to clipboard operation
mybatis-3 copied to clipboard

Mybatis-3.5.10 ParamNameResolver bug

Open liuzongliang0202 opened this issue 2 years ago • 3 comments

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"

liuzongliang0202 avatar Sep 13 '22 06:09 liuzongliang0202

ParamNameResolver构造方法中解析方法参数名时,就已经判断了是否为特殊参数(RowBounds || ResultHandler),特殊参数不会放到names集合中.

619123555 avatar Sep 21 '22 06:09 619123555

ParamNameResolver构造方法中解析方法参数名时,就已经判断了是否为特殊参数(RowBounds || ResultHandler),特殊参数不会放到names集合中.

是这样的逻辑,但是我上面的例子中names中存的key - value 是 1 -> "nameList" , 在上面源码$1行里 names.get(0)是获取不到

liuzongliang0202 avatar Sep 21 '22 06:09 liuzongliang0202

pom文件maven-compiler-plugin -> configuration标签中,加上这个 <compilerArgument>-parameters</compilerArgument>

https://github.com/mybatis/mybatis-3/releases/tag/mybatis-3.4.1

619123555 avatar Sep 22 '22 02:09 619123555

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!

harawata avatar Sep 24 '22 02:09 harawata