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级别的,并发情况下,也可能导致数据不一致情况。只有特定场合,觉得有必要,那才会使用此二级缓存

 

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。