zoukankan      html  css  js  c++  java
  • Mybatis是怎么执行一条语句的

      根据官方的推荐方式,通过mapper的方式执行sql,mapper的方式就是动态代理

      所以,我们就先看动态代理的入口

      一 MapperMethod

    public class MapperProxy<T> implements InvocationHandler, Serializable {
    
      private static final long serialVersionUID = -6424540398559729838L;
      private final SqlSession sqlSession;
      private final Class<T> mapperInterface;
      private final Map<Method, MapperMethod> methodCache;
    
      public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
        this.sqlSession = sqlSession;
        this.mapperInterface = mapperInterface;
        this.methodCache = 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开始执行

      二 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));

      三 Executor 

    @Override
      public int update(String statement, Object parameter) {
        try {
          dirty = true;
          MappedStatement ms = configuration.getMappedStatement(statement);
          return executor.update(ms, wrapCollection(parameter));
        } catch (Exception e) {
          throw ExceptionFactory.wrapException("Error updating database.  Cause: " + e, e);
        } finally {
          ErrorContext.instance().reset();
        }
      }

      四 StatementHandler

    @Override
      public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
        Configuration configuration = ms.getConfiguration();
        StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
        Statement stmt = prepareStatement(handler, ms.getStatementLog());
        return handler.update(stmt);
      }

      总结就是,mybatis的执行链路,

      1 MapperMethod

      2 SqlSession 

      3 Executor 

      4 StatementHandler

  • 相关阅读:
    TextSharp详情
    Java使用Itext5.5.10进行pdf签章
    java生成PDF,各种格式、样式、水印都有
    使用itext直接替换PDF中的文本
    在Asp.Net中操作PDF – iTextSharp
    使用iTextSharp修改PDF文件(一)
    微信公众平台HTTPS方式调用配置免费https服务器
    ef SQL Server 版本不支持数据类型“datetime2”
    开源框架.netCore DncZeus学习(三)增加一个菜单
    开源框架.netCore DncZeus学习(二)配置连接
  • 原文地址:https://www.cnblogs.com/juniorMa/p/13933413.html
Copyright © 2011-2022 走看看