开启mybatis二级缓存
mybatis的二级缓存是mapper级别的,也就是同一个mapper下的查询,可以使用缓存里面的值
下面就写个demo记录下
没有使用缓存之前
service
@Override public Device getUserById(Long id) { Device byId = deviceMapper.getDeviceById(id); log.info("======================"); Device one = deviceMapper.getDeviceById(id); return one; }
xml
<select id="getDeviceById" resultType="org.ongoal.tun.demos.entity.Device"> select * from device where device_id = #{i} </select>
控制台
Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@568fa466] was not registered for synchronization because synchronization is not active JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3c59cb10] will not be managed by Spring ==> Preparing: select * from device where device_id = ? ==> Parameters: 1(Long) <== Columns: device_id, device_name, source_id, sort_num, del_flag, create_by, update_by, create_time, update_time <== Row: 1, 开发数据1, 1750083747497119745, 0, 0, null, null, null, null <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@568fa466] 2024-03-14 16:45:05.679 INFO 19328 --- [nio-8080-exec-1] o.o.t.d.service.Impl.DeviceServiceImpl : ====================== Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46143ad3] was not registered for synchronization because synchronization is not active JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3c59cb10] will not be managed by Spring ==> Preparing: select * from device where device_id = ? ==> Parameters: 1(Long) <== Columns: device_id, device_name, source_id, sort_num, del_flag, create_by, update_by, create_time, update_time <== Row: 1, 开发数据1, 1750083747497119745, 0, 0, null, null, null, null <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46143ad3]
从控制台可以注意到,执行了两次SQL查询。
使用缓存
<cache/> <!--添加此标签--> <select id="getDeviceById" resultType="org.ongoal.tun.demos.entity.Device" useCache="true"> <!--加上useCache="true"属性 -->
select * from device where device_id = #{i} </select>
Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@25ff2dda] was not registered for synchronization because synchronization is not active Cache Hit Ratio [org.ongoal.tun.demos.mapper.DeviceMapper]: 0.0 JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@74786914] will not be managed by Spring ==> Preparing: select * from device where device_id = ? ==> Parameters: 1(Long) <== Columns: device_id, device_name, source_id, sort_num, del_flag, create_by, update_by, create_time, update_time <== Row: 1, 开发数据1, 1750083747497119745, 0, 0, null, null, null, null <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@25ff2dda] 2024-03-14 16:49:47.078 INFO 27412 --- [nio-8080-exec-1] o.o.t.d.service.Impl.DeviceServiceImpl : ====================== Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@558bce46] was not registered for synchronization because synchronization is not active Cache Hit Ratio [org.ongoal.tun.demos.mapper.DeviceMapper]: 0.5 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@558bce46]
可以看到第一次是执行了SQL,第二次是使用了缓存里面的值返回。
实际开发中,一般用的也不多,因为它的颗粒度比较高,是mapper级别的,并发情况下,也可能导致数据不一致情况。只有特定场合,觉得有必要,那才会使用此二级缓存
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)