org.apache.ibatis.reflection.SystemMetaObject Java Examples

The following examples show how to use org.apache.ibatis.reflection.SystemMetaObject. 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: Example.java    From tk-mybatis with MIT License 6 votes vote down vote up
/**
 * 将此对象的不为空的字段参数作为相等查询条件
 *
 * @param param 参数对象
 * @author Bob {@link}[email protected]
 * @Date 2015年7月17日 下午12:48:08
 */
public Criteria andEqualTo(Object param) {
    MetaObject metaObject = SystemMetaObject.forObject(param);
    String[] properties = metaObject.getGetterNames();
    for (String property : properties) {
        //属性和列对应Map中有此属性
        if (propertyMap.get(property) != null) {
            Object value = metaObject.getValue(property);
            //属性值不为空
            if (value != null) {
                andEqualTo(property, value);
            }
        }
    }
    return (Criteria) this;
}
 
Example #2
Source File: MapWrapper.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Override
public Class<?> getSetterType(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
    if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
      return Object.class;
    } else {
      return metaValue.getSetterType(prop.getChildren());
    }
  } else {
    if (map.get(name) != null) {
      return map.get(name).getClass();
    } else {
      return Object.class;
    }
  }
}
 
Example #3
Source File: MapWrapper.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Override
public Class<?> getGetterType(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
    if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
      return Object.class;
    } else {
      return metaValue.getGetterType(prop.getChildren());
    }
  } else {
    if (map.get(name) != null) {
      return map.get(name).getClass();
    } else {
      return Object.class;
    }
  }
}
 
Example #4
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 #5
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 #6
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 #7
Source File: MapperTemplate.java    From tk-mybatis with MIT License 6 votes vote down vote up
/**
 * 检查是否配置过缓存
 *
 * @param ms
 * @throws Exception
 */
private void checkCache(MappedStatement ms) throws Exception {
    if (ms.getCache() == null) {
        String nameSpace = ms.getId().substring(0, ms.getId().lastIndexOf("."));
        Cache cache;
        try {
            //不存在的时候会抛出异常
            cache = ms.getConfiguration().getCache(nameSpace);
        } catch (IllegalArgumentException e) {
            return;
        }
        if (cache != null) {
            MetaObject metaObject = SystemMetaObject.forObject(ms);
            metaObject.setValue("cache", cache);
        }
    }
}
 
Example #8
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 #9
Source File: Example.java    From tk-mybatis with MIT License 6 votes vote down vote up
/**
 * 将此对象的所有字段参数作为相等查询条件,如果字段为 null,则为 is null
 *
 * @param param 参数对象
 */
public Criteria andAllEqualTo(Object param) {
    MetaObject metaObject = SystemMetaObject.forObject(param);
    String[] properties = metaObject.getGetterNames();
    for (String property : properties) {
        //属性和列对应Map中有此属性
        if (propertyMap.get(property) != null) {
            Object value = metaObject.getValue(property);
            //属性值不为空
            if (value != null) {
                andEqualTo(property, value);
            } else {
                andIsNull(property);
            }
        }
    }
    return (Criteria) this;
}
 
Example #10
Source File: MapWrapper.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Override
public Class<?> getSetterType(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
    if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
      return Object.class;
    } else {
      return metaValue.getSetterType(prop.getChildren());
    }
  } else {
    if (map.get(name) != null) {
      return map.get(name).getClass();
    } else {
      return Object.class;
    }
  }
}
 
Example #11
Source File: MapWrapper.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Override
public Class<?> getGetterType(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
    if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
      return Object.class;
    } else {
      return metaValue.getGetterType(prop.getChildren());
    }
  } else {
    if (map.get(name) != null) {
      return map.get(name).getClass();
    } else {
      return Object.class;
    }
  }
}
 
Example #12
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 #13
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 #14
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 #15
Source File: DataPermissionInterceptor.java    From FEBS-Cloud with Apache License 2.0 6 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
    MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
    this.sqlParser(metaObject);
    MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");

    BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
    Object paramObj = boundSql.getParameterObject();
    // 数据权限只针对查询语句
    if (SqlCommandType.SELECT == mappedStatement.getSqlCommandType()) {
        DataPermission dataPermission = getDataPermission(mappedStatement);
        if (shouldFilter(mappedStatement, dataPermission)) {
            String id = mappedStatement.getId();
            log.info("\n 数据权限过滤 method -> {}", id);
            String originSql = boundSql.getSql();
            String dataPermissionSql = dataPermissionSql(originSql, dataPermission);
            metaObject.setValue("delegate.boundSql.sql", dataPermissionSql);
            log.info("\n originSql -> {} \n dataPermissionSql: {}", originSql, dataPermissionSql);
        }
    }
    return invocation.proceed();
}
 
Example #16
Source File: AbstractSelectMethodBuilder.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
@Override
void setResultType(Configuration configuration, MappedStatement ms, Class<?> entityClass) {
      List<ResultMap> resultMaps = new ArrayList<ResultMap>();
      resultMaps.add(getResultMap(configuration,entityClass));
      MetaObject metaObject = SystemMetaObject.forObject(ms);
      metaObject.setValue("resultMaps", Collections.unmodifiableList(resultMaps));
}
 
Example #17
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 #18
Source File: BeanWrapper.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Override
public Class<?> getSetterType(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
    if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
      return metaClass.getSetterType(name);
    } else {
      return metaValue.getSetterType(prop.getChildren());
    }
  } else {
    return metaClass.getSetterType(name);
  }
}
 
Example #19
Source File: BeanWrapper.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Override
public Class<?> getGetterType(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
    if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
      return metaClass.getGetterType(name);
    } else {
      return metaValue.getGetterType(prop.getChildren());
    }
  } else {
    return metaClass.getGetterType(name);
  }
}
 
Example #20
Source File: BeanWrapper.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Override
public Class<?> getGetterType(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
    if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
      return metaClass.getGetterType(name);
    } else {
      return metaValue.getGetterType(prop.getChildren());
    }
  } else {
    return metaClass.getGetterType(name);
  }
}
 
Example #21
Source File: BeanWrapper.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Override
public Class<?> getSetterType(String name) {
  PropertyTokenizer prop = new PropertyTokenizer(name);
  if (prop.hasNext()) {
    MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
    if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
      return metaClass.getSetterType(name);
    } else {
      return metaValue.getSetterType(prop.getChildren());
    }
  } else {
    return metaClass.getSetterType(name);
  }
}
 
Example #22
Source File: MyBatisUtils.java    From platform with Apache License 2.0 5 votes vote down vote up
/**
 * 获取真实代理对象
 *
 * @param target Object
 * @param <T>    T
 * @return T
 */
public static Object getTarget(Object target) {
    if (Proxy.isProxyClass(target.getClass())) {
        MetaObject metaObject = SystemMetaObject.forObject(target);
        return getTarget(metaObject.getValue("h.target"));
    }
    return target;
}
 
Example #23
Source File: MyBatisUtils.java    From platform with Apache License 2.0 5 votes vote down vote up
/**
 * 获取真实代理对象
 *
 * @param target Object
 * @param <T>    T
 * @return T
 */
public static <T> T getRealTarget(Object target) {
    if (Proxy.isProxyClass(target.getClass())) {
        MetaObject metaObject = SystemMetaObject.forObject(target);
        return getRealTarget(metaObject.getValue("h.target"));
    }
    return (T) target;
}
 
Example #24
Source File: MapperTemplate.java    From tk-mybatis with MIT License 5 votes vote down vote up
/**
 * 设置返回值类型 - 为了让typeHandler在select时有效,改为设置resultMap
 *
 * @param ms
 * @param entityClass
 */
protected void setResultType(MappedStatement ms, Class<?> entityClass) {
    EntityTable entityTable = EntityHelper.getEntityTable(entityClass);
    List<ResultMap> resultMaps = new ArrayList<ResultMap>();
    resultMaps.add(entityTable.getResultMap(ms.getConfiguration()));
    MetaObject metaObject = SystemMetaObject.forObject(ms);
    metaObject.setValue("resultMaps", Collections.unmodifiableList(resultMaps));
}
 
Example #25
Source File: MybatisUtil.java    From Milkomeda with MIT License 5 votes vote down vote up
/**
 * 获得真正的处理对象,可能多层代理
 * @param target    代理目标
 * @param <T>       目标类型
 * @return  被代理的真实对象
 */
@SuppressWarnings("unchecked")
public static <T> T realTarget(Object target) {
    if (Proxy.isProxyClass(target.getClass())) {
        MetaObject metaObject = SystemMetaObject.forObject(target);
        return realTarget(metaObject.getValue("h.target"));
    }
    return (T) target;
}
 
Example #26
Source File: DataScopeInterceptor.java    From MeetingFilm with Apache License 2.0 5 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
    MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
    MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");

    if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
        return invocation.proceed();
    }

    BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
    String originalSql = boundSql.getSql();
    Object parameterObject = boundSql.getParameterObject();

    //查找参数中包含DataScope类型的参数
    DataScope dataScope = findDataScopeObject(parameterObject);

    if (dataScope == null) {
        return invocation.proceed();
    } else {
        String scopeName = dataScope.getScopeName();
        List<Integer> deptIds = dataScope.getDeptIds();
        String join = CollectionKit.join(deptIds, ",");
        originalSql = "select * from (" + originalSql + ") temp_data_scope where temp_data_scope." + scopeName + " in (" + join + ")";
        metaStatementHandler.setValue("delegate.boundSql.sql", originalSql);
        return invocation.proceed();
    }
}
 
Example #27
Source File: DataScopeInterceptor.java    From code with Apache License 2.0 5 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler statementHandler = (StatementHandler) realTarget(invocation.getTarget());
    MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
    MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");

    if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
        return invocation.proceed();
    }

    BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
    String originalSql = boundSql.getSql();
    Object parameterObject = boundSql.getParameterObject();

    //查找参数中包含DataScope类型的参数
    DataScope dataScope = findDataScopeObject(parameterObject);

    if (dataScope == null) {
        return invocation.proceed();
    } else {
        String scopeName = dataScope.getScopeName();
        List<Integer> deptIds = dataScope.getDeptIds();
        String join = join(deptIds, ",");
        originalSql = "select * from (" + originalSql + ") temp_data_scope where temp_data_scope." + scopeName + " in (" + join + ")";

        metaStatementHandler.setValue("delegate.boundSql.sql", originalSql);
        return invocation.proceed();
    }
}
 
Example #28
Source File: DataScopeInterceptor.java    From code with Apache License 2.0 5 votes vote down vote up
private static Object realTarget(Object target) {
    if (Proxy.isProxyClass(target.getClass())) {
        MetaObject metaObject = SystemMetaObject.forObject(target);
        return realTarget(metaObject.getValue("h.target"));
    } else {
        return target;
    }
}
 
Example #29
Source File: MetaObjectAssistant.java    From Aooms with Apache License 2.0 5 votes vote down vote up
public static Object realTarget(Object target) {
    if (Proxy.isProxyClass(target.getClass())) {
        MetaObject metaObject = SystemMetaObject.forObject(target);
        return realTarget(metaObject.getValue("h.target"));
    } else {
        return target;
    }
}
 
Example #30
Source File: DataScopeInterceptor.java    From pig with MIT License 5 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
    MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
    this.sqlParser(metaObject);
    // 先判断是不是SELECT操作
    MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
    if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
        return invocation.proceed();
    }

    BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
    String originalSql = boundSql.getSql();
    Object parameterObject = boundSql.getParameterObject();

    //查找参数中包含DataScope类型的参数
    DataScope dataScope = findDataScopeObject(parameterObject);

    if (dataScope == null) {
        return invocation.proceed();
    } else {
        String scopeName = dataScope.getScopeName();
        List<Integer> deptIds = dataScope.getDeptIds();
        if(StrUtil.isNotBlank(scopeName) && CollectionUtil.isNotEmpty(deptIds)){
            String join = CollectionUtil.join(deptIds, ",");
            originalSql = "select * from (" + originalSql + ") temp_data_scope where temp_data_scope." + scopeName + " in (" + join + ")";
            metaObject.setValue("delegate.boundSql.sql", originalSql);
        }
        return invocation.proceed();
    }
}