Metrics
0
Watch 630 Star 1.9K Fork 623

abel533 / MapperJavaMIT

Open
!13 在update操作上上强制刷新缓存

张廷:developabel533:develop

张廷 Created on: 2018-01-11 15:21
Reviewer: 8363_free   Tester: 8363_free

基于mybatis对于二级缓存的支持是建立在namespace上的,默认在insert、delete、update时都会刷新缓存。

但在这两个接口上标注了@Options,但flushCache默认为false,这对于使用了二级缓存的人来说无非造成了很大的困扰,更新数据后缓存并未刷新,查询时却拿到了之前的脏数据,希望能将此接口做出调整,避免带来不必要的麻烦

4 comments, 3 participants 106991_zhangting 8363_free 25973_64205113

This Pull Request must review before merge. Current status: Unchecked Can auto merge Can not auto merge
8363_free
/free 2018-01-13 21:53 owner

。。先看到的回复,然后才是PR。。

8363_free
/free 2018-01-13 21:54 owner

flushCache = true 这个配置是和版本有关的,在3.4.3之前确实有问题。但是之后就变成了下面这样:

/**
 * @author Clinton Begin
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Options {
  /**
   * The options for the {@link Options#flushCache()}.
   * The default is {@link FlushCachePolicy#DEFAULT}
   */
  public enum FlushCachePolicy {
    /** <code>false</code> for select statement; <code>true</code> for insert/update/delete statement. */
    DEFAULT,
    /** Flushes cache regardless of the statement type. */
    TRUE,
    /** Does not flush cache regardless of the statement type. */
    FALSE
  }

  boolean useCache() default true;

  FlushCachePolicy flushCache() default FlushCachePolicy.DEFAULT;

  ResultSetType resultSetType() default ResultSetType.FORWARD_ONLY;

  StatementType statementType() default StatementType.PREPARED;

  int fetchSize() default -1;

  int timeout() default -1;

  boolean useGeneratedKeys() default false;

  String keyProperty() default "id";

  String keyColumn() default "";
  
  String resultSets() default "";
}

所以如果使用更新的版本是不存在这个问题的。

25973_64205113
/64205113 2018-01-16 16:43

@abel533 缓存还有个问题,我在Dao类上加注解org.apache.ibatis.annotations.CacheNamespace,Mapper生成的方法可以触发缓存,但我自己在xml中写的SQL不触发缓存。同样的,我在xml文件中启用,则自己在xml中写的sql可以触发缓存,但Mapper生成的方法不触发缓存。 如果我同时使用注解和,MyBatis启动会报错:“Caches collection already contains value for”,请问怎么解决此问题?

106991_zhangting
/zhangting 2018-01-18 13:44

好的,那我更新下版本看看。

Sign in and comment

src/main/java/tk/mybatis/mapper/common/base/update/UpdateByPrimaryKeyMapper.java
@@ -43,7 +43,7 @@ public interface UpdateByPrimaryKeyMapper<T> {
* @return
*/
@UpdateProvider(type = BaseUpdateProvider.class, method = "dynamicSQL")
@Options(useCache = false, useGeneratedKeys = false)
@Options(useCache = false, useGeneratedKeys = false, flushCache = true)
int updateByPrimaryKey(T record);
}
\ No newline at end of file
src/main/java/tk/mybatis/mapper/common/base/update/UpdateByPrimaryKeySelectiveMapper.java
@@ -43,7 +43,7 @@ public interface UpdateByPrimaryKeySelectiveMapper<T> {
* @return
*/
@UpdateProvider(type = BaseUpdateProvider.class, method = "dynamicSQL")
@Options(useCache = false, useGeneratedKeys = false)
@Options(useCache = false, useGeneratedKeys = false, flushCache = true)
int updateByPrimaryKeySelective(T record);
}
\ No newline at end of file

Help Search

Gitee_sixth 5th_float_left_close