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

The following examples show how to use org.apache.ibatis.reflection.MetaObject#hasSetter() . 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: MetaHandler.java    From yshopmall with Apache License 2.0 6 votes vote down vote up
/**
 * 更新数据执行
 *
 * @param metaObject
 */
@Override
public void updateFill(MetaObject metaObject) {
    try {
        Timestamp time=new Timestamp(System.currentTimeMillis());
        if (metaObject.hasSetter("updateTime")) {
            log.debug("自动插入 updateTime");
            this.setFieldValByName("updateTime", time, metaObject);
        }
        if (metaObject.hasSetter("updateDate")) {
            log.debug("自动插入 updateDate");
            this.setFieldValByName("updateDate", time, metaObject);
        }
        if (metaObject.hasSetter("delFlag")) {
            log.debug("自动插入 delFlag");
            this.setFieldValByName("delFlag", null, metaObject);
        }
        if (metaObject.hasSetter("createTime")) {
            log.debug("自动插入 createTime");
            this.setFieldValByName("createTime", null, metaObject);
        }
    } catch (Exception e) {
        log.error("自动注入失败:{}", e);
    }
}
 
Example 2
Source File: BeanWrapper.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Override
public boolean hasSetter(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    if (metaClass.hasSetter(prop.getIndexedName())) {
      MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
      if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
        return metaClass.hasSetter(name);
      } else {
        return metaValue.hasSetter(prop.getChildren());
      }
    } else {
      return false;
    }
  } else {
    return metaClass.hasSetter(name);
  }
}
 
Example 3
Source File: AbstractDataSourceFactory.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public void setProperties(final Properties properties) {
    final Properties driverProperties = new Properties();
    final MetaObject metaDataSource = SystemMetaObject.forObject(dataSource);
    for (final Object key : properties.keySet()) {
        final String propertyName = (String) key;
        if (metaDataSource.hasSetter(propertyName)) {
            final String value = (String) properties.get(propertyName);
            /** 对没有设置值的属性跳过设置 */
            if (StringUtils.isNotEmpty(value) && value.startsWith("${") && value.endsWith("}")) {
                continue;
            }

            final Object convertedValue = convertValue(metaDataSource, propertyName, value);
            metaDataSource.setValue(propertyName, convertedValue);
        } else {
            throw new DataSourceException("Unknown DataSource property: " + propertyName);
        }
    }

    if (driverProperties.size() > 0) {
        metaDataSource.setValue("driverProperties", driverProperties);
    }
}
 
Example 4
Source File: BeanWrapper.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Override
public boolean hasSetter(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    if (metaClass.hasSetter(prop.getIndexedName())) {
      MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
      if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
        return metaClass.hasSetter(name);
      } else {
        return metaValue.hasSetter(prop.getChildren());
      }
    } else {
      return false;
    }
  } else {
    return metaClass.hasSetter(name);
  }
}
 
Example 5
Source File: SelectKeyGenerator.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private void setValue(MetaObject metaParam, String property, Object value) {
  if (metaParam.hasSetter(property)) {
    metaParam.setValue(property, value);
  } else {
    throw new ExecutorException("No setter found for the keyProperty '" + property + "' in " + metaParam.getOriginalObject().getClass().getName() + ".");
  }
}
 
Example 6
Source File: MetaHandler.java    From yshopmall with Apache License 2.0 5 votes vote down vote up
/**
 * 新增数据执行
 *
 * @param metaObject
 */
@Override
public void insertFill(MetaObject metaObject) {
    try {
        Timestamp time=new Timestamp(System.currentTimeMillis());
        if (metaObject.hasSetter("createTime")) {
            log.debug("自动插入 createTime");
            this.setFieldValByName("createTime", time, metaObject);
        }
        if (metaObject.hasSetter("updateTime")) {
            log.debug("自动插入 updateTime");
            this.setFieldValByName("updateTime", time, metaObject);
        }
        if (metaObject.hasSetter("createDate")) {
            log.debug("自动插入 createDate");
            this.setFieldValByName("createDate", time, metaObject);
        }
        if (metaObject.hasSetter("updateDate")) {
            log.debug("自动插入 updateDate");
            this.setFieldValByName("updateDate", time, metaObject);
        }
        if (metaObject.hasSetter("delFlag")) {
            log.debug("自动插入 delFlag");
            this.setFieldValByName("delFlag", false, metaObject);
        }
    } catch (Exception e) {
        log.error("自动注入失败:{}", e);
    }
}
 
Example 7
Source File: Jdbc3KeyGenerator.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private TypeHandler<?>[] getTypeHandlers(TypeHandlerRegistry typeHandlerRegistry, MetaObject metaParam, String[] keyProperties) {
  TypeHandler<?>[] typeHandlers = new TypeHandler<?>[keyProperties.length];
  for (int i = 0; i < keyProperties.length; i++) {
    if (metaParam.hasSetter(keyProperties[i])) {
      Class<?> keyPropertyType = metaParam.getSetterType(keyProperties[i]);
      TypeHandler<?> th = typeHandlerRegistry.getTypeHandler(keyPropertyType);
      typeHandlers[i] = th;
    }
  }
  return typeHandlers;
}
 
Example 8
Source File: DefaultResultSetHandler.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private boolean applyAutomaticMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String columnPrefix) throws SQLException {
  final List<String> unmappedColumnNames = rsw.getUnmappedColumnNames(resultMap, columnPrefix);
  boolean foundValues = false;
  for (String columnName : unmappedColumnNames) {
    String propertyName = columnName;
    if (columnPrefix != null && !columnPrefix.isEmpty()) {
      // When columnPrefix is specified,
      // ignore columns without the prefix.
      if (columnName.toUpperCase(Locale.ENGLISH).startsWith(columnPrefix)) {
        propertyName = columnName.substring(columnPrefix.length());
      } else {
        continue;
      }
    }
    final String property = metaObject.findProperty(propertyName, configuration.isMapUnderscoreToCamelCase());
    if (property != null && metaObject.hasSetter(property)) {
      final Class<?> propertyType = metaObject.getSetterType(property);
      if (typeHandlerRegistry.hasTypeHandler(propertyType)) {
        final TypeHandler<?> typeHandler = rsw.getTypeHandler(propertyType, columnName);
        //巧妙的用TypeHandler取得结果
        final Object value = typeHandler.getResult(rsw.getResultSet(), columnName);
        // issue #377, call setter on nulls
        if (value != null || configuration.isCallSettersOnNulls()) {
          if (value != null || !propertyType.isPrimitive()) {
            //然后巧妙的用反射来设置到对象
            metaObject.setValue(property, value);
          }
          foundValues = true;
        }
      }
    }
  }
  return foundValues;
}
 
Example 9
Source File: CacheBuilder.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private void setCacheProperties(Cache cache) {
  if (properties != null) {
    MetaObject metaCache = SystemMetaObject.forObject(cache);
    //用反射设置额外的property属性
    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
      String name = (String) entry.getKey();
      String value = (String) entry.getValue();
      if (metaCache.hasSetter(name)) {
        Class<?> type = metaCache.getSetterType(name);
        //下面就是各种基本类型的判断了,味同嚼蜡但是又不得不写
        if (String.class == type) {
          metaCache.setValue(name, value);
        } else if (int.class == type
            || Integer.class == type) {
          metaCache.setValue(name, Integer.valueOf(value));
        } else if (long.class == type
            || Long.class == type) {
          metaCache.setValue(name, Long.valueOf(value));
        } else if (short.class == type
            || Short.class == type) {
          metaCache.setValue(name, Short.valueOf(value));
        } else if (byte.class == type
            || Byte.class == type) {
          metaCache.setValue(name, Byte.valueOf(value));
        } else if (float.class == type
            || Float.class == type) {
          metaCache.setValue(name, Float.valueOf(value));
        } else if (boolean.class == type
            || Boolean.class == type) {
          metaCache.setValue(name, Boolean.valueOf(value));
        } else if (double.class == type
            || Double.class == type) {
          metaCache.setValue(name, Double.valueOf(value));
        } else {
          throw new CacheException("Unsupported property type for cache: '" + name + "' of type " + type);
        }
      }
    }
  }
}
 
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() + ".");
    }
}
 
Example 11
Source File: SelectKeyGenerator.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private void setValue(MetaObject metaParam, String property, Object value) {
  if (metaParam.hasSetter(property)) {
    metaParam.setValue(property, value);
  } else {
    throw new ExecutorException("No setter found for the keyProperty '" + property + "' in " + metaParam.getOriginalObject().getClass().getName() + ".");
  }
}
 
Example 12
Source File: Jdbc3KeyGenerator.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private TypeHandler<?>[] getTypeHandlers(TypeHandlerRegistry typeHandlerRegistry, MetaObject metaParam, String[] keyProperties) {
  TypeHandler<?>[] typeHandlers = new TypeHandler<?>[keyProperties.length];
  for (int i = 0; i < keyProperties.length; i++) {
    if (metaParam.hasSetter(keyProperties[i])) {
      Class<?> keyPropertyType = metaParam.getSetterType(keyProperties[i]);
      TypeHandler<?> th = typeHandlerRegistry.getTypeHandler(keyPropertyType);
      typeHandlers[i] = th;
    }
  }
  return typeHandlers;
}
 
Example 13
Source File: DefaultResultSetHandler.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private boolean applyAutomaticMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String columnPrefix) throws SQLException {
  final List<String> unmappedColumnNames = rsw.getUnmappedColumnNames(resultMap, columnPrefix);
  boolean foundValues = false;
  for (String columnName : unmappedColumnNames) {
    String propertyName = columnName;
    if (columnPrefix != null && !columnPrefix.isEmpty()) {
      // When columnPrefix is specified,
      // ignore columns without the prefix.
      if (columnName.toUpperCase(Locale.ENGLISH).startsWith(columnPrefix)) {
        propertyName = columnName.substring(columnPrefix.length());
      } else {
        continue;
      }
    }
    final String property = metaObject.findProperty(propertyName, configuration.isMapUnderscoreToCamelCase());
    if (property != null && metaObject.hasSetter(property)) {
      final Class<?> propertyType = metaObject.getSetterType(property);
      if (typeHandlerRegistry.hasTypeHandler(propertyType)) {
        final TypeHandler<?> typeHandler = rsw.getTypeHandler(propertyType, columnName);
        //巧妙的用TypeHandler取得结果
        final Object value = typeHandler.getResult(rsw.getResultSet(), columnName);
        // issue #377, call setter on nulls
        if (value != null || configuration.isCallSettersOnNulls()) {
          if (value != null || !propertyType.isPrimitive()) {
            //然后巧妙的用反射来设置到对象
            metaObject.setValue(property, value);
          }
          foundValues = true;
        }
      }
    }
  }
  return foundValues;
}
 
Example 14
Source File: CacheBuilder.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private void setCacheProperties(Cache cache) {
  if (properties != null) {
    MetaObject metaCache = SystemMetaObject.forObject(cache);
    //用反射设置额外的property属性
    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
      String name = (String) entry.getKey();
      String value = (String) entry.getValue();
      if (metaCache.hasSetter(name)) {
        Class<?> type = metaCache.getSetterType(name);
        //下面就是各种基本类型的判断了,味同嚼蜡但是又不得不写
        if (String.class == type) {
          metaCache.setValue(name, value);
        } else if (int.class == type
            || Integer.class == type) {
          metaCache.setValue(name, Integer.valueOf(value));
        } else if (long.class == type
            || Long.class == type) {
          metaCache.setValue(name, Long.valueOf(value));
        } else if (short.class == type
            || Short.class == type) {
          metaCache.setValue(name, Short.valueOf(value));
        } else if (byte.class == type
            || Byte.class == type) {
          metaCache.setValue(name, Byte.valueOf(value));
        } else if (float.class == type
            || Float.class == type) {
          metaCache.setValue(name, Float.valueOf(value));
        } else if (boolean.class == type
            || Boolean.class == type) {
          metaCache.setValue(name, Boolean.valueOf(value));
        } else if (double.class == type
            || Double.class == type) {
          metaCache.setValue(name, Double.valueOf(value));
        } else {
          throw new CacheException("Unsupported property type for cache: '" + name + "' of type " + type);
        }
      }
    }
  }
}
 
Example 15
Source File: SnowflakeKeyGenerator.java    From spring-data-mybatis with Apache License 2.0 5 votes vote down vote up
private void setValue(MetaObject metaParam, String property, Object value) {
	if (metaParam.hasSetter(property)) {
		metaParam.setValue(property, value);
	}
	else {
		throw new ExecutorException("No setter found for the keyProperty '" + property
				+ "' in " + metaParam.getOriginalObject().getClass().getName() + ".");
	}
}
 
Example 16
Source File: IdentityKeyGenerator.java    From mybatis-jpa with Apache License 2.0 5 votes vote down vote up
private void setValue(MetaObject metaParam, String property, Object value) {
  if (metaParam.hasSetter(property)) {
    metaParam.setValue(property, value);
  } else {
    throw new ExecutorException(
        "No setter found for the keyProperty '" + property + "' in " + metaParam
            .getOriginalObject().getClass().getName() + ".");
  }
}
 
Example 17
Source File: MultipleJdbc3KeyGenerator.java    From tk-mybatis with MIT License 5 votes vote down vote up
private TypeHandler<?>[] getTypeHandlers(TypeHandlerRegistry typeHandlerRegistry, MetaObject metaParam, String[] keyProperties) {
    TypeHandler<?>[] typeHandlers = new TypeHandler<?>[keyProperties.length];
    for (int i = 0; i < keyProperties.length; i++) {
        if (metaParam.hasSetter(keyProperties[i])) {
            Class<?> keyPropertyType = metaParam.getSetterType(keyProperties[i]);
            TypeHandler<?> th = typeHandlerRegistry.getTypeHandler(keyPropertyType);
            typeHandlers[i] = th;
        }
    }
    return typeHandlers;
}
 
Example 18
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 19
Source File: MybatisConfig.java    From SpringBoot2.0 with Apache License 2.0 5 votes vote down vote up
/**
 * 公用字段配置
 */
@Override
public void insertFill(MetaObject metaObject) {
    if (metaObject.hasSetter("creatTime")) {
        metaObject.setValue("creatTime", new Date());
    }
    if (metaObject.hasSetter("updateTime")) {
        metaObject.setValue("updateTime", new Date());
    }
}
 
Example 20
Source File: MybatisConfig.java    From SpringBoot2.0 with Apache License 2.0 4 votes vote down vote up
@Override
public void updateFill(MetaObject metaObject) {
    if (metaObject.hasSetter("updateTime")) {
        metaObject.setValue("updateTime", new Date());
    }
}