@Test // 测试自动映射 public void testAutoMapping() throws IOException { // 2.获取sqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); // 3.获取对应mapper TUserMapper mapper = sqlSession.getMapper(TUserMapper.class); // 4.执行查询语句并返回结果 TUser user = mapper.selectByPrimaryKey(1); TUser user2 = mapper.selectByPrimaryKey(1); TUser user3 = mapper.selectByPrimaryKey(1); TUser user4 = mapper.selectByPrimaryKey(1); System.out.println(user == user4); }
2020-10-22 19:45:46.216 [main] DEBUG c.e.mybatis.mapper.TUserMapper.selectByPrimaryKey - <== Total: 1
true
一级缓存 是 sqlSession的, 一级 缓存是线程级别的
优先从 二级缓存【线程共享缓存】 查找,然后再查找一级缓存【线程独享】, 没有,然后再查找数据库
多个 mapper 可以通过 配置 cache-ref 配置同一个缓存数据源
使用二级缓存,容易脏读,没有提交的数据被回滚了,你就用了没有提交的脏数据,一般用专业的第三方缓存根据在业务层控制
原文:https://www.cnblogs.com/lyr-2000/p/13860719.html