Java Code Examples for org.apache.ibatis.reflection.MetaObject#hasGetter()

The following examples show how to use org.apache.ibatis.reflection.MetaObject#hasGetter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageObjectUtil.java    From sqlhelper with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 从对象中取参数
 *
 * @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;
}
 
Example 2
Source File: MapWrapper.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Override
public boolean hasGetter(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    if (map.containsKey(prop.getIndexedName())) {
      MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
      if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
        return true;
      } else {
        return metaValue.hasGetter(prop.getChildren());
      }
    } else {
      return false;
    }
  } else {
    return map.containsKey(prop.getName());
  }
}
 
Example 3
Source File: BeanWrapper.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Override
public boolean hasGetter(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    if (metaClass.hasGetter(prop.getIndexedName())) {
      MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
      if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
        return metaClass.hasGetter(name);
      } else {
        return metaValue.hasGetter(prop.getChildren());
      }
    } else {
      return false;
    }
  } else {
    return metaClass.hasGetter(name);
  }
}
 
Example 4
Source File: MapWrapper.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Override
public boolean hasGetter(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    if (map.containsKey(prop.getIndexedName())) {
      MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
      if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
        return true;
      } else {
        return metaValue.hasGetter(prop.getChildren());
      }
    } else {
      return false;
    }
  } else {
    return map.containsKey(prop.getName());
  }
}
 
Example 5
Source File: BeanWrapper.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Override
public boolean hasGetter(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    if (metaClass.hasGetter(prop.getIndexedName())) {
      MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
      if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
        return metaClass.hasGetter(name);
      } else {
        return metaValue.hasGetter(prop.getChildren());
      }
    } else {
      return false;
    }
  } else {
    return metaClass.hasGetter(name);
  }
}
 
Example 6
Source File: PageObjectUtil.java    From Mybatis-PageHelper with MIT License 6 votes vote down vote up
/**
 * 从对象中取参数
 *
 * @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;
}
 
Example 7
Source File: MetricInterceptor.java    From mybatis-boost with MIT License 5 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
    BoundSql boundSql = ((StatementHandler) invocation.getTarget()).getBoundSql();

    String sql = boundSql.getSql().replaceAll("\\s*\\n\\s*", " ");
    List<Object> parameters = new ArrayList<>();
    if (configuration.isShowQueryWithParameters()) {
        List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
        Object parameterObject = boundSql.getParameterObject();
        MetaObject metaObject = MyBatisUtils.getMetaObject(parameterObject);
        if (parameterMappings.size() == 1 && !(parameterObject instanceof Map) &&
                !metaObject.hasGetter(parameterMappings.get(0).getProperty())) {
            parameters.add(parameterObject);
        } else {
            parameterMappings.forEach(pm -> parameters.add(metaObject.getValue(pm.getProperty())));
        }
    }

    StopWatch stopWatch = StopWatch.createStarted();
    Object proceed = invocation.proceed();
    long time = stopWatch.getTime();
    if (time > configuration.getSlowQueryThresholdInMillis()) {
        if (parameters.isEmpty()) {
            logger.error(String.format("[SLOW Query took %s ms] %s", time, sql));
        } else {
            logger.error(String.format("[SLOW Query took %s ms, Parameters: %s] %s ", time, parameters, sql));
        }
        BiConsumer<String, Long> slowSqlHandler = configuration.getSlowQueryHandler();
        if (slowSqlHandler != null) {
            slowSqlHandler.accept(sql, time);
        }
    } else if (configuration.isShowQuery()) {
        if (parameters.isEmpty()) {
            logger.info(String.format("[Query took %s ms] %s", time, sql));
        } else {
            logger.info(String.format("[Query took %s ms, Parameters: %s] %s ", time, parameters, sql));
        }
    }
    return proceed;
}
 
Example 8
Source File: SelectKeyGenerator.java    From tk-mybatis with MIT License 5 votes vote down vote up
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;
            }
        }
        metaParam.setValue(property, value);
    } else {
        throw new ExecutorException("No setter found for the keyProperty '" + property + "' in " + metaParam.getOriginalObject().getClass().getName() + ".");
    }
}
 
Example 9
Source File: SqlUtil.java    From genericdao with Artistic License 2.0 5 votes vote down vote up
/**
 * 从对象中取参数
 *
 * @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;
}
 
Example 10
Source File: SelectKeyGenerator.java    From Mapper with MIT License 5 votes vote down vote up
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;
            }
        }
        metaParam.setValue(property, value);
    } else {
        throw new ExecutorException("No setter found for the keyProperty '" + property + "' in " + metaParam.getOriginalObject().getClass().getName() + ".");
    }
}