Flush MyBatis Cache externally (outside of mapper)

血红的双手。 提交于 2019-12-04 14:40:12

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();
        }
    }

Hi i have used another approach, because we used spring. Use autowire the Session implementation and call appropriate method

public class SomeServerClass{

    @Autowired
    private org.mybatis.spring.SqlSessionTemplate sqlSessionTemplate;

    private void someClearMethod(){
        sqlSessionTemplate.clearCache();
    }
}

If I use interface org.apache.ibatis.session.SqlSession it refers to same instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!