关于mybatis里面的Executor

来源:互联网 发布:网络神曲排行榜2017 编辑:程序博客网 时间:2024/05/06 15:59
使用mybatis查寻数据,跟踪其执行流程

最开始执行的语句

this.getSqlSession().selectList("QUERY-QUESTION", data, rowBounds);

这里需要找到sqlsession是从哪里来的

getSqlSession是SqlSessionDaoSupport类里面的方法,该类通过spring的自动注入可以把sqlSessionTemplate注入进来,当然这里的sqlSessionTemplate是需要spring配置的

  @Autowired(required = false)  public final void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {    this.sqlSession = sqlSessionTemplate;    this.externalSqlSession = true;  }

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"><constructor-arg index="0" ref="sqlSessionFactory"/><constructor-arg index="1" value="BATCH"/></bean>

@Autowired(required = false)是通过类型匹配来注入的,如果没有找到相应对,就不用注入

所以selectList方法为SqlSessionTemlate里面的,再看SqlSessionTemplage,里面的都是通过sqlSessionProxy来执行selectList方法的,也就是通过代理方式来的

  public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,      PersistenceExceptionTranslator exceptionTranslator) {    notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required");    notNull(executorType, "Property 'executorType' is required");    this.sqlSessionFactory = sqlSessionFactory;    this.executorType = executorType;    this.exceptionTranslator = exceptionTranslator;    this.sqlSessionProxy = (SqlSession) newProxyInstance(        SqlSessionFactory.class.getClassLoader(),        new Class[] { SqlSession.class },        new SqlSessionInterceptor());  }

这里用到了java的动态代理,详细可以见java api,有详细的说明

SqlSessionInterceptor实现了InvocationHandler,在invoke方法里面的开始有这样代码,那里是真正的sqlsession

 final SqlSession sqlSession = getSqlSession(          SqlSessionTemplate.this.sqlSessionFactory,          SqlSessionTemplate.this.executorType,          SqlSessionTemplate.this.exceptionTranslator);

跟踪geteSqlSession可以找到他的创建来源,见

SqlSession session = sessionFactory.openSession(executorType);

继续跟踪可以找到DefaultSqlSessionFactory里面的该方法

  private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {    Transaction tx = null;    try {      final Environment environment = configuration.getEnvironment();      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);      tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);      final Executor executor = configuration.newExecutor(tx, execType, autoCommit);      return new DefaultSqlSession(configuration, executor);    } catch (Exception e) {      closeTransaction(tx); // may have fetched a connection so lets call close()      throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);    } finally {      ErrorContext.instance().reset();    }  }

通过
 final Executor executor = configuration.newExecutor(tx, execType, autoCommit);

你就知道executor是怎么回来的了

mybatis默认使用了cache,在创建exector时,外面就包了一层CacheExecutor,详细见

  public Executor newExecutor(Transaction transaction, ExecutorType executorType, boolean autoCommit) {    executorType = executorType == null ? defaultExecutorType : executorType;    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;    Executor executor;    if (ExecutorType.BATCH == executorType) {      executor = new BatchExecutor(this, transaction);    } else if (ExecutorType.REUSE == executorType) {      executor = new ReuseExecutor(this, transaction);    } else {      executor = new SimpleExecutor(this, transaction);    }    if (cacheEnabled) {      executor = new CachingExecutor(executor, autoCommit);    }    executor = (Executor) interceptorChain.pluginAll(executor);    return executor;  }

CachingExecutor可以使mybatis先从缓存中提取数据,数据缓存中没有数据时才从数据库里面提取数据

原创粉丝点击