Mybatis中的Sql命令,在枚举类SqlCommandType中定义的。

public enum SqlCommandType {  UNKNOWN, INSERT, UPDATE, DELETE, SELECT, FLUSH;}

下面,我们以Mapper接口中的一个方法作为例子,看看Sql命令的执行完整流程。

public interface StudentMapper {  List findAllStudents(Map map, RowBounds rowBounds, ResultSetHandler rh);  }

参数RowBounds和ResultSetHandler是可选参数,表示分页对象和自定义结果集处理器,一般不需要。

一个完整的Sql命令,其执行的完整流程图如下:

(Made In Edrawmax)

对于上面的流程图,如果看过前面的文章的话,大部分对象我们都比较熟悉了。一个图,就完整展示了其执行流程。

MapperProxy的功能:

1. 因为Mapper接口不能直接实例化,MapperProxy的作用,就是使用JDK动态代理功能,间接实例化Mapper的proxy对象。可参看系列的第二篇。

2. 缓存MapperMethod对象。

  private final Map methodCache;  @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);  }  // 缓存MapperMethod  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的功能:

1. 解析Mapper接口的方法,并封装成MapperMethod对象。

2. 将Sql命令,正确路由到恰当的SqlSession的方法上。

public class MapperMethod {  // 保存了Sql命令的类型和键id  private final SqlCommand command;  // 保存了Mapper接口方法的解析信息  private final MethodSignature method;  public MapperMethod(Class> mapperInterface, Method method, Configuration config) {    this.command = new SqlCommand(config, mapperInterface, method);    this.method = new MethodSignature(config, method);  }  // 根据解析结果,路由到恰当的SqlSession方法上  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;  }  // ...

org.apache.ibatis.binding.MapperMethod.SqlCommand。

public static class SqlCommand {    // full id, 通过它可以找到MappedStatement    private final String name;    private final SqlCommandType type;// ...

org.apache.ibatis.binding.MapperMethod.MethodSignature。

  public static class MethodSignature {    private final boolean returnsMany;    private final boolean returnsMap;    private final boolean returnsVoid;    private final Class> returnType;    private final String mapKey;    private final Integer resultHandlerIndex;    private final Integer rowBoundsIndex;    private final SortedMap params;    private final boolean hasNamedParameters;    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);      // 自定义ResultHandler      this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class);      this.params = Collections.unmodifiableSortedMap(getParams(method, this.hasNamedParameters));    }

以上是对MapperMethod的补充说明。

本节的重点,是上面的那个Sql命令完整执行流程图。如果不是使用Mapper接口调用,而是直接调用SqlSession的方法,那么,流程图从SqlSession的地方开始即可,后续都是一样的。

来源:https://my.oschina.net/zudajun/blog/670373

mybatis直接执行sql_拼多多二面:Mybatis是如何执行一条SQL命令的?相关推荐

  1. 拼多多二面:Mybatis是如何执行一条SQL命令的?

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 Mybatis中的Sql命令,在枚举类SqlCommandType ...

  2. oracle一条sql的执行过程,请问执行一条sql的整个过程是怎样的,谢谢!

    select count(*) into c from yaoyao_pbj where object_id=7559; select count(*) into c from yaoyao_pbj ...

  3. mysql执行一条sql语句的完整过程,sql语句在mysql中的执行过程

    文章目录 1. SQL语句在mysql的执行过程 一:客户端层 二:mysql服务器层 三:innoDB引擎层 2. undo日志.redo日志.binlog日志的区别? 1. SQL语句在mysql ...

  4. c mysql并行多条sql_Linux上使用C语言执行多条SQL命令访问MYSQL数据库的有关问题...

    Linux下使用c语言执行多条SQL命令访问mysql数据库的问题 Linux(Ubuntu10.04)系统,MYSQL5.1数据库,C语言! 数据库中有两张表:表A.B! 使用C语言从表A中查询数据 ...

  5. Spring学习笔记:Spring整合Mybatis(mybatis-spring.jar)(二:mybatis整合spring)

    http://blog.csdn.net/qq598535550/article/details/51703190 二.Spring整合mybatis其实是在mybatis的基础上实现Spring框架 ...

  6. 某拼多多程序员:拼多多员工学历比阿里强了几条街!拼多多平均985!阿里平均普通本科!难怪拼多多氛围这么好!...

    请点击上面 一键关注! 互联网大厂对学历的要求有多高?有的大厂要求211,985,有的大厂却觉得普通一本二本都可以,只要能干活就行,这之间的差别有多大呢? 一个拼多多程序员发帖说,拼多多的员工整体学历 ...

  7. Java让数据库执行一条sql_java数据库编程——执行SQL 语句

    [0]README [1]java数据库编程--执行SQL 语句相关 1)执行 SQL 命令前, 首先需要创建一个 Statement 对象: 要创建 statement 对象,不需要调用 Drive ...

  8. sql if 和insert_拼多多面试:Mybatis是如何实现SQL语句复用功能的?

    在工作中,往往有这样的需求,对于同一个sql条件查询,首先需要统计记录条数,用以计算pageCount,然后再对结果进行分页查询显示,看下面一个例子. <sql id="student ...

  9. 定时任务 每小时执行一次 每天8-18点每30分钟执行一次

    每小时执行一次 0 0 */1 * * * 每天8-18点每30分钟执行一次 0 0/30 8-18 * * ?

最新文章

  1. c# 读取excel的一系列问题
  2. [转]把复杂事物简明化
  3. BasicExcel CSpreadSheet 使用感受
  4. Shell 概述、截取字符操作等
  5. Phoenix命令及语法
  6. 黑客Alex Tapanaris与PDF文档
  7. kafaka的消息存储机制
  8. 第一个servlet
  9. 如何用简单易懂的例子解释隐马尔可夫模型?(进阶篇)
  10. hibernate-annotation
  11. Axure RP 8.0激活码 Mac Windows
  12. Linux入学—共享文件夹(保姆教程)
  13. Python常用取整函数
  14. 计算机网络中ipv6什么意思,路由器ipv6是什么意思(图文)
  15. 区块链Baas应用服务平台开发搭建
  16. CocosCreator快速接入bugly
  17. 致远OA—V5版本系统预置用户密码恢复方法
  18. 伤病缠身仍愿竭力而战 澳网一别穆雷何时再见?
  19. react-router如何配置可选参数
  20. 为什么阿里巴巴规定禁止超过三张表 join

热门文章

  1. Python实战技术 - Python虚拟隔离环境 和 Docker技术
  2. java项目整合mybatis_JavaWeb项目整合Spring,SpringMVC,Mybatis框架
  3. java获取classes_一个Java项目布署到weblogic里,听说weblogic会把classes目录打成jar包,怎么获取classes里文件的路径...
  4. SparkStreaming安全消费Kafka数据
  5. 第十二章_网络搭建及训练
  6. qt分割获取文件路径(去文件名)
  7. php面试专题---6、正则表达式考点
  8. Ant Desing Pro2.0(一)项目初始化
  9. 复制虚拟机/vmware中linux系统
  10. 第二次扩大会议(3.19)