先说结论:
Mybatis默认开启了一级缓存,在同一个会话Session中,同一个sql的查询结果会有缓存,会导致取出来的是同一个实体。更新,删除数据会清除这个sql缓存,重新从数据库获取,此时会根据sql的缓存机制来处理。
例子:
BService
public void testB(Long id){
TestPO test1 = testDao.getById(id);
test1.setName("小明");
}
AService
@Autowired
private BService bservice;
@Transactional(rollbackFor = Exception.class)
public void testA(Long id){
TestPO test = testDao.getById(id);
log.info(test.getName());//小黄
bservice.testB(id);
log.info(test.getName());//小明
}
原本的小黄变成了小明