当定义好一个Mapper接口(UserDao)里,我们并不需要去实现这个类,但sqlSession.getMapper()最终会返回一个实现该接口的对象。这个对象是Mybatis利用jdk的动态代理实现的。这里将介绍这个代理对象的生成过程及其方法的实现过程。
Mapper代码对象的生成过程
DefaultSqlSession.getMapp()方法最终会调用MapperRegistry.getMapper()方法
- public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
-
- final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
- if (mapperProxyFactory == null)
-
- throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
- try {
-
- return mapperProxyFactory.newInstance(sqlSession);
- } catch (Exception e) {
- throw new BindingException("Error getting mapper instance. Cause: " + e, e);
- }
- }
下面是MapperProxyFactory的newInstance方法
- public T newInstance(SqlSession sqlSession) {
-
- final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
- return newInstance(mapperProxy);
- }
-
- protected T newInstance(MapperProxy<T> mapperProxy) {
-
- return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
- }
MapperProxy
详细分析一下MapperProxy类
- public class MapperProxy<T> implements InvocationHandler, Serializable {
-
- private static final long serialVersionUID = -6424540398559729838L;
- private final SqlSession sqlSession;
-
-
- private final Class<T> mapperInterface;
-
-
-
-
- private final Map<Method, MapperMethod> methodCache;
-
- public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
- this.sqlSession = sqlSession;
-
-
- this.mapperInterface = mapperInterface;
- this.methodCache = methodCache;
- }
-
-
-
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
-
- if (Object.class.equals(method.getDeclaringClass())) {
- try {
- return method.invoke(this, args);
- } catch (Throwable t) {
- throw ExceptionUtil.unwrapThrowable(t);
- }
- }
-
- final MapperMethod mapperMethod = cachedMapperMethod(method);
- return mapperMethod.execute(sqlSession, args);
- }
-
- private MapperMethod cachedMapperMethod(Method method) {
- MapperMethod mapperMethod = methodCache.get(method);
- if (mapperMethod == null) {
- mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
- methodCache.put(method, mapperMethod);
- }
- return mapperMethod;
- }
-
- }
MapperMethod
-
- public Object execute(SqlSession sqlSession, Object[] args) {
- Object result;
-
- if (SqlCommandType.INSERT == command.getType()) {
-
- Object param = method.convertArgsToSqlCommandParam(args);
- result = rowCountResult(sqlSession.insert(command.getName(), param));
- } else if (SqlCommandType.UPDATE == command.getType()) {
-
- Object param = method.convertArgsToSqlCommandParam(args);
- result = rowCountResult(sqlSession.update(command.getName(), param));
- } else if (SqlCommandType.DELETE == command.getType()) {
- /delete
- Object param = method.convertArgsToSqlCommandParam(args);
- result = rowCountResult(sqlSession.delete(command.getName(), param));
- } else if (SqlCommandType.SELECT == command.getType()) {
-
- if (method.returnsVoid() && method.hasResultHandler()) {
-
- executeWithResultHandler(sqlSession, args);
- result = null;
- } else if (method.returnsMany()) {
-
- result = executeForMany(sqlSession, args);
- } else if (method.returnsMap()) {
-
- result = executeForMap(sqlSession, args);
- } else {
-
- Object param = method.convertArgsToSqlCommandParam(args);
- result = sqlSession.selectOne(command.getName(), param);
- }
- } else {
- 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;
- }
小结
- 在Mybatis提供的编程接口中,开发人员只需要定义好Mapper接口(如:UserDao),开发人员无需去实现。Mybatis会利用JDK的动态代理实现 Mapper接口。
- 在Mybatis中,每个Mapper接口都会对应一个MapperProxyFactory对象实例,这个对应关系在Configuration.mapperRegistry.knownMappers中。
- 当getMapper()方法被调用时,Mybatis会找到相对应的MapperProxyFactory对象实例,利用这个工厂来创建一个jdk动态代理对象,是这个Mapper接口的实现类,当Mapper定义的方法被调用时,会调用MapperProxy来处理。
- MapperProxy会根据方法找到对应的MapperMethod对象来实现这次调用。
- MapperMethod对应会读取方法中的注解,从Configuration中找到相对应的MappedStatement对象,再执行。