Mybatis3源码分析(21)-Mapper实现-动态代理

当定义好一个Mapper接口(UserDao)里,我们并不需要去实现这个类,但sqlSession.getMapper()最终会返回一个实现该接口的对象。这个对象是Mybatis利用jdk的动态代理实现的。这里将介绍这个代理对象的生成过程及其方法的实现过程。

Mapper代码对象的生成过程

DefaultSqlSession.getMapp()方法最终会调用MapperRegistry.getMapper()方法
[java]  view plain  copy
  1. public <T> T getMapper(Class<T> type, SqlSession sqlSession) {  
  2.    //这个MapperProxyFactory是调用addMapper方法时加到knownMappers中的,  
  3.    final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);  
  4.    if (mapperProxyFactory == null)  
  5.      //说明这个Mapper接口没有注册  
  6.      throw new BindingException("Type " + type + " is not known to the MapperRegistry.");  
  7.    try {  
  8.       //生成一个MapperProxy对象  
  9.      return mapperProxyFactory.newInstance(sqlSession);  
  10.    } catch (Exception e) {  
  11.      throw new BindingException("Error getting mapper instance. Cause: " + e, e);  
  12.    }  
  13.  }  
下面是MapperProxyFactory的newInstance方法
[java]  view plain  copy
  1. public T newInstance(SqlSession sqlSession) {  
  2.     //创建一个Mapperxy对象,这个方法实现了JDK动态代理中的InvocationHandler接口  
  3.     final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);  
  4.     return newInstance(mapperProxy);  
  5.   }  
  6.   
  7. protected T newInstance(MapperProxy<T> mapperProxy) {  
  8.     //mapperInterface,说明Mapper接口被代理了,这样子返回的对象就是Mapper接口的子类,方法被调用时会被mapperProxy拦截,也就是执行mapperProxy.invoke()方法  
  9.     return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);  
  10.   }  

MapperProxy

详细分析一下MapperProxy类
[java]  view plain  copy
  1. public class MapperProxy<T> implements InvocationHandler, Serializable {  
  2.   
  3.   private static final long serialVersionUID = -6424540398559729838L;  
  4.   private final SqlSession sqlSession;  
  5.   
  6.   //Mapper接口  
  7.   private final Class<T> mapperInterface;  
  8.   
  9.   //Mapper接口中的每个方法都会生成一个MapperMethod对象, methodCache维护着他们的对应关系  
  10.   //这个methodCache是在MapperProxyFactory中持有的,MapperProxyFactory又是在Configuration中持有的  
  11.   //所以每个Mapper接口类对应的MapperProxyFactory和methodCache在整个应用中是共享的,一般只会有一个实例  
  12.   private final Map<Method, MapperMethod> methodCache;  
  13.   
  14.   public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {  
  15.     this.sqlSession = sqlSession;  
  16.   
  17.       
  18.     this.mapperInterface = mapperInterface;  
  19.     this.methodCache = methodCache;  
  20.   }  
  21.   
  22.   
  23.   //这里会拦截Mapper接口(UserDao)的所有方法  
  24.   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {  
  25.     //如果是Object中定义的方法,直接执行。如toString(),hashCode()等待。  
  26.     if (Object.class.equals(method.getDeclaringClass())) {  
  27.       try {  
  28.         return method.invoke(this, args);  
  29.       } catch (Throwable t) {  
  30.         throw ExceptionUtil.unwrapThrowable(t);  
  31.       }  
  32.     }  
  33.     //其他Mapper接口定义的方法交由mapperMethod来执行。  
  34.     final MapperMethod mapperMethod = cachedMapperMethod(method);  
  35.     return mapperMethod.execute(sqlSession, args);  
  36.   }  
  37.   
  38.   private MapperMethod cachedMapperMethod(Method method) {  
  39.     MapperMethod mapperMethod = methodCache.get(method);  
  40.     if (mapperMethod == null) {  
  41.       mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());  
  42.       methodCache.put(method, mapperMethod);  
  43.     }  
  44.     return mapperMethod;  
  45.   }  
  46.   
  47. }  

MapperMethod

[java]  view plain  copy
  1. //所有Mapper接口中方法被调用里,都会执行这个方法.这里实际上是调用SqlSession中的相关方法,  
  2. public Object execute(SqlSession sqlSession, Object[] args) {  
  3.   Object result;  
  4.   //判断这个方法被注解里的Sql类型  
  5.   if (SqlCommandType.INSERT == command.getType()) {  
  6.     //执行insert  
  7.     Object param = method.convertArgsToSqlCommandParam(args);  
  8.     result = rowCountResult(sqlSession.insert(command.getName(), param));  
  9.   } else if (SqlCommandType.UPDATE == command.getType()) {  
  10.     //执行update  
  11.     Object param = method.convertArgsToSqlCommandParam(args);  
  12.     result = rowCountResult(sqlSession.update(command.getName(), param));  
  13.   } else if (SqlCommandType.DELETE == command.getType()) {  
  14.     /delete  
  15.     Object param = method.convertArgsToSqlCommandParam(args);  
  16.     result = rowCountResult(sqlSession.delete(command.getName(), param));  
  17.   } else if (SqlCommandType.SELECT == command.getType()) {  
  18.     //select ,查询  
  19.     if (method.returnsVoid() && method.hasResultHandler()) {  
  20.       //没有返回值,并且有ResultHandler的情况  
  21.       executeWithResultHandler(sqlSession, args);  
  22.       result = null;  
  23.     } else if (method.returnsMany()) {  
  24.       //返回一个List  
  25.       result = executeForMany(sqlSession, args);  
  26.     } else if (method.returnsMap()) {  
  27.       //返回一个Map  
  28.       result = executeForMap(sqlSession, args);  
  29.     } else {  
  30.       //返回一个对象  
  31.       Object param = method.convertArgsToSqlCommandParam(args);  
  32.       result = sqlSession.selectOne(command.getName(), param);  
  33.     }  
  34.   } else {  
  35.     throw new BindingException("Unknown execution method for: " + command.getName());  
  36.   }  
  37.   if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {  
  38.     throw new BindingException("Mapper method '" + command.getName()   
  39.         + " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");  
  40.   }  
  41.   return result;  
  42. }  

小结

  1. 在Mybatis提供的编程接口中,开发人员只需要定义好Mapper接口(如:UserDao),开发人员无需去实现。Mybatis会利用JDK的动态代理实现 Mapper接口。
  2. 在Mybatis中,每个Mapper接口都会对应一个MapperProxyFactory对象实例,这个对应关系在Configuration.mapperRegistry.knownMappers中。
  3. 当getMapper()方法被调用时,Mybatis会找到相对应的MapperProxyFactory对象实例,利用这个工厂来创建一个jdk动态代理对象,是这个Mapper接口的实现类,当Mapper定义的方法被调用时,会调用MapperProxy来处理。
  4. MapperProxy会根据方法找到对应的MapperMethod对象来实现这次调用。
  5. MapperMethod对应会读取方法中的注解,从Configuration中找到相对应的MappedStatement对象,再执行。
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值