0
点赞
收藏
分享

微信扫一扫

聊聊Mybatis的binding模块之MapperMethod

@[TOC]

聊聊Mybatis的binding模块之MapperMethod

通过MapperProxy来调用MapperMethod的execute()方法,

构造方法

先看一下MapperMethod的构造方法:

public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {
    this.command = new SqlCommand(config, mapperInterface, method);
    this.method = new MethodSignature(config, mapperInterface, method);
  }

SqlCommand

SqlCommand是用来记录sql语句的唯一标识和sql语句的类型:UNKNOWN, INSERT, UPDATE, DELETE, SELECT, FLUSH

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);
        }
      }
    }
  1. 获取Mapper接口对应的方法名
  2. 获取Mapper接口的类型
  3. 调用resolveMappedStatement()方法返回MappedStatement,MappedStatement是xml文件中的sql解析得到的对象,id是接口名+方法名
  4. 设置name 和type

而MapperMethod保存的是方法的一些信息

MapperMethod的执行方法

看一下MapperMethod的execute()方法:

public Object execute(SqlSession sqlSession, Object[] args) {
    Object result;
    switch (command.getType()) {
      case INSERT: {
        Object param = method.convertArgsToSqlCommandParam(args);
        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:
        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 if (method.returnsCursor()) {
          result = executeForCursor(sqlSession, args);
        } else {
          Object param = method.convertArgsToSqlCommandParam(args);
          result = sqlSession.selectOne(command.getName(), param);
          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;
  }
  1. 根据sql语句的类型进行调用,对应INSERT、UPDATE、DELETE,逻辑都差不多,都是调用convertArgsToSqlCommandParam()方法处理参数,然后调用sqlSession来进行执行sql最后返回的结果通过rowCountResult来进行处理
  2. 对于SELECT语句,根据方法的返回类型选择不同的execute方法执行,最后都是调用SqlSession中的方法

总结

至此,mybatis的binding模块差不多分析完了,大体流程就是MapperRegistry根据不同的Mapper接口获取MapperProxyFactory的实例,然后调用newInstance()方法,利用MapperProxy代理类获取Mapper接口的动态代理对象,最终调用MapperMethod的execute()传入参数和封装的sql对象信息,执行sql语句

举报

相关推荐

0 条评论