目录
MapperRegistry和MapperProxyFactory
MapperProxy
MapperMethod
SqlCommand
MethodSignature
execute()方法
binding模块主要为了解决一个历史遗留问题,原先查询一个VO对象时需要调用 SqlSession.queryForObject(“selectXXVOById”, primaryKey)方法,执行指定的sql语句,第一个参数selectXXVOById指定了执行的sql语句id,如果我们不小心写错了参数,Mybatis是无法在初始化时发现这个错误的,只会在实际调用queryForObject(“selectXXVOById”, primaryKey)方法时才会抛出异常,就像泛型出来之前,很多类型转换不会在编译期发现错误一样。而binding模块就像 Java的泛型机制 一样,将程序的错误提前暴露出来,为开发人员省去不少排查问题的精力。
Binding模块如何将Mapper接口与映射配置信息相关联,binding模块的解决方案是,定义一个Mapper接口,在接口中定义sql语句对应的方法名Id及参数,这些方法在Mybatis的初始化过程中,会与该Mapper接口对应的映射配置文件中的sql语句相关联,如果存在无法关联的sql语句,Mybatis就会抛出异常,帮助我们及时发现问题。示例代码如下:
public interface HeroMapper {// 映射文件中会存在一个
MapperRegistry是Mapper接口及其对应的代理对象工厂的注册中心。Configuration是Mybatis中全局性的配置对象,根据Mybatis的核心配置文件mybatis-config.xml解析而成。Configuration通过 mapperRegistry属性持有该对象。Mybatis在初始化过程中会读取映射配置文件和Mapper接口中的注解信息,并调用MapperRegistry的addMappers()方法 填充knownMappers集合,在需要执行某sql语句时,会先调用getMapper()方法获取实现了Mapper接口的动态代理对象
public class MapperRegistry {// Mybatis 全局唯一的配置对象,包含了几乎所有配置信息private final Configuration config;// key:Mapper接口,value:MapperProxyFactory 为 Mapper接口 创建代理对象的工厂private final Map, MapperProxyFactory>> knownMappers = new HashMap<>();// 下面的两个重载方法 通过扫描指定的包目录,获取所有的 Mapper接口public void addMappers(String packageName) {addMappers(packageName, Object.class);}public void addMappers(String packageName, Class> superType) {ResolverUtil> resolverUtil = new ResolverUtil<>();resolverUtil.find(new ResolverUtil.IsA(superType), packageName);Set>> mapperSet = resolverUtil.getClasses();for (Class> mapperClass : mapperSet) {addMapper(mapperClass);}}public void addMapper(Class type) {// 该 type 是不是接口if (type.isInterface()) {// 是否已经加载过if (hasMapper(type)) {throw new BindingException("Type " + type + " is already known to the MapperRegistry.");}boolean loadCompleted = false;try {// 将 Mapper接口 的 Class对象 和 对应的 MapperProxyFactory对象 添加到 knownMappers集合knownMappers.put(type, new MapperProxyFactory<>(type));// XML 解析和注解的处理MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);parser.parse();loadCompleted = true;} finally {if (!loadCompleted) {knownMappers.remove(type);}}}}@SuppressWarnings("unchecked")public T getMapper(Class type, SqlSession sqlSession) {// 获取 type 对应的 MapperProxyFactory对象final MapperProxyFactory mapperProxyFactory = (MapperProxyFactory) knownMappers.get(type);if (mapperProxyFactory == null) {throw new BindingException("Type " + type + " is not known to the MapperRegistry.");}try {// 根据 sqlSession 创建 type接口 的代理对象return mapperProxyFactory.newInstance(sqlSession);} catch (Exception e) {throw new BindingException("Error getting mapper instance. Cause: " + e, e);}}// 获取所有的 MapperProxyFactorypublic Collection> getMappers() {return Collections.unmodifiableCollection(knownMappers.keySet());}// 初始化的时候会持有 Configuration对象public MapperRegistry(Configuration config) {this.config = config;}// 是否存在指定的 MapperProxyFactorypublic boolean hasMapper(Class type) {return knownMappers.containsKey(type);}
}
MapperProxyFactory主要负责创建代理对象。
public class MapperProxyFactory {// 要创建的动态代理对象 所实现的接口private final Class mapperInterface;// 缓存 mapperInterface接口 中 Method对象 和其对应的 MapperMethod对象private final Map methodCache = new ConcurrentHashMap<>();// 初始化时为 mapperInterface 注入值public MapperProxyFactory(Class mapperInterface) {this.mapperInterface = mapperInterface;}public Class getMapperInterface() {return mapperInterface;}public Map getMethodCache() {return methodCache;}public T newInstance(SqlSession sqlSession) {// 每次都会创建一个新的 MapperProxy对象final MapperProxy mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);return newInstance(mapperProxy);}/*** 非常眼熟的 JDK动态代理 代码,创建了实现 mapperInterface接口 的代理对象* 根据国际惯例,mapperProxy对应的类 肯定实现了 InvocationHandler接口,* 为 mapperInterface接口方法的调用 织入统一处理逻辑*/protected T newInstance(MapperProxy mapperProxy) {return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);}
}
MapperProxy实现了InvocationHandler接口,为Mapper接口的方法调用织入了统一处理。
public class MapperProxy implements InvocationHandler, Serializable {private static final long serialVersionUID = -6424540398559729838L;// 记录关联的 sqlSession对象private final SqlSession sqlSession;// 对应的 Mapper接口 的 Class对象private final Class mapperInterface;// 用于缓存 MapperMethod对象,key:Mapper接口 中方法对应的 Method对象,// value:MapperMethod对象(该对象会完成参数转换 及 sql语句 的执行功能)private final Map methodCache;public MapperProxy(SqlSession sqlSession, Class mapperInterface, Map methodCache) {this.sqlSession = sqlSession;this.mapperInterface = mapperInterface;this.methodCache = methodCache;}// 为被代理对象的方法 织入统一处理public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {try {// 如果目标方法继承自 Object,则直接调用目标方法if (Object.class.equals(method.getDeclaringClass())) {return method.invoke(this, args);} else if (method.isDefault()) {return invokeDefaultMethod(proxy, method, args);}} catch (Throwable t) {throw ExceptionUtil.unwrapThrowable(t);}// 从缓存中获取 mapperMethod对象,如果没有就创建新的final MapperMethod mapperMethod = cachedMapperMethod(method);// 执行 sql语句,返回结果集return mapperMethod.execute(sqlSession, args);}// 主要负责维护 methodCache 缓存private MapperMethod cachedMapperMethod(Method method) {// 这里用到了 Java8 的新特性,computeIfAbsent() 是 Java8 新增的方法,Lambda表达式 也是 Java8中 最重要的新特性之一// computeIfAbsent()方法 表示 当前map中,若 key 对应的 value 为空,则执行传入的 Lambda表达式,将 key 和表达式的 value// 存入 当前map,并返回 value值// 在这段代码中的意思是:若 methodCache 中没有 method 对应的 value,就执行右侧的 Lambda表达式,并将表达式的结果// 存入 methodCache 并返回return methodCache.computeIfAbsent(method, k -> new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()));}
}
MapperMethod中封装了Mapper接口中对应方法的信息,和对应sql语句的信息,是连接Mapper 接口及映射配置文件中定义的sql语句的桥梁。MapperMethod中持有两个非常重要的属性,这两个属性对应的类 都是MapperMethod中的静态内部类。另外,MapperMethod在被实例化时就对这两个属性进行了初始化。
public class MapperMethod {/** 下面这俩货都是内部类,而且还是 public static 的 */private final SqlCommand command;private final MethodSignature method;public MapperMethod(Class> mapperInterface, Method method, Configuration config) {this.command = new SqlCommand(config, mapperInterface, method);this.method = new MethodSignature(config, mapperInterface, method);}
}
MapperMethod中的核心方法execute()就主要用到了这两个类,所以我们先看一下SqlCommand 和MethodSignature的源码
public static class SqlCommand {// sql语句的idprivate final String name;// sql语句的类型,SqlCommandType 是枚举类型,持有常用的 增、删、改、查等操作类型private final SqlCommandType type;public SqlCommand(Configuration configuration, Class> mapperInterface, Method method) {// 方法名final String methodName = method.getName();// 该方法对应的类的 Class对象final Class> declaringClass = method.getDeclaringClass();// MappedStatement 封装了 sql语句 相关的信息,在 Mybatis初始化 时创建MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass, configuration);if (ms == null) {// 处理 Flush注解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 和 typename = ms.getId();type = ms.getSqlCommandType();if (type == SqlCommandType.UNKNOWN) {throw new BindingException("Unknown execution method for: " + name);}}}private MappedStatement resolveMappedStatement(Class> mapperInterface, String methodName,Class> declaringClass, Configuration configuration) {// sql语句 的名称默认是由 Mapper接口方法 的 包名.类名.方法名String statementId = mapperInterface.getName() + "." + methodName;// 检测是否有该名称的 sql语句if (configuration.hasStatement(statementId)) {// 从 configuration 的 mappedStatements容器 中获取 statementId 对应的 MappedStatement对象return configuration.getMappedStatement(statementId);// 如果此方法不是 mapperInterface接口 定义的,则返回空} else if (mapperInterface.equals(declaringClass)) {return null;}// 对 mapperInterface 的父接口 进行递归处理for (Class> superInterface : mapperInterface.getInterfaces()) {if (declaringClass.isAssignableFrom(superInterface)) {MappedStatement ms = resolveMappedStatement(superInterface, methodName,declaringClass, configuration);if (ms != null) {return ms;}}}return null;}public String getName() {return name;}public SqlCommandType getType() {return type;}}
public static class MethodSignature {// 返回值类型是否为 集合 或 数组private final boolean returnsMany;// 返回值类型是否为 Map类型private final boolean returnsMap;// 返回值类型是否为 voidprivate final boolean returnsVoid;// 返回值类型是否为 Cursorprivate final boolean returnsCursor;// 返回值类型是否为 Optionalprivate final boolean returnsOptional;// 返回值类型的 Class对象private final Class> returnType;// 如果返回值类型为 Map,则用该字段记录了作为 key 的列名private final String mapKey;// 标记该方法参数列表中 ResultHandler类型参数 的位置private final Integer resultHandlerIndex;// 标记该方法参数列表中 RowBounds类型参数 的位置private final Integer rowBoundsIndex;/*** 顾名思义,这是一个处理 Mapper接口 中 方法参数列表的解析器,它使用了一个 SortedMap* 类型的容器,记录了参数在参数列表中的位置索引 与 参数名之间的对应关系,key参数 在参数列表中的索引位置,* value参数名(参数名可用@Param注解指定,默认使用参数索引作为其名称)*/private final ParamNameResolver paramNameResolver;/*** MethodSignature 的构造方法会解析对应的 method,并初始化上述字段*/public MethodSignature(Configuration configuration, Class> mapperInterface, Method method) {// 获取 method方法 的返回值类型Type resolvedReturnType = TypeParameterResolver.resolveReturnType(method, mapperInterface);if (resolvedReturnType instanceof Class>) {this.returnType = (Class>) resolvedReturnType;} else if (resolvedReturnType instanceof ParameterizedType) {this.returnType = (Class>) ((ParameterizedType) resolvedReturnType).getRawType();} else {this.returnType = method.getReturnType();}// 对 MethodSignature 持有的各属性 进行初始化this.returnsVoid = void.class.equals(this.returnType);this.returnsMany = configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray();this.returnsCursor = Cursor.class.equals(this.returnType);this.returnsOptional = Optional.class.equals(this.returnType);this.mapKey = getMapKey(method);this.returnsMap = this.mapKey != null;this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class);this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class);this.paramNameResolver = new ParamNameResolver(configuration, method);}/*** 查找指定类型的参数在参数列表中的位置,要查找的参数类型在参数列表中必须是唯一的* 如果参数列表中存在多个 要查找的参数类型,则会抛出异常*/private Integer getUniqueParamIndex(Method method, Class> paramType) {Integer index = null;final Class>[] argTypes = method.getParameterTypes();for (int i = 0; i < argTypes.length; i++) {if (paramType.isAssignableFrom(argTypes[i])) {if (index == null) {index = i;} else {throw new BindingException(method.getName() + " cannot have multiple " + paramType.getSimpleName() + " parameters");}}}return index;}}
execute()方法会根据sql语句的类型(CRUD)调用SqlSession对应的方法完成数据库操作,SqlSession是Mybatis的核心组件之一。
public class MapperMethod {public Object execute(SqlSession sqlSession, Object[] args) {Object result;// 根据 sql语句 的类型 调用 sqlSession 对应的方法switch (command.getType()) {case INSERT: {// 使用 ParamNameResolver 处理 args实参列表,将用户传入的实参与// 指定参数名称关联起来Object param = method.convertArgsToSqlCommandParam(args);// 获取返回结果,rowCountResult()方法 会根据 method属性 中的 returnType,// 对结果的类型进行转换result = rowCountResult(sqlSession.insert(command.getName(), param));break;}case UPDATE: {Object param = method.convertArgsToSqlCommandParam(args);result = rowCountResult(sqlSession.update(command.getName(), param));break;}case DELETE: {Object param = method.convertArgsToSqlCommandParam(args);result = rowCountResult(sqlSession.delete(command.getName(), param));break;}case SELECT:// 处理返回值为 void 且 ResultSet 通过 ResultHandler 处理的方法if (method.returnsVoid() && method.hasResultHandler()) {executeWithResultHandler(sqlSession, args);result = null;// 处理返回值为集合 或 数组的方法} else if (method.returnsMany()) {result = executeForMany(sqlSession, args);// 处理返回值为 Map 的方法} else if (method.returnsMap()) {result = executeForMap(sqlSession, args);// 处理返回值为 Cursor 的方法} else if (method.returnsCursor()) {result = executeForCursor(sqlSession, args);} else {// 处理返回值为单一对象的方法Object param = method.convertArgsToSqlCommandParam(args);result = sqlSession.selectOne(command.getName(), param);// 处理返回值为 Optional 的方法if (method.returnsOptional()&& (result == null || !method.getReturnType().equals(result.getClass()))) {result = Optional.ofNullable(result);}}break;case FLUSH:result = sqlSession.flushStatements();break;default:throw new BindingException("Unknown execution method for: " + command.getName());}if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {throw new BindingException("Mapper method '" + command.getName()+ " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");}return result;}/*** 当执行 insert、update、delete 类型的 sql语句 时,其执行结果都要经过本方法处理*/private Object rowCountResult(int rowCount) {final Object result;// 方法的返回值为 void 时if (method.returnsVoid()) {result = null;// 方法的返回值为 Integer 时} else if (Integer.class.equals(method.getReturnType()) || Integer.TYPE.equals(method.getReturnType())) {result = rowCount;// 方法的返回值为 Long 时} else if (Long.class.equals(method.getReturnType()) || Long.TYPE.equals(method.getReturnType())) {result = (long)rowCount;// 方法的返回值为 Boolean 时} else if (Boolean.class.equals(method.getReturnType()) || Boolean.TYPE.equals(method.getReturnType())) {result = rowCount > 0;} else {throw new BindingException("Mapper method '" + command.getName() + "' has an unsupported return type: " + method.getReturnType());}return result;}/*** 如果 Mapper接口 中定义的方法准备使用 ResultHandler 处理查询结果集,则通过此方法处理*/private void executeWithResultHandler(SqlSession sqlSession, Object[] args) {// 获取 sql语句 对应的 MappedStatement对象,该对象中记录了 sql语句 相关信息MappedStatement ms = sqlSession.getConfiguration().getMappedStatement(command.getName());// 当使用 ResultHandler 处理结果集时,必须指定 ResultMap 或 ResultTypeif (!StatementType.CALLABLE.equals(ms.getStatementType())&& void.class.equals(ms.getResultMaps().get(0).getType())) {throw new BindingException("method " + command.getName()+ " needs either a @ResultMap annotation, a @ResultType annotation,"+ " or a resultType attribute in XML so a ResultHandler can be used as a parameter.");}// 转换实参列表Object param = method.convertArgsToSqlCommandParam(args);// 如果实参列表中有 RowBounds类型参数if (method.hasRowBounds()) {// 从 args参数列表 中获取 RowBounds对象RowBounds rowBounds = method.extractRowBounds(args);// 执行查询,并用指定的 ResultHandler 处理结果对象sqlSession.select(command.getName(), param, rowBounds, method.extractResultHandler(args));} else {sqlSession.select(command.getName(), param, method.extractResultHandler(args));}}/*** 如果 Mapper接口 中对应方法的返回值为集合(Collection接口实现类) 或 数组,* 则调用本方法将结果集处理成 相应的集合或数组*/private Object executeForMany(SqlSession sqlSession, Object[] args) {List result;// 参数列表转换Object param = method.convertArgsToSqlCommandParam(args);// 参数列表中是否有 RowBounds类型的参数if (method.hasRowBounds()) {RowBounds rowBounds = method.extractRowBounds(args);// 这里使用了 selectList()方法 进行查询,所以返回的结果集就是 List类型的result = sqlSession.selectList(command.getName(), param, rowBounds);} else {result = sqlSession.selectList(command.getName(), param);}// 将结果集转换为数组或 Collection集合if (!method.getReturnType().isAssignableFrom(result.getClass())) {if (method.getReturnType().isArray()) {return convertToArray(result);} else {return convertToDeclaredCollection(sqlSession.getConfiguration(), result);}}return result;}/*** 将结果集转换成 Collection集合*/private Object convertToDeclaredCollection(Configuration config, List list) {// 使用前面介绍的 ObjectFactory,通过反射方式创建集合对象Object collection = config.getObjectFactory().create(method.getReturnType());MetaObject metaObject = config.newMetaObject(collection);// 实际上就是调用了 Collection 的 addAll()方法metaObject.addAll(list);return collection;}/*** 本方法和上面的 convertToDeclaredCollection()功能 类似,主要负责将结果对象转换成数组*/@SuppressWarnings("unchecked")private Object convertToArray(List list) {// 获取数组中元素的 类型ClassClass> arrayComponentType = method.getReturnType().getComponentType();// 根据元素类型 和 元素数量 初始化数组Object array = Array.newInstance(arrayComponentType, list.size());// 将 List 转换成数组if (arrayComponentType.isPrimitive()) {for (int i = 0; i < list.size(); i++) {Array.set(array, i, list.get(i));}return array;} else {return list.toArray((E[])array);}}/*** 如果 Mapper接口 中对应方法的返回值为类型为 Map,则调用此方法执行 sql语句*/private Map executeForMap(SqlSession sqlSession, Object[] args) {Map result;// 转换实参列表Object param = method.convertArgsToSqlCommandParam(args);if (method.hasRowBounds()) {RowBounds rowBounds = method.extractRowBounds(args);// 注意这里调用的是 SqlSession 的 selectMap()方法,返回的是一个 Map类型结果集result = sqlSession.selectMap(command.getName(), param, method.getMapKey(), rowBounds);} else {result = sqlSession.selectMap(command.getName(), param, method.getMapKey());}return result;}/*** 本方法与上面的 executeForMap()方法 类似,只不过 sqlSession 调用的是 selectCursor()*/private Cursor executeForCursor(SqlSession sqlSession, Object[] args) {Cursor result;Object param = method.convertArgsToSqlCommandParam(args);if (method.hasRowBounds()) {RowBounds rowBounds = method.extractRowBounds(args);result = sqlSession.selectCursor(command.getName(), param, rowBounds);} else {result = sqlSession.selectCursor(command.getName(), param);}return result;}
}