上文Mybatis之Mapper接口如何执行SQL中了解到,Mapper通过动态代理的方式执行SQL,但是并没有详细的介绍方法是如何做映射的,方法包括:方法名,返回值,参数等;这些都是如何同xxMapper.xml进行关联的。
方法名映射
上文中提到缓存MapperMethod的目的是因为需要实例化SqlCommand和MethodSignature两个类,而这两个类实例化需要花费一些时间;而方法名的映射就在实例化SqlCommand的时候,具体可以看构造方法:
private final String name;
private final SqlCommandType type;
public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
final String methodName = method.getName();
final Class<?> declaringClass = method.getDeclaringClass();
MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass,
configuration);
if (ms == null) {
if (method.getAnnotation(Flush.class) != null) {
name = null;
type = SqlCommandType.FLUSH;
} else {
throw new BindingException("Invalid bound statement (not found): "
+ mapperInterface.getName() + "." + methodName);
}
} else {
name = ms.getId();
type = ms.getSqlCommandType();
if (type == SqlCommandType.UNKNOWN) {
throw new BindingException("Unknown execution method for: " + name);
}
}
}
复制代码首先获取了方法的名称和定义此方法的类,一般此类都是xxxMapper类;注此处的declaringClass类和mapperInterface是有区别的,主要是因为此方法可以是父类里面的方法,而mapperInterface是子类;所以如果定义了父xxxMapper,同样也能进行映射,所以可以看相关代码:
private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName,
Class<?> declaringClass, Configuration configuration) {
String statementId = mapperInterface.getName() + “.” + methodName;
if (configuration.hasStatement(statementId)) {
return configuration.getMappedStatement(statementId);
} else if (mapperInterface.equals(declaringClass)) {
return null;
}
for (Class<?> superInterface : mapperInterface.getInterfaces()) {
if (declaringClass.isAssignableFr