mybatis中一个查询方法的执行流程

本文深入探讨了MyBatis中查询方法的执行过程,从获取Mapper代理对象开始,详细阐述了如何通过MapperProxy的invoke()方法进行参数处理、执行查询、涉及的一级和二级缓存机制,以及结果集的处理。整个流程涵盖了从SQL预编译到结果映射的各个环节,有助于理解MyBatis的工作原理。
摘要由CSDN通过智能技术生成

一:简述
本文通过分析mybatis的源码来展示mybatis是如何帮我们实现一个查询的方法的。

二:时序图
在这里插入图片描述
三:源码分析

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

注:这里获取的是一个代理对象,而我们一般从Spring容器中获取的也是同样的代理对象,只不过是交给Spring管理了而已

1.获取代理类Mapper

sqlSession的getMapper()方法会先调用configuration的getMapper()方法,最后调用mapperRegistry.getMapper()

public <T> T getMapper(Class<T> type) {
   
    //最后会去调用MapperRegistry.getMapper
    return configuration.<T>getMapper(type, this);
  }
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
   
    return mapperRegistry.getMapper(type, sqlSession);
  }

在我们解析配置的时候,会将解析好的Mapper接口和对应的代理工厂关联起来,保存在mapperRegistry的knownMappers中。而mapperRegistry的getMapper方法就是从这个knownMappers的map中获取工厂对象,然后利用工厂创建代理类并且返回。

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
   
    //knownMapper是一个map key是Mapper接口的类对象,
    //value是对应的代理工厂 用来创建代理对象的
    final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
    if (mapperProxyFactory == null) {
   
      throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
    }
    try {
   
      return mapperProxyFactory.newInstance(sqlSession);
    } catch (Exception e) {
   
      throw new BindingException("Error getting mapper instance. Cause: " + e, e);
    }
  }

代理工厂创建代理类:

public T newInstance(SqlSession sqlSession) {
   
    final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
    return newInstance(mapperProxy);
  }
protected T newInstance(MapperProxy<T> mapperProxy) {
   
    //用JDK自带的动态代理生成映射器
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] {
    mapperInterface }, mapperProxy);
  }

所以返回的对象应该是一个MapperProxy对象,而我们在调用mapper的方法的时候应该也是进入到MapperProxy的invoke()方法。

2.执行查询方法
根据上面的分析,我们会调用MapperProxy的invoke()方法

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
   
    //代理以后,所有Mapper的方法调用时,都会调用这个invoke方法
    //并不是任何一个方法都需要执行调用代理对象进行执行,如果这个方法是Object中通用的方法(toString、hashCode等)无需执行
    if (Object.class.equals(method.getDeclaringClass())) {
   
      try {
   
        return method.invoke(this, args);
      } catch (Throwable t) {
   
        throw ExceptionUtil.unwrapThrowable(t);
      }
    }
    //获取MapperMethod MapperMethod会执行代理的一些逻辑
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    //执行
    return mapperMethod.execute(sqlSession, args);
  }
public Object execute(SqlSession sqlSession, Object[] args) {
   
    Object result;
    //可以看到执行时就是4种情况,insert|update|delete|select,
    //分别调用SqlSession的4大类方法 我们这里就以查询为例
    //查询是最复杂的 因为还需要对结果集进行处理
    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)
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值