Java Code Examples for org.apache.ibatis.mapping.MappedStatement#getCache()

The following examples show how to use org.apache.ibatis.mapping.MappedStatement#getCache() . 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: CachingExecutor.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Override
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)
    throws SQLException {
  Cache cache = ms.getCache();
  //默认情况下是没有开启缓存的(二级缓存).要开启二级缓存,你需要在你的 SQL 映射文件中添加一行: <cache/>
  //简单的说,就是先查CacheKey,查不到再委托给实际的执行器去查
  if (cache != null) {
    flushCacheIfRequired(ms);
    if (ms.isUseCache() && resultHandler == null) {
      ensureNoOutParams(ms, parameterObject, boundSql);
      @SuppressWarnings("unchecked")
      List<E> list = (List<E>) tcm.getObject(cache, key);
      if (list == null) {
        list = delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
        tcm.putObject(cache, key, list); // issue #578 and #116
      }
      return list;
    }
  }
  return delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}
 
Example 2
Source File: MultipleCrossIncludeTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void testMappedStatementCache() throws Exception {
  Reader configReader = Resources
  .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MultipleCrossIncludeMapperConfig.xml");
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
  configReader.close();

  Configuration configuration = sqlSessionFactory.getConfiguration();
  configuration.getMappedStatementNames();
  
  MappedStatement selectPetStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePetMapper.select");
  MappedStatement selectPersonStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePersonMapper.select");
  Cache cache = selectPetStatement.getCache();
  assertEquals("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePetMapper", cache.getId());
  assertSame(cache, selectPersonStatement.getCache());
}
 
Example 3
Source File: XmlExternalRefTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void testMappedStatementCache() throws Exception {
  Reader configReader = Resources
  .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MapperConfig.xml");
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
  configReader.close();

  Configuration configuration = sqlSessionFactory.getConfiguration();
  configuration.getMappedStatementNames();
  
  MappedStatement selectPetStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.PetMapper.select");
  MappedStatement selectPersonStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.PersonMapper.select");
  Cache cache = selectPetStatement.getCache();
  assertEquals("org.apache.ibatis.submitted.xml_external_ref.PetMapper", cache.getId());
  assertSame(cache, selectPersonStatement.getCache());
}
 
Example 4
Source File: CachingExecutor.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Override
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)
    throws SQLException {
  Cache cache = ms.getCache();
  //默认情况下是没有开启缓存的(二级缓存).要开启二级缓存,你需要在你的 SQL 映射文件中添加一行: <cache/>
  //简单的说,就是先查CacheKey,查不到再委托给实际的执行器去查
  if (cache != null) {
    flushCacheIfRequired(ms);
    if (ms.isUseCache() && resultHandler == null) {
      ensureNoOutParams(ms, parameterObject, boundSql);
      @SuppressWarnings("unchecked")
      List<E> list = (List<E>) tcm.getObject(cache, key);
      if (list == null) {
        list = delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
        tcm.putObject(cache, key, list); // issue #578 and #116
      }
      return list;
    }
  }
  return delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}
 
Example 5
Source File: MultipleCrossIncludeTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void testMappedStatementCache() throws Exception {
  Reader configReader = Resources
  .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MultipleCrossIncludeMapperConfig.xml");
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
  configReader.close();

  Configuration configuration = sqlSessionFactory.getConfiguration();
  configuration.getMappedStatementNames();
  
  MappedStatement selectPetStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePetMapper.select");
  MappedStatement selectPersonStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePersonMapper.select");
  Cache cache = selectPetStatement.getCache();
  assertEquals("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePetMapper", cache.getId());
  assertSame(cache, selectPersonStatement.getCache());
}
 
Example 6
Source File: XmlExternalRefTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void testMappedStatementCache() throws Exception {
  Reader configReader = Resources
  .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MapperConfig.xml");
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
  configReader.close();

  Configuration configuration = sqlSessionFactory.getConfiguration();
  configuration.getMappedStatementNames();
  
  MappedStatement selectPetStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.PetMapper.select");
  MappedStatement selectPersonStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.PersonMapper.select");
  Cache cache = selectPetStatement.getCache();
  assertEquals("org.apache.ibatis.submitted.xml_external_ref.PetMapper", cache.getId());
  assertSame(cache, selectPersonStatement.getCache());
}
 
Example 7
Source File: OffsetLimitInterceptor.java    From AsuraFramework with Apache License 2.0 4 votes vote down vote up
public Object intercept(final Invocation invocation) throws Throwable {
       final Executor executor = (Executor) invocation.getTarget();
       final Object[] queryArgs = invocation.getArgs();
       final MappedStatement ms = (MappedStatement)queryArgs[MAPPED_STATEMENT_INDEX];
       final Object parameter = queryArgs[PARAMETER_INDEX];
       final RowBounds rowBounds = (RowBounds)queryArgs[ROWBOUNDS_INDEX];
       final PageBounds pageBounds = new PageBounds(rowBounds);

       if(pageBounds.getOffset() == RowBounds.NO_ROW_OFFSET
               && pageBounds.getLimit() == RowBounds.NO_ROW_LIMIT
               && pageBounds.getOrders().isEmpty()){
           return invocation.proceed();
       }

       final Dialect dialect;
       try {
           Class clazz = Class.forName(dialectClass);
           Constructor constructor = clazz.getConstructor(MappedStatement.class, Object.class, PageBounds.class);
           dialect = (Dialect)constructor.newInstance(new Object[]{ms, parameter, pageBounds});
       } catch (Exception e) {
           throw new ClassNotFoundException("Cannot create dialect instance: "+dialectClass,e);
       }

       final BoundSql boundSql = ms.getBoundSql(parameter);

       queryArgs[MAPPED_STATEMENT_INDEX] = copyFromNewSql(ms,boundSql,dialect.getPageSQL(), dialect.getParameterMappings(), dialect.getParameterObject());
       queryArgs[PARAMETER_INDEX] = dialect.getParameterObject();
       queryArgs[ROWBOUNDS_INDEX] = new RowBounds(RowBounds.NO_ROW_OFFSET,RowBounds.NO_ROW_LIMIT);

       Boolean async = pageBounds.getAsyncTotalCount() == null ? asyncTotalCount : pageBounds.getAsyncTotalCount();
       Future<List> listFuture = call(new Callable<List>() {
           public List call() throws Exception {
               return (List)invocation.proceed();
           }
       }, async);


       if(pageBounds.isContainsTotalCount()){
           Callable<Paginator> countTask = new Callable() {
               public Object call() throws Exception {
                   Integer count;
                   Cache cache = ms.getCache();
                   if(cache != null && ms.isUseCache() && ms.getConfiguration().isCacheEnabled()){
                       CacheKey cacheKey = executor.createCacheKey(ms,parameter,new PageBounds(),copyFromBoundSql(ms,boundSql,dialect.getCountSQL(), boundSql.getParameterMappings(), boundSql.getParameterObject()));
                       count = (Integer)cache.getObject(cacheKey);
                       if(count == null){
                           count = SQLHelp.getCount(ms,executor.getTransaction(),parameter,boundSql,dialect);
                           cache.putObject(cacheKey, count);
                       }
                   }else{
                       count = SQLHelp.getCount(ms,executor.getTransaction(),parameter,boundSql,dialect);
                   }
                   return new Paginator(pageBounds.getPage(), pageBounds.getLimit(), count);
               }
           };
           Future<Paginator> countFutrue = call(countTask, async);
           return new PageList(listFuture.get(),countFutrue.get());
       }

       return listFuture.get();
}
 
Example 8
Source File: CachingExecutor.java    From mybaties with Apache License 2.0 4 votes vote down vote up
private void flushCacheIfRequired(MappedStatement ms) {
  Cache cache = ms.getCache();
  if (cache != null && ms.isFlushCacheRequired()) {      
    tcm.clear(cache);
  }
}
 
Example 9
Source File: CacheOrderTest.java    From mybaties with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldResolveACacheRefNotYetRead() {
  MappedStatement ms = sqlSessionFactory.getConfiguration().getMappedStatement("getUser");
  Cache cache = ms.getCache();
  assertEquals("org.apache.ibatis.submitted.cacheorder.Mapper2", cache.getId());
}
 
Example 10
Source File: CachingExecutor.java    From mybatis with Apache License 2.0 4 votes vote down vote up
private void flushCacheIfRequired(MappedStatement ms) {
  Cache cache = ms.getCache();
  if (cache != null && ms.isFlushCacheRequired()) {      
    tcm.clear(cache);
  }
}
 
Example 11
Source File: CacheOrderTest.java    From mybatis with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldResolveACacheRefNotYetRead() {
  MappedStatement ms = sqlSessionFactory.getConfiguration().getMappedStatement("getUser");
  Cache cache = ms.getCache();
  assertEquals("org.apache.ibatis.submitted.cacheorder.Mapper2", cache.getId());
}