【Mybatis学习笔记】3.缓存

1.缓存检索顺序:二级缓存 > 一级缓存 > 数据库
2.【缓存失效方式一】如果查询之后进行增删改行为,则缓存失效
【缓存失效方式二】sqlSession.clearCache()强制清空缓存
3.
mybatis的二级缓存是namespace级别的,就是同一Mapper下的多个sqlSession共享一个缓存。因此只要有一个sqlSession发出了一个查询,第二sqlSession发出同样的查询的话,是直接从二级缓存中取数据的。
二级缓存默认不开启,如果要使用的话要在mybatis-config.xml的全局配置文件中开启,同时要在mapper 的sql映射文件中配置cache, 此外缓存的对象 必须 implments Serializable接口。
4.在这里插入图片描述

<!--开启二级缓存-->
    <cache
            eviction="FIFO"
            flushInterval="60000"
            size="512"
            readOnly="true"/>
//二级缓存实验
    @Test
    public void m8(){
        SqlSession sqlSession = MybatisUtil.getSession();
        SqlSession sqlSession2 = MybatisUtil.getSession();
        AddressesMapper mapper = sqlSession.getMapper(AddressesMapper.class);
        AddressesMapper mapper2 = sqlSession2.getMapper(AddressesMapper.class);

        mapper.ListAll();
        sqlSession.close();

        mapper2.ListAll();
        sqlSession.close();

    }

猜你喜欢

转载自blog.csdn.net/weixin_43046082/article/details/88935586