private void processGeneratedKeys(Executor executor, MappedStatement ms, Object parameter) { try { if (parameter != null && keyStatement != null && keyStatement.getKeyProperties() != null) { String keyProperty = keyStatement.getKeyProperties()[0]; // just one key property is supported final Configuration configuration = ms.getConfiguration(); final MetaObject metaParam = configuration.newMetaObject(parameter); if (keyProperty != null && metaParam.hasSetter(keyProperty)) { // 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 { metaParam.setValue(keyProperty, values.get(0)); } } } } catch (ExecutorException e) { throw e; } catch (Exception e) { throw new ExecutorException( "Error selecting key or setting result to parameter object. Cause: " + e, e); } }
/** * @description TODO * @createTime 2016-4-27 下午12:33:11 * @fileName CacheInterceptor.java * @author yaojiamin */ private <E> List<E> executeQuery( CacheHandlerBean cacheHandlerBean, BoundSql boundSql, CacheKey cacheKey, Executor executor) throws SQLException { return executor.query( cacheHandlerBean.getMappedStatement(), cacheHandlerBean.getParameterObject(), cacheHandlerBean.getRowBounds(), cacheHandlerBean.getResultHandler(), cacheKey, boundSql); }
private <E> List<E> selectList() throws SQLException { Executor localExecutor = executor; if (localExecutor.isClosed()) { localExecutor = newExecutor(); } try { return localExecutor.<E>query( mappedStatement, parameterObject, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER, cacheKey, boundSql); } finally { if (localExecutor != executor) { localExecutor.close(false); } } }