use of org.apache.ibatis.executor.Executor in project mybatis-3 by mybatis.
the class Configuration method newExecutor.
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
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);
}
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}
use of org.apache.ibatis.executor.Executor in project mybatis-3 by mybatis.
the class SelectKeyGenerator method processGeneratedKeys.
private void processGeneratedKeys(Executor executor, MappedStatement ms, Object parameter) {
try {
if (parameter != null && keyStatement != null && keyStatement.getKeyProperties() != null) {
String[] keyProperties = keyStatement.getKeyProperties();
final Configuration configuration = ms.getConfiguration();
final MetaObject metaParam = configuration.newMetaObject(parameter);
if (keyProperties != null) {
// Do not close keyExecutor.
// The transaction will be closed by parent executor.
Executor keyExecutor = configuration.newExecutor(executor.getTransaction(), ExecutorType.SIMPLE);
List<Object> values = keyExecutor.query(keyStatement, parameter, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
if (values.size() == 0) {
throw new ExecutorException("SelectKey returned no data.");
} else if (values.size() > 1) {
throw new ExecutorException("SelectKey returned more than one value.");
} else {
MetaObject metaResult = configuration.newMetaObject(values.get(0));
if (keyProperties.length == 1) {
if (metaResult.hasGetter(keyProperties[0])) {
setValue(metaParam, keyProperties[0], metaResult.getValue(keyProperties[0]));
} else {
// no getter for the property - maybe just a single value object
// so try that
setValue(metaParam, keyProperties[0], values.get(0));
}
} else {
handleMultipleProperties(keyProperties, metaParam, metaResult);
}
}
}
}
} catch (ExecutorException e) {
throw e;
} catch (Exception e) {
throw new ExecutorException("Error selecting key or setting result to parameter object. Cause: " + e, e);
}
}
use of org.apache.ibatis.executor.Executor in project mybatis-paginator by HuQingmiao.
the class OffsetLimitInterceptor method intercept.
public Object intercept(final Invocation invocation) throws Throwable {
final Executor executor = (Executor) invocation.getTarget();
final Object[] queryArgs = invocation.getArgs();
final MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX];
final Object parameter = queryArgs[PARAMETER_INDEX];
final RowBounds rowBounds = (RowBounds) queryArgs[ROWBOUNDS_INDEX];
//DAO接口中没有传PageBounds参量
if (!(rowBounds instanceof PageBounds)) {
return invocation.proceed();
}
//DAO接口传有PageBounds参量
PageBounds pageBounds = (PageBounds) rowBounds;
if (pageBounds.notValid()) {
return new PageList((List) invocation.proceed());
}
final Dialect dialect;
try {
Class clazz = Class.forName(dialectClass);
Constructor constructor = clazz.getConstructor(MappedStatement.class, Object.class, PageBounds.class);
dialect = (Dialect) constructor.newInstance(new Object[] { ms, parameter, pageBounds });
} catch (Exception e) {
throw new ClassNotFoundException("Cannot create dialect instance: " + dialectClass, e);
}
final BoundSql boundSql = ms.getBoundSql(parameter);
queryArgs[MAPPED_STATEMENT_INDEX] = copyFromNewSql(ms, boundSql, dialect.getPageSQL(), dialect.getParameterMappings(), dialect.getParameterObject());
queryArgs[PARAMETER_INDEX] = dialect.getParameterObject();
queryArgs[ROWBOUNDS_INDEX] = new RowBounds();
//采用同步方式,执行分页查询
//修复bug: 提交后台线程破坏了spring 事务管理,参见TransactionSynchronizationManager
Callable<List> queryThread = new Callable<List>() {
public List call() throws Exception {
return (List) invocation.proceed();
}
};
Future<List> queryFuture = call(queryThread, false);
//如果不需要count总的结果集,则直接返回分页查询结果
if (!pageBounds.isIfCount()) {
return new PageList(queryFuture.get());
}
//对总的结果集进行count
Callable<Integer> countThread = new Callable<Integer>() {
public Integer call() throws Exception {
Cache cache = ms.getCache();
Integer count = null;
if (cache != null && ms.isUseCache() && ms.getConfiguration().isCacheEnabled()) {
CacheKey cacheKey = executor.createCacheKey(ms, parameter, new RowBounds(), copyFromBoundSql(ms, boundSql, dialect.getCountSQL(), boundSql.getParameterMappings(), boundSql.getParameterObject()));
count = (Integer) cache.getObject(cacheKey);
if (count == null) {
count = SQLHelp.getCount(ms, parameter, boundSql, dialect);
cache.putObject(cacheKey, count);
}
} else {
count = SQLHelp.getCount(ms, parameter, boundSql, dialect);
}
return count;
}
};
Future<Integer> countFutrue = call(countThread, false);
return new PageList(queryFuture.get(), countFutrue.get().intValue());
}
Aggregations