mybatis随笔四之MapperProxy

本文深入解析MyBatis框架中Mapper接口的工作原理,详细分析了如何通过代理模式实现接口方法调用,并最终转化为SQL执行的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在上一篇文章我们已经得到了mapper的代理对象,接下来我们对demoMapper.getDemo(1)这种语句进行分析。
由于返回的mapper是个代理对象,因此会进入invoke方法,接下来我们来看看MapperProxy的invoke方法。
@Override
  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);
  }
Object.class.equals(method.getDeclaringClass())的意思是如果定义方法的类是个具体类就使用具体类的实现,如果是接口则往下执行。
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;
  }
methodCache是个Map<Method, MapperMethod>对象,第一次取时为空会进入MapperMethod构造方法。
public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {
    this.command = new SqlCommand(config, mapperInterface, method);
    this.method = new MethodSignature(config, method);
  }
public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
      String statementName = mapperInterface.getName() + "." + method.getName();
      MappedStatement ms = null;
      if (configuration.hasStatement(statementName)) {
        ms = configuration.getMappedStatement(statementName);
      } else if (!mapperInterface.equals(method.getDeclaringClass())) { // issue #35
        String parentStatementName = method.getDeclaringClass().getName() + "." + method.getName();
        if (configuration.hasStatement(parentStatementName)) {
          ms = configuration.getMappedStatement(parentStatementName);
        }
      }
      if (ms == null) {
        if(method.getAnnotation(Flush.class) != null){
          name = null;
          type = SqlCommandType.FLUSH;
        } else {
          throw new BindingException("Invalid bound statement (not found): " + statementName);
        }
      } else {
        name = ms.getId();
        type = ms.getSqlCommandType();
        if (type == SqlCommandType.UNKNOWN) {
          throw new BindingException("Unknown execution method for: " + name);
        }
      }
    }
这里主要做了这几件事,根据方法名以及接口名的组合从configuration中取得对应的MappedStatement,然后从中取出name和type。

MethodSignature构造方法如下
public MethodSignature(Configuration configuration, Method method) {
      this.returnType = method.getReturnType();
      this.returnsVoid = void.class.equals(this.returnType);
      this.returnsMany = (configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray());
      this.mapKey = getMapKey(method);
      this.returnsMap = (this.mapKey != null);
      this.hasNamedParameters = hasNamedParams(method);
      this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class);
      this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class);
      this.params = Collections.unmodifiableSortedMap(getParams(method, this.hasNamedParameters));
    }
这里主要是标记下入参中的RowBounds、ResultHandler类型参数,以及对返回值进行些标记。
MethodSignature与SqlCommand初始化后MapperMethod也就构造完成,然后methodCache将method与mapperMethod关系保留。
接下来就是屌用mapperMethod的execute方法来执行。
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()) {
      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 if (SqlCommandType.FLUSH == command.getType()) {
        result = sqlSession.flushStatements();
    } 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;
  }
我们的mapper方法定义如下,返回的不是集合也不为空,因此进入convertArgsToSqlCommandParam方法。
convertArgsToSqlCommandParam对入参进行转换,如果没有入参返回null如果一个入参对象则直接返回,多个入参则封装成个map对象返回。
public Demo getDemo(long id);
现在进入到sqlSession的selectOne方法,使用ibatis的同学应该相当熟悉。
在selectOne内部调用了selectList方法,然后返回集合对象的第一个元素,如果集合对象大于1个则抛错。
@Override
  public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
    try {
      MappedStatement ms = configuration.getMappedStatement(statement);
      return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
    } catch (Exception e) {
      throw ExceptionFactory.wrapException("Error querying database.  Cause: " + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }
rowBounds是用来分页的,暂时不管该对象,默认的初始值如下
public static final int NO_ROW_OFFSET = 0;
public static final int NO_ROW_LIMIT = Integer.MAX_VALUE;
selectList也是根据statement从configuration中取得mappedStatement,然后交由executor来执行,sqlSessionFactory构建的时候默认使用的是simpleExecutor

到这里我们分析了mapper接口的方法最终是交到executor来执行。
 




### 回答1: Mybatis MapperProxy 的 invoke 方法是一个用来执行 SQL 语句的方法。它通过反射来调用接口中声明的方法,并将参数传递给 SQL 语句,然后执行该 SQL 语句并返回结果。 invoke 方法的作用是将接口方法的调用转化为对底层数据库的调用。 ### 回答2: MybatisMapperProxyMybatis框架中用于代理Mapper接口的类,它的invoke方法是用来处理Mapper接口的方法调用的。 MapperProxy的invoke方法会接收到代理对象的方法调用,并且包装成一个MapperMethod对象,然后通过SqlSession的selectOne、selectList、update、insertdelete等方法来执行SQL语句。 在invoke方法中,首先会判断方法是否为Object类的方法(如toString、hashCode等),如果是,则直接调用代理对象的相应方法。 接下来,会从configuration中获取到对应的MapperMethod对象,并且将方法调用相关的参数传递给MapperMethod对象的execute方法。 MapperMethod的execute方法会根据方法调用的类型(查询、更新、插入或删除),调用相应的SQL语句执行方法。 在执行SQL语句之前,还会对参数进行处理。对于查询方法,会根据方法参数中是否含有RowBoundsResultHandler参数,来决定是否使用分页查询结果处理器。 执行SQL语句后,会根据返回结果的类型,调用相应的处理方法。对于查询方法,会判断是否需要延迟加载结果,如果需要,则封装成代理对象返回。 最后,在invoke方法中,会根据MapperMethod的返回类型,将执行结果返回给调用者。 总的来说,MybatisMapperProxy的invoke方法主要是根据方法的调用类型,来获取对应的MapperMethod对象,并且将方法调用的参数传递给MapperMethod对象的execute方法,最后返回执行结果给调用者。 ### 回答3: MybatisMapperProxy是一个动态代理类,用于实现Mapper接口的方法调用。其核心方法是invoke方法。 invoke方法首先判断被调用的方法是否为Object类中的方法,如果是,则直接调用原始对象的方法。这是为了避免代理对象自身的方法调用被拦截。 接着,invoke方法根据被调用的方法所属的类方法名,从MapperRegistry中获取对应的MappedStatement对象。MappedStatement包含了定义在Mapper接口方法上的注解信息,以及与之对应的SQL语句。 然后,invoke方法根据MappedStatement的配置信息,决定执行查询、插入、更新、删除等具体操作。 在执行操作前,invoke方法会利用MapperMethod对象解析方法参数,并将参数与SQL语句中的占位符进行匹配,生成完整的SQL语句。 接下来,invoke方法会调用SqlSession的相关方法,执行SQL语句,并将执行结果返回给调用者。 最后,invoke方法会根据MapperMethod的配置信息,将返回结果进行适配,转化为最终的返回值。 值得一提的是,MybatisMapperProxy是基于JDK动态代理实现的,它将Mapper接口方法的调用转发给SqlSession对象进行处理。这样的设计使得Mybatis能够实现面向接口编程,并将代码中的SQL语句与Java逻辑进行解耦。 总之,MybatisMapperProxy的invoke方法是实现Mapper接口方法调用的核心逻辑,通过动态代理在运行时生成代理对象,并通过SqlSession完成具体的数据库操作。它为Mybatis提供了强大的灵活性可扩展性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值