See this code
public static void main(String[] args) throws IOException {
InputStream resource = Resources.getResourceAsStream("mybatis/config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
SqlSession sqlSession = sqlSessionFactory.openSession(false);
BasicMapper mapper = sqlSession.getMapper(BasicMapper.class);
System.out.println(mapper.queryName(1));
System.out.println(mapper.queryName(2));
System.out.println(mapper.queryName(3));
//three data has been cached
sqlSession.commit();
//this query hit cache
System.out.println(mapper.queryName(2));
//but update will flush cache and all of cache will be clear after this statement
mapper.updateName(1,"liuyu");
sqlSession.commit();
//so mybatis need to query from database again even this data doesn't change
System.out.println(mapper.queryName(2));
sqlSession.close();
}
Reason
mybatis has no fine-grained control over caching,so i think we can remove cache system in mybatis.
Comment From: harawata
We cannot remove an existing feature just because someone thinks it's useless. :) There are many users who are using cache.
Comment From: liuyueve
Sorry ,i don't mean to remove it now .just want to make a discussion.Or we can use it better on another way?
Comment From: harawata
Hello @liuyueve ,
What you expect is called 'entity cache' (I think) and MyBatis does not (and will not) support it because MyBatis is a SQL mapper. If your project requires a library that supports entity cache, you should look for an ORM like JPA/Hibernate.