Search in sources :

Example 6 with Executor

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;
}
Also used : BatchExecutor(org.apache.ibatis.executor.BatchExecutor) BatchExecutor(org.apache.ibatis.executor.BatchExecutor) ReuseExecutor(org.apache.ibatis.executor.ReuseExecutor) CachingExecutor(org.apache.ibatis.executor.CachingExecutor) SimpleExecutor(org.apache.ibatis.executor.SimpleExecutor) Executor(org.apache.ibatis.executor.Executor) CachingExecutor(org.apache.ibatis.executor.CachingExecutor) ReuseExecutor(org.apache.ibatis.executor.ReuseExecutor) SimpleExecutor(org.apache.ibatis.executor.SimpleExecutor)

Example 7 with 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);
    }
}
Also used : Executor(org.apache.ibatis.executor.Executor) ExecutorException(org.apache.ibatis.executor.ExecutorException) Configuration(org.apache.ibatis.session.Configuration) MetaObject(org.apache.ibatis.reflection.MetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) ExecutorException(org.apache.ibatis.executor.ExecutorException)

Example 8 with Executor

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());
}
Also used : Constructor(java.lang.reflect.Constructor) RowBounds(org.apache.ibatis.session.RowBounds) Executor(org.apache.ibatis.executor.Executor) BoundSql(org.apache.ibatis.mapping.BoundSql) Dialect(com.github.walker.mybatis.paginator.dialect.Dialect) List(java.util.List) MappedStatement(org.apache.ibatis.mapping.MappedStatement) CacheKey(org.apache.ibatis.cache.CacheKey) Cache(org.apache.ibatis.cache.Cache)

Aggregations

Executor (org.apache.ibatis.executor.Executor)8 BoundSql (org.apache.ibatis.mapping.BoundSql)4 MappedStatement (org.apache.ibatis.mapping.MappedStatement)4 RowBounds (org.apache.ibatis.session.RowBounds)4 CacheKey (org.apache.ibatis.cache.CacheKey)3 ResultHandler (org.apache.ibatis.session.ResultHandler)3 SQLException (java.sql.SQLException)2 List (java.util.List)2 Environment (org.apache.ibatis.mapping.Environment)2 Transaction (org.apache.ibatis.transaction.Transaction)2 TransactionFactory (org.apache.ibatis.transaction.TransactionFactory)2 ManagedTransactionFactory (org.apache.ibatis.transaction.managed.ManagedTransactionFactory)2 Dialect (com.github.walker.mybatis.paginator.dialect.Dialect)1 Constructor (java.lang.reflect.Constructor)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Cache (org.apache.ibatis.cache.Cache)1 BatchExecutor (org.apache.ibatis.executor.BatchExecutor)1 CachingExecutor (org.apache.ibatis.executor.CachingExecutor)1 ExecutorException (org.apache.ibatis.executor.ExecutorException)1