【SSM-Mybatis】缓存

一级缓存:
一级缓存在session上,只要通过session查过的数据,都会放在session上,下一次再查询相同id的数据,都直接冲缓存中取出来,而不用到数据库里去取了。

二级缓存:
二级缓存是SessionFactory,如果两次查询基于同一个SessionFactory,那么就从二级缓存中取数据,而不用到数据库里去取了。

一级缓存:

 @Test
    public void test() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session1 = sqlSessionFactory.openSession();
        SqlSession session2 = sqlSessionFactory.openSession();

        World w1 = session1.selectOne("selId",1);
        System.out.println(w1);
        World w2 = session1.selectOne("selId",1);
        System.out.println(w2);

        System.out.println("--------------------");

        World w3 = session1.selectOne("selId",1);
        System.out.println(w3);
        World w4 = session2.selectOne("selId",1);
        System.out.println(w4);

    }

在这里插入图片描述

二级缓存

1.在mybatis.xml中添加设置允许二级缓存

<setting name="cacheEnabled" value="true"/>

2.在World.xml中添加 <cache/>

<mapper namespace="M2.pojo">
    <cache/>

3.World类实现序列化
implements Serializable{

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值