Search in sources :

Example 31 with MetaObject

use of org.apache.ibatis.reflection.MetaObject in project mybatis.flying by limeng32.

the class AutoMapperInterceptor method getRealObj.

private MetaObject getRealObj(Object obj) {
    MetaObject metaStatement = MetaObject.forObject(obj, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, DEFAULT_REFLECTOR_FACTORY);
    // 分离代理对象链(由于目标类可能被多个拦截器拦截,从而形成多次代理,通过下面的两次循环可以分离出最原始的的目标类)
    while (metaStatement.hasGetter("h")) {
        Object object = metaStatement.getValue("h");
        metaStatement = MetaObject.forObject(object, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, DEFAULT_REFLECTOR_FACTORY);
    }
    // 分离最后一个代理对象的目标类
    while (metaStatement.hasGetter("target")) {
        Object object = metaStatement.getValue("target");
        metaStatement = MetaObject.forObject(object, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, DEFAULT_REFLECTOR_FACTORY);
    }
    return metaStatement;
}
Also used : MetaObject(org.apache.ibatis.reflection.MetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject)

Example 32 with MetaObject

use of org.apache.ibatis.reflection.MetaObject in project mybatis.flying by limeng32.

the class EnhancedCachingInterceptor method processQuery.

/**
 * when executing a query operation 1. record this statement's id and it's
 * corresponding Cache Object into Global Caching Manager; 2. record this
 * statement's id and
 *
 * @param invocation
 * @return
 * @throws Throwable
 */
protected Object processQuery(Invocation invocation) throws Throwable {
    Object result = invocation.proceed();
    if (cachingManager.isCacheEnabled()) {
        Object[] args = invocation.getArgs();
        MappedStatement mappedStatement = (MappedStatement) args[0];
        // 如果本条statementId表示的查询语句配置了 flushCache=true,则清空querCacheOnCommit缓存
        if (mappedStatement.isFlushCacheRequired()) {
            queryCacheOnCommit.clear();
        }
        // 和对应的二级缓存对象映射关系添加到全局缓存映射管理器中
        if (mappedStatement.isUseCache() && mappedStatement.getCache() != null) {
            cachingManager.appendStatementCacheMap(mappedStatement.getId(), mappedStatement.getCache());
        }
        Object parameter = args[1];
        RowBounds rowBounds = (RowBounds) args[2];
        Executor executor = (Executor) invocation.getTarget();
        BoundSql boundSql = mappedStatement.getBoundSql(parameter);
        FlyingModel flyingModel = CookOriginalSql.fetchFlyingFeature(boundSql.getSql());
        // 记录本次查询所产生的CacheKey
        CacheKey cacheKey = createCacheKey(mappedStatement, parameter, rowBounds, boundSql, executor);
        if (flyingModel.isHasFlyingFeature()) {
            switch(flyingModel.getActionType()) {
                case count:
                    cacheKey.update(DigestUtils.md5Hex(JSON.toJSONString(parameter)));
                    break;
                case selectAll:
                    cacheKey.update(DigestUtils.md5Hex(JSON.toJSONString(parameter)));
                    break;
                case selectOne:
                    cacheKey.update(DigestUtils.md5Hex(JSON.toJSONString(parameter)));
                    break;
                default:
                    break;
            }
        }
        queryCacheOnCommit.putElement(mappedStatement.getId(), cacheKey);
        /* 处理分页 */
        ResultHandler resultHandler = (ResultHandler) args[3];
        Executor executorProxy = (Executor) invocation.getTarget();
        MetaObject metaParameter = MetaObject.forObject(parameter, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, DEFAULT_REFLECTOR_FACTORY);
        MetaObject metaExecutor = MetaObject.forObject(executorProxy, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, DEFAULT_REFLECTOR_FACTORY);
        /* 当需要分页查询时,缓存里加入page信息 */
        // if (metaParameter.getOriginalObject() instanceof Conditionable) {
        Cache cache = mappedStatement.getCache();
        Object value = cache.getObject(cacheKey);
        if (metaExecutor.hasGetter("delegate")) {
            TransactionalCacheManager tcm = (TransactionalCacheManager) metaExecutor.getValue("tcm");
            Executor delegate = (Executor) metaExecutor.getValue("delegate");
            Object list = delegate.query(mappedStatement, parameter, rowBounds, resultHandler, cacheKey, boundSql);
            if (value != null) {
                return value;
            } else {
                tcm.putObject(cache, cacheKey, list);
                return list;
            }
        }
    // }
    }
    return result;
}
Also used : FlyingModel(indi.mybatis.flying.models.FlyingModel) Executor(org.apache.ibatis.executor.Executor) BoundSql(org.apache.ibatis.mapping.BoundSql) MetaObject(org.apache.ibatis.reflection.MetaObject) RowBounds(org.apache.ibatis.session.RowBounds) MetaObject(org.apache.ibatis.reflection.MetaObject) MappedStatement(org.apache.ibatis.mapping.MappedStatement) ResultHandler(org.apache.ibatis.session.ResultHandler) TransactionalCacheManager(org.apache.ibatis.cache.TransactionalCacheManager) CacheKey(org.apache.ibatis.cache.CacheKey) Cache(org.apache.ibatis.cache.Cache)

Example 33 with MetaObject

use of org.apache.ibatis.reflection.MetaObject in project paascloud-master by paascloud.

the class SqlLogInterceptor method getParamList.

/**
 * 获取sql参数集合。
 *
 * @param configuration the configuration
 * @param boundSql      the bound sql
 *
 * @return the param list
 */
private List<String> getParamList(Configuration configuration, BoundSql boundSql) {
    Object parameterObject = boundSql.getParameterObject();
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    List<String> params = new ArrayList<>();
    if (parameterMappings.size() > 0 && parameterObject != null) {
        TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
        if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
            params.add(getParameterValue(parameterObject));
        } else {
            MetaObject metaObject = configuration.newMetaObject(parameterObject);
            for (ParameterMapping parameterMapping : parameterMappings) {
                String propertyName = parameterMapping.getProperty();
                if (metaObject.hasGetter(propertyName)) {
                    Object obj = metaObject.getValue(propertyName);
                    params.add(getParameterValue(obj));
                } else if (boundSql.hasAdditionalParameter(propertyName)) {
                    Object obj = boundSql.getAdditionalParameter(propertyName);
                    params.add(getParameterValue(obj));
                }
            }
        }
    }
    return params;
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MetaObject(org.apache.ibatis.reflection.MetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject)

Aggregations

MetaObject (org.apache.ibatis.reflection.MetaObject)33 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)9 ExecutorException (org.apache.ibatis.executor.ExecutorException)6 CacheKey (org.apache.ibatis.cache.CacheKey)5 SystemMetaObject (org.apache.ibatis.reflection.SystemMetaObject)5 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)5 SQLException (java.sql.SQLException)4 Configuration (org.apache.ibatis.session.Configuration)4 TypeHandler (org.apache.ibatis.type.TypeHandler)4 BoundSql (org.apache.ibatis.mapping.BoundSql)3 MappedStatement (org.apache.ibatis.mapping.MappedStatement)3 ResultMapping (org.apache.ibatis.mapping.ResultMapping)3 AutoMapperException (indi.mybatis.flying.exception.AutoMapperException)2 Conditionable (indi.mybatis.flying.models.Conditionable)2 FlyingModel (indi.mybatis.flying.models.FlyingModel)2 ResultSet (java.sql.ResultSet)2 CacheException (org.apache.ibatis.cache.CacheException)2 Executor (org.apache.ibatis.executor.Executor)2 ResultLoaderMap (org.apache.ibatis.executor.loader.ResultLoaderMap)2 JdbcType (org.apache.ibatis.type.JdbcType)2