Search in sources :

Example 6 with FlyingModel

use of indi.mybatis.flying.models.FlyingModel 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 7 with FlyingModel

use of indi.mybatis.flying.models.FlyingModel in project mybatis.flying by limeng32.

the class CookOriginalSqlTest method testCook1.

@Test
public void testCook1() {
    String sql = "flying?:select:noPassword";
    FlyingModel flyingModel = CookOriginalSql.fetchFlyingFeature(sql);
    Assert.assertTrue(flyingModel.isHasFlyingFeature());
    Assert.assertEquals(ActionType.select, flyingModel.getActionType());
    Assert.assertEquals("noPassword", flyingModel.getIgnoreTag());
}
Also used : FlyingModel(indi.mybatis.flying.models.FlyingModel) Test(org.junit.Test)

Aggregations

FlyingModel (indi.mybatis.flying.models.FlyingModel)7 Test (org.junit.Test)4 BoundSql (org.apache.ibatis.mapping.BoundSql)2 MappedStatement (org.apache.ibatis.mapping.MappedStatement)2 MetaObject (org.apache.ibatis.reflection.MetaObject)2 AutoMapperException (indi.mybatis.flying.exception.AutoMapperException)1 MilliSecondKeyHandler (indi.mybatis.flying.handlers.MilliSecondKeyHandler)1 SnowFlakeKeyHandler (indi.mybatis.flying.handlers.SnowFlakeKeyHandler)1 UuidKeyHandler (indi.mybatis.flying.handlers.UuidKeyHandler)1 UuidWithoutLineKeyHandler (indi.mybatis.flying.handlers.UuidWithoutLineKeyHandler)1 Conditionable (indi.mybatis.flying.models.Conditionable)1 ActionType (indi.mybatis.flying.statics.ActionType)1 KeyGeneratorType (indi.mybatis.flying.statics.KeyGeneratorType)1 KeyHandler (indi.mybatis.flying.type.KeyHandler)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 DataSource (javax.sql.DataSource)1 Cache (org.apache.ibatis.cache.Cache)1 CacheKey (org.apache.ibatis.cache.CacheKey)1