参考:
https://blog.csdn.net/HelloWorld20161112/article/details/136262676
MyBatis一级缓存的作用域是同一个SqlSession,在同一个SqlSession中执行两次相同的查询,第一次执行完毕后,Mybatis会将查询到的数据缓存起来(缓存到内存中), 第二次执行相同的查询时,会从缓存中取数据,不再进行数据库的查询,从而提高查询效率。默认情况下,Mybatis开启了一级缓存。但是MyBatis整合Spring Boot时,一级缓存却没有生效。
使用方式1
@Test
public void testLevel1Cache2() {
DoubanInfo doubanInfo = doubanInfoMapper.selectDoubanInfoById(8L);
System.out.println(doubanInfo);
DoubanInfo doubanInfo2 = doubanInfoMapper.selectDoubanInfoById(8L);
System.out.println(doubanInfo2);
}
会出现2条sql查询:
18:16:52.382 [main] DEBUG c.r.m.m.D.selectDoubanInfoById - [debug,137] - ==> Preparing: select id, subject_id, score,summary, image_url, rating_count, do_count, collect_count, wish_count, title, alias, year, genres, tags, director, actors,episodes_count, status, del_flag, create_by, create_time, update_by, update_time, remark from douban_info where id = ?
18:16:52.384 [main] DEBUG c.r.m.m.D.selectDoubanInfoById - [debug,137] - ==> Parameters: 8(Long)
18:16:52.396 [main] DEBUG c.r.m.m.D.selectDoubanInfoById - [debug,137] - <== Total: 1
DoubanInfo(id=8, subjectId=34902572, score=8.3, summary=讲述七个经常喝得烂醉的二流货色,他们组成名为「机械之声」的小队。过去这七人只在乎赚快钱及啤酒,但当他们发现家乡Emon市正面临一只可怕怪物的袭击时,他们意识到自己就是唯一能对抗怪物的人。, image=null, imageUrl=https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2844258904.jpg, ratingCount=1824, doCount=145, collectCount=1967, wishCount=3932, title=机械之声的传奇 第一季, episodesCount=12, alias=null, year=2022, genres=喜剧|动作|动画, tags=动画|美国|奇幻|2022|动作|喜剧|美剧|冒险|动漫|魔幻, director=, actors=艾什莉·约翰逊|劳拉·贝莉|特拉维斯·威林厄姆|连姆·奥布赖恩, status=1, delFlag=0)
18:16:52.410 [main] DEBUG c.r.m.m.D.selectDoubanInfoById - [debug,137] - ==> Preparing: select id, subject_id, score,summary, image_url, rating_count, do_count, collect_count, wish_count, title, alias, year, genres, tags, director, actors,episodes_count, status, del_flag, create_by, create_time, update_by, update_time, remark from douban_info where id = ?
18:16:52.411 [main] DEBUG c.r.m.m.D.selectDoubanInfoById - [debug,137] - ==> Parameters: 8(Long)
18:16:52.426 [main] DEBUG c.r.m.m.D.selectDoubanInfoById - [debug,137] - <== Total: 1
DoubanInfo(id=8, subjectId=34902572, score=8.3, summary=讲述七个经常喝得烂醉的二流货色,他们组成名为「机械之声」的小队。过去这七人只在乎赚快钱及啤酒,但当他们发现家乡Emon市正面临一只可怕怪物的袭击时,他们意识到自己就是唯一能对抗怪物的人。, image=null, imageUrl=https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2844258904.jpg, ratingCount=1824, doCount=145, collectCount=1967, wishCount=3932, title=机械之声的传奇 第一季, episodesCount=12, alias=null, year=2022, genres=喜剧|动作|动画, tags=动画|美国|奇幻|2022|动作|喜剧|美剧|冒险|动漫|魔幻, director=, actors=艾什莉·约翰逊|劳拉·贝莉|特拉维斯·威林厄姆|连姆·奥布赖恩, status=1, delFlag=0)
失效原因
当同一个线程开启事务时,同一个sql查询多次会走一级缓存,而不开启事务时,每一查询都是不同的sqlsession,即缓存为“失效”状态 。
解决
添加 @Transactional 注解。
代码
@Test
@Transactional
public void testLevel1Cache2() {
DoubanInfo doubanInfo = doubanInfoMapper.selectDoubanInfoById(8L);
System.out.println(doubanInfo);
DoubanInfo doubanInfo2 = doubanInfoMapper.selectDoubanInfoById(8L);
System.out.println(doubanInfo2);
}
log
18:17:49.668 [main] DEBUG c.r.m.m.D.selectDoubanInfoById - [debug,137] - ==> Preparing: select id, subject_id, score,summary, image_url, rating_count, do_count, collect_count, wish_count, title, alias, year, genres, tags, director, actors,episodes_count, status, del_flag, create_by, create_time, update_by, update_time, remark from douban_info where id = ?
18:17:49.670 [main] DEBUG c.r.m.m.D.selectDoubanInfoById - [debug,137] - ==> Parameters: 8(Long)
18:17:49.683 [main] DEBUG c.r.m.m.D.selectDoubanInfoById - [debug,137] - <== Total: 1
DoubanInfo(id=8, subjectId=34902572, score=8.3, summary=讲述七个经常喝得烂醉的二流货色,他们组成名为「机械之声」的小队。过去这七人只在乎赚快钱及啤酒,但当他们发现家乡Emon市正面临一只可怕怪物的袭击时,他们意识到自己就是唯一能对抗怪物的人。, image=null, imageUrl=https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2844258904.jpg, ratingCount=1824, doCount=145, collectCount=1967, wishCount=3932, title=机械之声的传奇 第一季, episodesCount=12, alias=null, year=2022, genres=喜剧|动作|动画, tags=动画|美国|奇幻|2022|动作|喜剧|美剧|冒险|动漫|魔幻, director=, actors=艾什莉·约翰逊|劳拉·贝莉|特拉维斯·威林厄姆|连姆·奥布赖恩, status=1, delFlag=0)
DoubanInfo(id=8, subjectId=34902572, score=8.3, summary=讲述七个经常喝得烂醉的二流货色,他们组成名为「机械之声」的小队。过去这七人只在乎赚快钱及啤酒,但当他们发现家乡Emon市正面临一只可怕怪物的袭击时,他们意识到自己就是唯一能对抗怪物的人。, image=null, imageUrl=https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2844258904.jpg, ratingCount=1824, doCount=145, collectCount=1967, wishCount=3932, title=机械之声的传奇 第一季, episodesCount=12, alias=null, year=2022, genres=喜剧|动作|动画, tags=动画|美国|奇幻|2022|动作|喜剧|美剧|冒险|动漫|魔幻, director=, actors=艾什莉·约翰逊|劳拉·贝莉|特拉维斯·威林厄姆|连姆·奥布赖恩, status=1, delFlag=0)
使用方式2:使用sqlSessionFactory单独开session
@Test
public void testLevel1Cache() {
SqlSession sqlSession = sqlSessionFactory.openSession();
DoubanInfoMapper mapper = sqlSession.getMapper(DoubanInfoMapper.class);
DoubanInfo doubanInfo = mapper.selectDoubanInfoById(8L);
System.out.println(doubanInfo);
DoubanInfo doubanInfo2 = mapper.selectDoubanInfoById(8L);
System.out.println(doubanInfo2);
}
使用sqlSessionFactory创建一个sqlSession,该sqlSession是带有一级缓存的。
18:19:03.545 [main] DEBUG c.r.m.m.D.selectDoubanInfoById - [debug,137] - ==> Preparing: select id, subject_id, score,summary, image_url, rating_count, do_count, collect_count, wish_count, title, alias, year, genres, tags, director, actors,episodes_count, status, del_flag, create_by, create_time, update_by, update_time, remark from douban_info where id = ?
18:19:03.548 [main] DEBUG c.r.m.m.D.selectDoubanInfoById - [debug,137] - ==> Parameters: 8(Long)
18:19:03.566 [main] DEBUG c.r.m.m.D.selectDoubanInfoById - [debug,137] - <== Total: 1
DoubanInfo(id=8, subjectId=34902572, score=8.3, summary=讲述七个经常喝得烂醉的二流货色,他们组成名为「机械之声」的小队。过去这七人只在乎赚快钱及啤酒,但当他们发现家乡Emon市正面临一只可怕怪物的袭击时,他们意识到自己就是唯一能对抗怪物的人。, image=null, imageUrl=https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2844258904.jpg, ratingCount=1824, doCount=145, collectCount=1967, wishCount=3932, title=机械之声的传奇 第一季, episodesCount=12, alias=null, year=2022, genres=喜剧|动作|动画, tags=动画|美国|奇幻|2022|动作|喜剧|美剧|冒险|动漫|魔幻, director=, actors=艾什莉·约翰逊|劳拉·贝莉|特拉维斯·威林厄姆|连姆·奥布赖恩, status=1, delFlag=0)
DoubanInfo(id=8, subjectId=34902572, score=8.3, summary=讲述七个经常喝得烂醉的二流货色,他们组成名为「机械之声」的小队。过去这七人只在乎赚快钱及啤酒,但当他们发现家乡Emon市正面临一只可怕怪物的袭击时,他们意识到自己就是唯一能对抗怪物的人。, image=null, imageUrl=https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2844258904.jpg, ratingCount=1824, doCount=145, collectCount=1967, wishCount=3932, title=机械之声的传奇 第一季, episodesCount=12, alias=null, year=2022, genres=喜剧|动作|动画, tags=动画|美国|奇幻|2022|动作|喜剧|美剧|冒险|动漫|魔幻, director=, actors=艾什莉·约翰逊|劳拉·贝莉|特拉维斯·威林厄姆|连姆·奥布赖恩, status=1, delFlag=0)