JavaEE鸿蒙应用开发HTML&JS+前端Python+大数据开发人工智能开发电商视觉设计软件测试新媒体+短视频直播运营产品经理集成电路应用开发(含嵌入式)Linux云计算+运维开发C/C++拍摄剪辑+短视频制作PMP项目管理认证电商运营Go语言与区块链大数据PHP工程师Android+物联网iOS.NET

Mybatis是如何执行一条SQL命令

来源:黑马程序员

浏览15443人

2020.10.14

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

 

public enum SqlCommandType {  

    UNKNOWN, INSERT, UPDATE, DELETE, SELECT, FLUSH;

}

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

 

public interface StudentMapper {

  List findAllStudents(Map<String, Object> map, RowBounds rowBounds, ResultSetHandler rh);  

}

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

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

1602662117758774.png

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

MapperProxy的功能:

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

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() + ").");

      }