Example usage for org.apache.ibatis.reflection MetaObject hasGetter

List of usage examples for org.apache.ibatis.reflection MetaObject hasGetter

Introduction

In this page you can find the example usage for org.apache.ibatis.reflection MetaObject hasGetter.

Prototype

public boolean hasGetter(String name) 

Source Link

Usage

From source file:com.appleframework.orm.mybatis.pagehelper.SqlUtil.java

License:Open Source License

/**
 * ??//from ww w.j  a v  a  2s .  c  o m
 *
 * @param paramsObject
 * @param paramName
 * @param required
 * @return
 */
public static Object getParamValue(MetaObject paramsObject, String paramName, boolean required) {
    Object value = null;
    if (paramsObject.hasGetter(PARAMS.get(paramName))) {
        value = paramsObject.getValue(PARAMS.get(paramName));
    }
    if (value != null && value.getClass().isArray()) {
        Object[] values = (Object[]) value;
        if (values.length == 0) {
            value = null;
        } else {
            value = values[0];
        }
    }
    if (required && value == null) {
        throw new RuntimeException("??:" + PARAMS.get(paramName));
    }
    return value;
}

From source file:com.dao.genericdao.mybatis.plugins.page.support.SqlUtil.java

License:Open Source License

/**
 * ??/*from  www.j  a  va  2 s.  c om*/
 *
 * @param paramsObject
 * @param paramName
 * @param required
 * @return
 */
public static Object getParamValue(MetaObject paramsObject, String paramName, boolean required) {
    Object value = null;
    if (paramsObject.hasGetter(PARAMS.get(paramName))) {
        value = paramsObject.getValue(PARAMS.get(paramName));
    }
    if (required && value == null) {
        throw new RuntimeException("??:" + PARAMS.get(paramName));
    }
    return value;
}

From source file:com.github.abel533.entity.BaseProvider.java

License:Open Source License

/**
 * ?Example - ???Example//  w w w .j  a v  a  2  s  .  c o m
 *
 * @param params
 * @return
 */
protected MetaObject getExample(Map<String, Object> params) {
    Object result = null;
    if (params.containsKey("example")) {
        result = params.get("example");
    }
    if (result == null) {
        return null;
    }
    //?Example???????example
    MetaObject example = MapperTemplate.forObject(result);
    if (example.hasGetter("orderByClause") && example.hasGetter("oredCriteria")
            && example.hasGetter("distinct")) {
        return example;
    }
    throw new IllegalArgumentException("Example???Mybatis Example!");
}

From source file:com.github.pagehelper.util.PageObjectUtil.java

License:Open Source License

/**
 * ??/*from w w  w .j  a v a2 s  .c  o m*/
 *
 * @param paramsObject
 * @param paramName
 * @param required
 * @return
 */
protected static Object getParamValue(MetaObject paramsObject, String paramName, boolean required) {
    Object value = null;
    if (paramsObject.hasGetter(PARAMS.get(paramName))) {
        value = paramsObject.getValue(PARAMS.get(paramName));
    }
    if (value != null && value.getClass().isArray()) {
        Object[] values = (Object[]) value;
        if (values.length == 0) {
            value = null;
        } else {
            value = values[0];
        }
    }
    if (required && value == null) {
        throw new PageException("??:" + PARAMS.get(paramName));
    }
    return value;
}

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

License:Apache License

private void processGeneratedKeys(Executor executor, MappedStatement ms, Object parameter) {
    try {/*from  w  w w. ja va2s .com*/
        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);
    }
}

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

License:Apache License

private void setValue(MetaObject metaParam, String property, Object value) {
    if (metaParam.hasSetter(property)) {
        if (metaParam.hasGetter(property)) {
            Object defaultValue = metaParam.getValue(property);
            if (defaultValue != null) {
                return;
            }//  w  ww  .ja v  a  2 s  .  com
        }
        metaParam.setValue(property, value);
    } else {
        throw new ExecutorException("No setter found for the keyProperty '" + property + "' in "
                + metaParam.getOriginalObject().getClass().getName() + ".");
    }
}

From source file:core.plugin.mybatis.ExecuteSqlLogInterceptor.java

License:Apache License

private static String getRuntimeExeSql(Configuration configuration, BoundSql boundSql) {
    Object parameterObject = boundSql.getParameterObject();
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
    if (parameterMappings.size() > 0 && parameterObject != null) {
        TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
        if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
            sql = sql.replaceFirst("\\?", getParameterValue(parameterObject));
        } else {/* ww  w.  j ava 2  s .c o  m*/
            MetaObject metaObject = configuration.newMetaObject(parameterObject);
            for (ParameterMapping parameterMapping : parameterMappings) {
                String propertyName = parameterMapping.getProperty();
                if (metaObject.hasGetter(propertyName)) {
                    Object obj = metaObject.getValue(propertyName);
                    sql = sql.replaceFirst("\\?", getParameterValue(obj));
                } else if (boundSql.hasAdditionalParameter(propertyName)) {
                    Object obj = boundSql.getAdditionalParameter(propertyName);
                    sql = sql.replaceFirst("\\?", getParameterValue(obj));
                }
            }
        }
    }
    return sql;
}