Example usage for org.apache.ibatis.executor Executor query

List of usage examples for org.apache.ibatis.executor Executor query

Introduction

In this page you can find the example usage for org.apache.ibatis.executor Executor query.

Prototype

<E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler)
            throws SQLException;

Source Link

Usage

From source file:com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.java

License:Apache License

public Object queryForObject(final String id, final Object parameterObject) throws SQLException {
    return transactionManager.doInTransaction(new TransactionScope() {
        public Object execute(Transaction transaction) throws SQLException {
            MappedStatement ms = configuration.getMappedStatement(id);
            Executor executor = transaction.getExecutor();
            List list = executor.query(ms, wrapCollection(parameterObject), RowBounds.DEFAULT, null);
            if (list.size() == 1) {
                return list.get(0);
            } else if (list.size() > 1) {
                throw new SQLException("queryForObject() returned more than one row.");
            } else {
                return null;
            }/* w  ww  .ja va 2s  .  c  o m*/
        }
    });
}

From source file:com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.java

License:Apache License

public List queryForList(final String id, final Object parameterObject, final int skip, final int max)
        throws SQLException {
    return (List) transactionManager.doInTransaction(new TransactionScope() {
        public Object execute(Transaction transaction) throws SQLException {
            Executor executor = transaction.getExecutor();
            MappedStatement ms = configuration.getMappedStatement(id);
            return executor.query(ms, wrapCollection(parameterObject), new RowBounds(skip, max), null);
        }//from   www  .  jav  a2  s.  co m
    });
}

From source file:com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.java

License:Apache License

public void queryWithRowHandler(final String id, final Object parameterObject, final RowHandler rowHandler)
        throws SQLException {
    transactionManager.doInTransaction(new TransactionScope() {
        public Object execute(Transaction transaction) throws SQLException {
            MappedStatement ms = configuration.getMappedStatement(id);
            Executor executor = transaction.getExecutor();
            return executor.query(ms, wrapCollection(parameterObject), RowBounds.DEFAULT, new ResultHandler() {
                public void handleResult(ResultContext context) {
                    rowHandler.handleRow(context.getResultObject());
                }//ww  w.j  a  v  a2  s  . c  om
            });
        }
    });
}

From source file:com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.java

License:Apache License

private int getKey(String id, final Object parameterObject) throws SQLException {
    int key = Integer.MIN_VALUE;
    String selectKeyId = selectKeyIdFor(id);
    final MappedStatement keyStatement;

    if (configuration.getMappedStatementNames().contains(selectKeyId)) {
        keyStatement = configuration.getMappedStatement(selectKeyId);
    } else {//from   w ww .  j  a  va 2s .c  om
        keyStatement = null;
    }
    if (keyStatement != null) {
        List results = (List) transactionManager.doInTransaction(new TransactionScope() {
            public Object execute(Transaction transaction) throws SQLException {
                transaction.setCommitRequired(true);
                Executor executor = transaction.getExecutor();
                return executor.query(keyStatement, parameterObject, RowBounds.DEFAULT, null);
            }
        });
        try {
            key = (Integer) ((Map) results.get(0)).values().iterator().next();
        } catch (Exception e) {
            //Ignore...sometimes code sucks.  This is a good example.
        }
    }
    try {
        String property = keyStatement.getResultMaps().get(0).getResultMappings().get(0).getProperty();
        SystemMetaObject.forObject(parameterObject).setValue(property, key);
    } catch (Exception e) {
        //Ignore...sometimes code sucks.  This is a good example.
    }
    return key;
}

From source file:com.sinotopia.mybatis.mapper.mapperhelper.SelectKeyGenerator.java

License:Apache License

private void processGeneratedKeys(Executor executor, MappedStatement ms, Object parameter) {
    try {//w w  w.j  av a2 s. co m
        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);
    }
}