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

点击上方“方志朋”,选择“设为星标”

回复”666“获取新整理的面试文章

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

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

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

public interface StudentMapper {
  List<Student> findAllStudents(Map<String, Object> map, RowBounds rowBounds, ResultSetHandler rh);  
}

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

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

(Made In Edrawmax)

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

MapperProxy的功能:

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

2. 缓存MapperMethod对象。

  private final Map<Method, MapperMethod> 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<Integer, String> 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

热门内容:Docker 部署SpringBoot项目不香吗?为什么 Java 中“1000==1000”为false,而”100==100“为true?
面试官问我:如何设计 QQ、微信等第三方账号登陆 ?还要我说出数据库表设计!
为什么MySQL不推荐使用uuid或者雪花id作为主键?

最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。
获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。

明天见(。・ω・。)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值