MyBatis二级缓存

Mybatis默认是不开启二级缓存的,相对于同一个sqlsession而言只开启一级缓存,所以在参数和sql完全相同的情况下,我们使用同一个sqlSession调用同一个mapper方法,只会执行一次sql查询

一:下面是一级缓存的实例,一级缓存是在sqlSession层面的

sqlSession=SqlSessionFactotyUtil.openSqlSession();
RoleMapper roleMapper=sqlSession.getMapper(RoleMapper.class);
        <!--第一次使用-->
            List<Role> roles=roleMapper.findRoleByMap("d","9");
            for (Role role2 : roles) {
                System.out.println("符合条件的结果为"+role2.getId());
            }
        <!--第二次使用-->
            List<Role> roles3=roleMapper.findRoleByMap("d","9");
            for (Role role2 : roles3) {
                System.out.println("符合条件的结果为"+role2.getId());
            }
            sqlSession.commit();

查询的结果如下

<!--第一次结果-->
==>  Preparing: select id,roleName as roleName, note from role where roleName like concat('%',?,'%') and note like concat('%',?,'%') 
==> Parameters: d(String), 9(String)
<==      Total: 2
符合条件的结果为3
符合条件的结果为5

<!--第二次结果-->
符合条件的结果为3
符合条件的结果为5

二级缓存是针对SqlSessionFactory的,不会在各种sqlSession之间隔离

开启二级缓存
1.需要在xml影射中加入

    <cache></cache>这样设置都是默认的
    <cache eviction="LRU" flushInterval="100000" size="1024" readOnly="true" />s
    flushInterval刷新间隔时间,100s
    size 最大缓存的对象数量,不事宜过大,会导致内存溢出
    readOnly 该为true之后,数据只能读取不能修改,这样可以快速读取缓存。默认为false
    eviction 缓存策略 lru为最长时间不使用

还可以自定义缓存,就不介绍了
2.影射的实体类中要实现Serializable接口

@Alias("role")
public class Role implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String roleName;
    private String note;

下面看实例代码

sqlSession=SqlSessionFactotyUtil.openSqlSession();
RoleMapper roleMapper=sqlSession.getMapper(RoleMapper.class);
        <!--第一次使用-->
            List<Role> roles=roleMapper.findRoleByMap("d","9");
            for (Role role2 : roles) {
                System.out.println("符合条件的结果为"+role2.getId());
            }
        <!--第二次使用-->
            List<Role> roles3=roleMapper.findRoleByMap("d","9");
            for (Role role2 : roles3) {
                System.out.println("符合条件的结果为"+role2.getId());
            }
            sqlSession.commit();
        <!-- 更换sqlsession之后-->
            System.out.println("------------------------------");
            sqlSession2=SqlSessionFactotyUtil.openSqlSession();
            RoleMapper roleMapper2=sqlSession2.getMapper(RoleMapper.class);
            List<Role> roles2=roleMapper2.findRoleByMap("d","9");
            for (Role role3 : roles2) {
                System.out.println("符合条件的结果为"+role3.getId());
            }
            sqlSession2.commit();

开启二级缓存之后结果如下

Cache Hit Ratio [com.learn.chapter2.mapper.RoleMapper]: 0.0
==>  Preparing: select id,roleName as roleName, note from role where roleName like concat('%',?,'%') and note like concat('%',?,'%') 
==> Parameters: d(String), 9(String)
<==      Total: 2
符合条件的结果为3
符合条件的结果为5
Cache Hit Ratio [com.learn.chapter2.mapper.RoleMapper]: 0.0
符合条件的结果为3
符合条件的结果为5
------------------------------
Cache Hit Ratio [com.learn.chapter2.mapper.RoleMapper]: 0.3333333333333333
符合条件的结果为3
符合条件的结果为5

可以从结果看出,就算更换了sqlsession之后,也总共执行了一次sql查询。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值