3.5 mybatis源码篇-mapper解析-执行逻辑

版本说明:mybatis3.4.0

一、mapperMethod执行解析

上一章中mapper代理对象执行了mapperMethod、

mapperMethod.execute(this.sqlSession, args);

我们看这个类的一部分、可以看到大部分的执行方法类型了。

public class MapperMethod {
    //...
    public Object execute(SqlSession sqlSession, Object[] args) {
        Object param;
        Object result;
        if (SqlCommandType.INSERT == this.command.getType()) {
            param = this.method.convertArgsToSqlCommandParam(args);
            result = this.rowCountResult(sqlSession.insert(this.command.getName(), param));
        } else if (SqlCommandType.UPDATE == this.command.getType()) {
            param = this.method.convertArgsToSqlCommandParam(args);
            result = this.rowCountResult(sqlSession.update(this.command.getName(), param));
        } else if (SqlCommandType.DELETE == this.command.getType()) {
            param = this.method.convertArgsToSqlCommandParam(args);
            result = this.rowCountResult(sqlSession.delete(this.command.getName(), param));
        } else if (SqlCommandType.SELECT == this.command.getType()) {
            if (this.method.returnsVoid() && this.method.hasResultHandler()) {
                this.executeWithResultHandler(sqlSession, args);
                result = null;
            } else if (this.method.returnsMany()) {
                result = this.executeForMany(sqlSession, args);//我们取一个例子
            } else if (this.method.returnsMap()) {
                result = this.executeForMap(sqlSession, args);
            } else if (this.method.returnsCursor()) {
                result = this.executeForCursor(sqlSession, args);
            } else {
                param = this.method.convertArgsToSqlCommandParam(args);
                result = sqlSession.selectOne(this.command.getName(), param);
            }
        } else {
            if (SqlCommandType.FLUSH != this.command.getType()) {
                throw new BindingException("Unknown execution method for: " + this.command.getName());
            }

            result = sqlSession.flushStatements();
        }

        if (result == null && this.method.getReturnType().isPrimitive() && !this.method.returnsVoid()) {
            throw new BindingException("Mapper method '" + this.command.getName() + " attempted to return null from a method with a primitive return type (" + this.method.getReturnType() + ").");
        } else {
            return result;
        }
    }
    //...
    //这个是例子
      private <E> Object executeForMany(SqlSession sqlSession, Object[] args) {
          Object param = this.method.convertArgsToSqlCommandParam(args);
          List result;
          if (this.method.hasRowBounds()) {
              RowBounds rowBounds = this.method.extractRowBounds(args);
              result = sqlSession.selectList(this.command.getName(), param, rowBounds);//其实都是执行sqlSession的方法
          } else {
              result = sqlSession.selectList(this.command.getName(), param);
          }
  
          if (!this.method.getReturnType().isAssignableFrom(result.getClass())) {
              return this.method.getReturnType().isArray() ? this.convertToArray(result) : this.convertToDeclaredCollection(sqlSession.getConfiguration(), result);
          } else {
              return result;
          }
      }   
}
二、SqlSession的执行sql

我们简单了解一下

public class DefaultSqlSession implements SqlSession {
    //...
    public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
        List var5;
        try {
            MappedStatement ms = this.configuration.getMappedStatement(statement);
            var5 = this.executor.query(ms, this.wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
        } catch (Exception var9) {
            throw ExceptionFactory.wrapException("Error querying database.  Cause: " + var9, var9);
        } finally {
            ErrorContext.instance().reset();
        }

        return var5;
    }
    //...
} 

可以看到从configuration拿出来的MappedStatement,放到executor执行器中执行了。

MappedStatement中储存了sql语句和返回结果集的类型等,sqlsource和resultmaps等等。

其实executor就是把MappedStatement中的内容、转化为jdbc中的PreparedStatement并且执行,处理返回结果。

主要有几个处理对象:

StatementHandler:处理sql语句预编译,设置参数等相关工作;
ParameterHandler:设置预编译参数用的
ResultSetHandler:       处理结果集
TypeHandler:在整个过程中,进行数据库类型和javaBean类型的映射

这里讲解的比较简单,下面讲解的比较清楚。可以查看下面链接:
刘信坚的博客:Mybatis源码解析之四大对象
MyBatis源码分析-SQL语句执行的完整流程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值