Mybatis之数据库操作流程解析

UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.selectByPrimaryKey(1);

这篇文章我们将从上面两行代码看起,首先就是获得mapper文件对应的映射类,然后执行里面的数据库操作:

public  T getMapper(Class type) {
    return configuration.getMapper(type, this);
  }

在方法里面将映射类的class对象和当前session作为参数在configuration里面查找:

public  T getMapper(Class type, SqlSession sqlSession) {
    return mapperRegistry.getMapper(type, sqlSession);
  }

然后从存放mapper的工厂里面来查找:

public  T getMapper(Class type, SqlSession sqlSession) {
	//根据class对象获取它对应的代理工厂
    final MapperProxyFactory mapperProxyFactory = (MapperProxyFactory) 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 mapperProxy = new MapperProxy(sqlSession, mapperInterface, methodCache);
    //作为参数实例化
    return newInstance(mapperProxy);
  }

将当前的会话、接口、和方法的缓存封装成一个mapper代理类,然后实例化:

protected T newInstance(MapperProxy mapperProxy) {
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  }

实际上刚刚创建的MapperProxy就是一个拦截器,那么我们所关注的点就是它里面的invoke方法:

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
    	//当前代理类是否是执行方法的声明类,如果是,直接执行
      if (Object.class.equals(method.getDeclaringClass())) {
        return method.invoke(this, args);
        //如果是默认的方法,则按照执行默认方法的逻辑处理
      } else if (isDefaultMethod(method)) {
        return invokeDefaultMethod(proxy, method, args);
      }
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    return mapperMethod.execute(sqlSession, args);
  }

首先会判断声明当前方法的是否是一个类,如果不是就需要创建一个MapperMethod :

public Object execute(SqlSession sqlSession, Object[] args) {
    Object result;
    switch (command.getType()) {
      case INSERT: {...  }
      case UPDATE: {...  }
      case DELETE: {...}
      case SELECT:...
      case FLUSH:...
      default:
        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;
  }

在MapperMethod 的execute方法里面会根据method的类型对应数据库的不同的操作进入不同的分支进行执行,在这我们就以select来看:

case SELECT:
        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 if (method.returnsCursor()) {
          result = executeForCursor(sqlSession, args);
        } else {
          Object param = method.convertArgsToSqlCommandParam(args);
          result = sqlSession.selectOne(command.getName(), param);
        }
        break;

根据不同的返回值类型采用不同的执行方法,在后面我们会看到对method的方法调用,所以我们在这就先深入到这个地方,然后我们回到动态代理的创建:

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

动态代理创建完后就返回了我们需要的接口类型的实例,下面就开始执行具体的方法了:

User user = mapper.selectByPrimaryKey(1);

获取主键为1的对象:

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      if (Object.class.equals(method.getDeclaringClass())) {
        return method.invoke(this, args);
      } else if (isDefaultMethod(method)) {
        return invokeDefaultMethod(proxy, method, args);
      }
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    return mapperMethod.execute(sqlSession, args);
  }

执行代理类的invoke方法,和我们刚才看见的一样,由于在这里mapper文件里面的命名空间对应的类是一个接口,所以在这是会创建一个mappermethod来执行:

Object param = method.convertArgsToSqlCommandParam(args);
 result = sqlSession.selectOne(command.getName(), param);

在execute方法里面由于是select方法且返回值只有一个,所以只会执行上面这段代码段,首先将我们的参数封装成Map或者是单个对象,然后执行数据库操作返回结果:

public  T selectOne(String statement, Object parameter) {
    //执行数据库操作
    List list = this.selectList(statement, parameter);
    if (list.size() == 1) {
      return list.get(0);
    } else if (list.size() > 1) {
      throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
    } else {
      return null;
    }
  }

statement实际上是我们的接口的全限命名和方法的名字,parameter则是我们传入的参数:

public  List selectList(String statement, Object parameter, RowBounds rowBounds) {
    try {
    //根据statement获取对应的sql语句
      MappedStatement ms = configuration.getMappedStatement(statement);
      //执行器执行
      return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
    } catch (Exception e) {
      throw ExceptionFactory.wrapException("Error querying database.  Cause: " + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }

首先会根据statement找到对应的sql语句,然后执行器带着sql语句和参数执行query方法:

public  List query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
	//带着参数从ms中获取绑定的sql
    BoundSql boundSql = ms.getBoundSql(parameterObject);
    //生成一个缓存的key
    CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);
    执行查询
    return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
  }

我们之前解析完sql后,存在着一些占位符,所以需要我们传入的参数进行填充,下面我们就来看一下是如何获得绑定的sql的:

public BoundSql getBoundSql(Object parameterObject) {
	//从当前sqlSource中获得sql
    BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
    //获取需要的参数列表
    List parameterMappings = boundSql.getParameterMappings();
    //如果需要的参数为空,就直接重新创建一个sql
    if (parameterMappings == null || parameterMappings.isEmpty()) {
      boundSql = new BoundSql(configuration, boundSql.getSql(), parameterMap.getParameterMappings(), parameterObject);
    }
	...

    return boundSql;
  }

接着看从sqlSource里面获取 sql:

public BoundSql getBoundSql(Object parameterObject) {
    return new BoundSql(configuration, sql, parameterMappings, parameterObject);
  }

实际上就是创建了要给sql 的对象,保存了我们传入的值。获取到boundSql之后会为当前这次查询生成一个cacheKey:

public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) {
    if (closed) {
      throw new ExecutorException("Executor was closed.");
    }
    CacheKey cacheKey = new CacheKey();
    cacheKey.update(ms.getId());
    cacheKey.update(rowBounds.getOffset());
    cacheKey.update(rowBounds.getLimit());
    cacheKey.update(boundSql.getSql());
    List parameterMappings = boundSql.getParameterMappings();
    TypeHandlerRegistry typeHandlerRegistry = ms.getConfiguration().getTypeHandlerRegistry();
    // mimic DefaultParameterHandler logic
    for (ParameterMapping parameterMapping : parameterMappings) {
      if (parameterMapping.getMode() != ParameterMode.OUT) {
        Object value;
        String propertyName = parameterMapping.getProperty();
        if (boundSql.hasAdditionalParameter(propertyName)) {
          value = boundSql.getAdditionalParameter(propertyName);
        } else if (parameterObject == null) {
          value = null;
        } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
          value = parameterObject;
        } else {
          MetaObject metaObject = configuration.newMetaObject(parameterObject);
          value = metaObject.getValue(propertyName);
        }
        cacheKey.update(value);
      }
    }
    if (configuration.getEnvironment() != null) {
      // issue #176
      cacheKey.update(configuration.getEnvironment().getId());
    }
    return cacheKey;
  }

看完我们就会发现,一次完全相同的查询包括相同的statementid,相同的结果集的偏移量和数据量,相同的需要的参数的值,以及相同的sql语句

public  List query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)
      throws SQLException {
      //首先检查缓存中有没有结果
    Cache cache = ms.getCache();
    //缓存中存在
    if (cache != null) {
      flushCacheIfRequired(ms);
      if (ms.isUseCache() && resultHandler == null) {
        ensureNoOutParams(ms, boundSql);
        @SuppressWarnings("unchecked")
        List list = (List) tcm.getObject(cache, key);
        //没有在缓存中获取到值,需要再重新执行查询,然后放到缓存中
        if (list == null) {
          list = delegate. query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
          tcm.putObject(cache, key, list); // issue #578 and #116
        }
        return list;
      }
    }
    //缓存中没有,直接从数据库中查询
    return delegate. query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
  }

如果缓存不为空,那么就会先尝试从缓存中获取,获取失败后还会从数据库中查询:

public  List query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
    ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());
    if (closed) {
      throw new ExecutorException("Executor was closed.");
    }
    //当前没有线程执行查询并且需要刷新缓存,那么就要清空缓存
    if (queryStack == 0 && ms.isFlushCacheRequired()) {
      clearLocalCache();
    }
    List list;
    try {
    //每当一个线程要执行缓存就要计一次数
      queryStack++;
      //从缓存中查找,如果没有执行查询
      list = resultHandler == null ? (List) localCache.getObject(key) : null;
      if (list != null) {
        handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);
      } else {
        list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
      }
    } finally {
    //执行完查询后计数器减1
      queryStack--;
    }
    if (queryStack == 0) {
      for (DeferredLoad deferredLoad : deferredLoads) {
        deferredLoad.load();
      }
      // issue #601
      deferredLoads.clear();
      if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {
        // issue #482
        clearLocalCache();
      }
    }
    return list;
  }

这个方法对进行查询的线程的数量进行了统计,并在合适的时刻清除缓存,查询的时候还是会尝试从缓存中查找,找不到的时候再从数据库中查:

public  List doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
    Statement stmt = null;
    try {
      Configuration configuration = ms.getConfiguration();
      //创建一个生成statement的处理器
      StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
      //生成statement
      stmt = prepareStatement(handler, ms.getStatementLog());
      //执行查询
      return handler.query(stmt, resultHandler);
    } finally {
      closeStatement(stmt);
    }
  }

最后通过statement执行的查询,所以说再mybatis的mapper文件中建议使用#就是由于#会被替换成?,在生成最终的sql的时候是作为参数被替换的,而不是简单的字符串拼接,可以防止sql注入:

private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
    Statement stmt;
    //获取数据库连接
    Connection connection = getConnection(statementLog);
    //准备一些连接信息和超时信息
    stmt = handler.prepare(connection, transaction.getTimeout());
    //将占位符替换成我们传入的数据
    handler.parameterize(stmt);
    return stmt;
  }

在这方法中就是最终完成了statement的封装并返回。

你可能感兴趣的:(Mybatis)