开启mybatis开启二级缓存

Mybatis中有一级缓存和二级缓存,默认情况下一级缓存是开启的,而且是不能关闭的。一级缓存是指SqlSession级别的缓存,当在同一个SqlSession中进行相同的SQL语句查询时,第二次以后的查询不会从数据库查询,而是直接从缓存中获取,一级缓存最多缓存1024条SQL。二级缓存是指可以跨SqlSession的缓存,是mapper级别的缓存,对于mapper级别的缓存不同的sqlsession是可以共享的,mapper以命名空间为单位创建缓存数据结构,需要手动开启。

一、开启二级缓存

和一级缓存默认开启不一样,二级缓存需要我们手动开启

1、开启缓存

首先在全局配置文件 mybatis-configuration.xml 文件中加入如下代码:

<!--开启二级缓存  -->
<settings>    
     <setting name="cacheEnabled" value="true"/>
</settings>

springboot配置开启二级缓存

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.demo.entity
  configuration:
    cache-enabled: true

2、在XXXmapper.xml文件中使用缓存

<!-- 开启二级缓存 -->
<cache></cache>

我们可以看到 mapper.xml 文件中就这么一个空标签,其实这里可以配置,PerpetualCache这个类是mybatis默认实现缓存功能的类。我们不写type就使用mybatis默认的缓存,也可以去实现 Cache 接口来自定义缓存。

    <cache type="org.apache.ibatis.cache.impl.PerpetualCache">
        <property name="eviction" value="LRU" />
        <property name="flushInterval" value="6000000" />
        <property name="size" value="1024" />
        <property name="readOnly" value="false" />
    </cache>

二、useCache和flushCache

1、useCache
mybatis中还可以配置userCache和flushCache等配置项,userCache是用来设置是否禁用二级缓存的,useCache=false禁用当前select语句的二级缓存,即每次查询都会发出sql去查询,默认情况是true,即该sql使用二级缓存。

#禁用缓存
<select id="selectUserByUserId" useCache="false" resultType="com.ys.twocache.User" parameterType="int">    
select * from user where id=#{id}
</select>

对每次查询都需要最新的数据sql,要设置成useCache=false,禁用二级缓存,直接从数据库中获取。

在mapper的同一个namespace中,如果有其它insert、update、delete操作数据后需要刷新缓存,如果不执行刷新缓存会出现脏读。

2、flushCache

flushCache=”true”,默认情况下为true,即刷新缓存,如果改成false则不会刷新。使用缓存时如果手动修改数据库表中的查询数据会出现脏读。

#flushCache="true" 刷新缓存,flushCache="false" 不刷新缓存,可能出现脏读
<select id="selectUserByUserId" flushCache="true" useCache="false" resultType="com.ys.twocache.User" parameterType="int">    
select * from user where id=#{id}
</select>

一般下执行完commit操作都需要刷新缓存,flushCache=true表示刷新缓存,这样可以避免数据库脏读。
所以我们不用设置,默认即可。

三、关于@CacheNamespace

@CacheNamespace注解主要用于mybatis二级缓存,等同于属性。但是在使用过程中要注意:

@CacheNamespace虽然xml配置和注解的功能基本相同,但是使用@CacheNamespace时候要注意:

配置文件和接口注释是不能够配合使用的。只能通过全注解的方式或者全部通过xml配置文件的方式使用,配合使用二级缓存会出问题,比如更新数据后不刷新缓存

见下面的例子:

//通过注解的方式使用二级缓存
@CacheNamespace(implementation = MybatisRedisCache.class)
public interface UserMapper(
    @Select("select * from t_user where user_id = #{userId}")
    @Options(useCache = true)
    List<User> getUser(User u);
}
//通过配置文件的方式,使用二级缓存
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.mybatis.UserMapper">
 
    <cache type="cn.mybatis.MybatisRedisCache">
        <property name="eviction" value="LRU" />
        <property name="flushInterval" value="6000000" />
        <property name="size" value="1024" />
        <property name="readOnly" value="false" />
    </cache>
 
    <select id="selectById">
        select * from test where id = #{id}
    </select >
</mapper>

四、mybatis二级缓存的局限性

mybatis二级缓存粒度不够细,他是以mapper为单位进行缓存的,比如对所有商品进行缓存,但是其中只有一个商品发生了更新,那么将会刷新所有的缓存,这时可以采用其他的方式进行缓存,比如第三方缓存redis等等。
在这里插入图片描述

参考链接:
http://www.mybatis.cn/archives/128.html
https://www.cnblogs.com/charlypage/p/9747145.html

猜你喜欢

转载自blog.csdn.net/u011582840/article/details/108105540
今日推荐