如果readonly=false的时候,就比较好玩了,更新数据会写入新的cache,并且DO对象要实现序列化
[08/22 11:58:27][main] DEBUG org.apache.ibatis.cache.decorators.LoggingCache55: Cache Hit Ratio [com.xx.dao.UserMapper]: 0.25
[08/22 11:58:27][main] DEBUG org.springframework.jdbc.datasource.DataSourceUtils110: Fetching JDBC Connection from DataSource
/**
* 测试cache的读取和更新
* @author weisong
*
*/
public class MybatisCacheTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring-core.xml","spring-task.xml","spring-jms.xml");
UserMapper u = (UserMapper)ac.getBean("userMapper");
User u0 = u.selectByPrimaryKey(10035L); //sql
System.out.println("u0-name="+u0.getNickname());
User u00 = u.selectByPrimaryKey(10035L); //cache here! DEBUG org.apache.ibatis.cache.decorators.LoggingCache55: Cache Hit Ratio [com.xx.dao.UserMapper]: 0.0
System.out.println("u00(cache)-name="+u00.getNickname());
User u1 = u.selectByPrimaryKey(10036L);
u0.setNickname("wei");
u.updateByPrimaryKey(u0); //update here
User u01 = u.selectByPrimaryKey(10035L);
System.out.println("u01-name="+u01.getNickname()); //should no cache, next will cache
User u11 = u.selectByPrimaryKey(10036L);
System.out.println("u11-name="+u11.getNickname()); //!!!sql , no cache here