MyBatis版本3.5.3
package org.apache.ibatis.reflection;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.binding.MapperMethod.ParamMap;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
public class ParamNameResolver {
// 通用参数名称前缀
private static final String GENERIC_NAME_PREFIX = "param";
// key是索引,value是参数的名称。
// 如果指定了名称,则从@Param中获得。如果未指定@Param,则使用参数索引。
// 注意,当方法具有特殊参数(如RowBounds或ResultHandler)时,该索引可能与实际索引不同。
// Method(@Param("M") int a, @Param("N") int b)转化为map为{{0, "M"}, {1, "N"}}
// Method(int a, int b)转化为map为{{0, "0"}, {1, "1"}}
// Method(int a, RowBounds rb, int b)转化为map为{{0, "0"}, {2, "1"}}
// 有序映射 存储参数名称
private final SortedMap<Integer, String> names;
// 是否使用@Param注解标志
private boolean hasParamAnnotation;
// 构造方法
public ParamNameResolver(Configuration config, Method method) {
final Class<?>[] paramTypes = method.getParameterTypes();
final Annotation[][] paramAnnotations = method.getParameterAnnotations();
final SortedMap<Integer, String> map = new TreeMap<>();
int paramCount = paramAnnotations.length;
// get names from @Param annotations
for (int paramIndex = 0; paramIndex < paramCount; paramIndex++) {
if (isSpecialParameter(paramTypes[paramIndex])) {
// 跳过特殊参数
continue;
}
String name = null;
for (Annotation annotation : paramAnnotations[paramIndex]) {
if (annotation instanceof Param) {
hasParamAnnotation = true;
name = ((Param) annotation).value();
break;
}
}
if (name == null) {
// 没有指定@Param
if (config.isUseActualParamName()) {
name = getActualParamName(method, paramIndex);
}
if (name == null) {
// 使用参数索引作为名称("0","1",…)
name = String.valueOf(map.size());
}
}
map.put(paramIndex, name);
}
names = Collections.unmodifiableSortedMap(map);
}
// 获取实际参数名称
private String getActualParamName(Method method, int paramIndex) {
return ParamNameUtil.getParamNames(method).get(paramIndex);
}
// 是不是特殊参数()
private static boolean isSpecialParameter(Class<?> clazz) {
return RowBounds.class.isAssignableFrom(clazz) || ResultHandler.class.isAssignableFrom(clazz);
}
/**
* 返回SQL引用的参数名。
*/
public String[] getNames() {
return names.values().toArray(new String[0]);
}
/**
* 返回一个没有名称的非特殊参数。
* 使用命名规则命名多个参数
* 除了默认名称之外,该方法还添加通用名称(paraml、param2、...).
*/
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()];
} else {
final Map<String, Object> param = new ParamMap<>();
int i = 0;
for (Map.Entry<Integer, String> entry : names.entrySet()) {
param.put(entry.getValue(), args[entry.getKey()]);
// 添加通用参数名称(参数、参数 2...)
final String genericParamName = GENERIC_NAME_PREFIX + String.valueOf(i + 1);
// /确保没有覆盖以@Param命名的参数
if (!names.containsValue(genericParamName)) {
param.put(genericParamName, args[entry.getKey()]);
}
i++;
}
return param;
}
}
}
- MapperProxy类的invoke方法调用了MapperMethod类的execute方法
- execute方法中调用了MethodSignature类的convertArgsToSqlCommandParam方法
- MethodSignature类的convertArgsToSqlCommandParam方法调用了paramNameResolver.getNamedParams方法
由getNamedParams方法可以看出如果只有一个参数,无论你是否使用@Param注解,Java版本是否支持反射获取实际参数名称,Mybatis版本是否在3.4.1以上,Java运行时是否添加-parameters参数或者方法参数名称与Mapper.xml文件中的参数名称不一致都是可以的.
在Java8以前的版本中,我们利用反射获取到的参数名字都是arg0,arg1,arg2.....之类的形式.
而Java8之后,我们可以通过指定“-parameters”参数,来实现将方法的参数名写入class文件,并在运行时通过反射机制获取对应的参数名。该参数默认是关闭的
IDEA增加-parameters参数
给Maven指定编译时的参数:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf8</encoding>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
Mybatis版本3.4.1新增一个配置选项为useActualParamName,默认是开启的.
官网地址: https://mybatis.org/mybatis-3/zh/configuration.html
Java8+Mybatis3.4.1+-parameters=Mapper方法参数不用加@Param注解
使用Mybatis-plus需要注意其依赖的Mybatis版本.