从MyBatis缓存中删除特定的选择ID数据

时间:2016-02-15 10:03:53

标签: mybatis ibatis spring-mybatis

<mapper namespace="src.main.domain.EqMapper">
    <cache eviction="FIFO" size="512" readOnly="true"/>

    <select id="getStoreIdAndEqId" resultType="String" flushCache="false" useCache="true">
        select count(author) from blog
    </select>
    <select id="getWholeData" resultType="java.util.LinkedHashMap" flushCache="false" useCache="true">
        select * from blog
    </select>
</mapper>

Configuration configuration = MyBatisUtil.getSqlSessionFactory().getConfiguration(); 
Collection<Cache> caches = configuration.getCaches(); 
for (Cache cache : caches) { 
    Lock w = cache.getReadWriteLock().writeLock(); 
    w.lock(); 
    try { 
        cache.clear(); 
    } finally { 
        w.unlock(); 
    } 
} 

上述缓存逻辑清除所有缓存。是否可以清除特定的缓存?我想删除getStoreIdAndEqId缓存而不是getWholeData缓存。

1 个答案:

答案 0 :(得分:0)

试试这个:

<select id="getStoreIdAndEqId" resultType="String" flushCache="true" useCache="false">
     select count(author) from blog
</select>
<select id="getWholeData" resultType="java.util.LinkedHashMap" flushCache="false" useCache="true">
     select * from blog
</select>