问题描述
我正在使用MyBatis,并通过xml映射器文件中的<cache/>
激活了二级缓存.
I'm using MyBatis with second level cache activated via <cache/>
in xml mapper files.
假设我想与从MyBatis分离的底层数据库/数据源进行交互,例如通过直接jdbcTemplate.
Suppose I want to interact with the underlying DB/DataSource decoupled from MyBatis, for instance via direct jdbcTemplate.
当我通过jdbcTemplate插入/更新/删除MyBatis保存查询结果的表时,如何确保MyBatis缓存被适当刷新.
How can I assure, that the MyBatis cache gets flushed appropriateley when I Insert/Update/Delete via jdbcTemplate on a table for that MyBatis holds cached query results.
换句话说,对于某些缓存名称空间,如何强制MyBatis从MyBatis映射器外部刷新其缓存?
In other words, how can I force MyBatis to flush its cache from outside of MyBatis mappers for certain cache namespace?
我知道@Options(flushCache=true)
批注,但这似乎在mapper界面之外不起作用.
I'm aware of @Options(flushCache=true)
annotation, but this seems not to work outside of mapper interfaces.
推荐答案
您可以从配置中获取缓存,然后按名称空间获取并清除它.
you can get cache from configuration and then get by namespace and clear it.
@Resource
SqlSessionFactory sqlSessionFactory;
public void clearCacheByNamespace(){
Configuration config = sqlSessionFactory.getConfiguration();
Cache cache = config.getCache("com.persia.dao.UserInfoMapper");
if(cache != null){
cache.clear();
}
}
这篇关于从外部(在映射器外部)刷新MyBatis缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!