您的位置:首页 > 其它

Mybatis中的Executor

2013-08-30 12:07 120 查看
使用mybatis查寻数据,跟踪其执行流程

最开始执行的语句

[java] view
plaincopyprint?

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

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

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

[java] view
plaincopyprint?

@Autowired(required = false)

public final void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {

this.sqlSession = sqlSessionTemplate;

this.externalSqlSession = true;

}

[html] view
plaincopyprint?

<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里面的,再看SqlSessionTemplate,里面的都是通过sqlSessionProxy来执行selectList方法的,也就是通过代理方式来的

[java] view
plaincopyprint?

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

[java] view
plaincopyprint?

final SqlSession sqlSession = getSqlSession(

SqlSessionTemplate.this.sqlSessionFactory,

SqlSessionTemplate.this.executorType,

SqlSessionTemplate.this.exceptionTranslator);

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

[java] view
plaincopyprint?

SqlSession session = sessionFactory.openSession(executorType);

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

[java] view
plaincopyprint?

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

}

}

通过

[java] view
plaincopyprint?

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

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

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

[java] view
plaincopyprint?

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: