关闭

MyBatis缓存

标签: mybatis缓存
76人阅读 评论(0) 收藏 举报
  • 一级缓存
    MyBatis默认是开启一级缓存的,是sqlSession,代码如下
public void queryById(long seckillId,SqlSessionFactory sqlSessionFactory) {
       // System.out.println("hehe--------"+sqlSessionFactory);

        SqlSession session = sqlSessionFactory.openSession();
        SqlSession session2 = sqlSessionFactory.openSession();

        Seckill seckill1= session.selectOne("selectById", seckillId);
        System.out.println("-------------"+seckill1);
        Seckill seckill12= session.selectOne("selectById", seckillId);
        System.out.println("-------------"+seckill1);

        session.close();


    }

系统日志,如下,只执行了一次sql查询

[DEBUG] 15:26:43.565 [main] o.m.s.t.SpringManagedTransaction - JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@26a792d3] will not be managed by Spring
[DEBUG] 15:26:43.579 [main] o.s.d.i.SeckillDao1Impl.selectById - ==>  Preparing: select seckill_id, name, number, start_time, end_time, create_time from seckill where seckill_id = ? 
[DEBUG] 15:26:43.659 [main] o.s.d.i.SeckillDao1Impl.selectById - ==> Parameters: 1000(Long)
[DEBUG] 15:26:43.703 [main] o.s.d.i.SeckillDao1Impl.selectById - <==      Total: 1
-------------Seckill{seckillId=1000, name='1000元秒杀iphone6', number=99, createTime=Thu Dec 29 14:20:31 CST 2016, startTime=Fri Dec 30 00:00:00 CST 2016, endTime=Sat Dec 31 00:00:00 CST 2016}
[DEBUG] 15:26:43.713 [main] org.seckill.dao.impl.SeckillDao1Impl - Cache Hit Ratio [org.seckill.dao.impl.SeckillDao1Impl]: 0.0
-------------Seckill{seckillId=1000, name='1000元秒杀iphone6', number=99, createTime=Thu Dec 29 14:20:31 CST 2016, startTime=Fri Dec 30 00:00:00 CST 2016, endTime=Sat Dec 31 00:00:00 CST 2016}

Process finished with exit code 0
  • 二级缓存sqlSessionFactory
    要开启二级缓存,首先需要在mybatis-config.xml里面配置,由于MyBatis的二级缓存作用域是Mapper的namespace,所以还要再Mapper.xml里面加上用以开启二级缓存
public void queryById(long seckillId,SqlSessionFactory sqlSessionFactory) {// System.out.println("hehe--------"+sqlSessionFactory);
        SqlSession session = sqlSessionFactory.openSession();
        SqlSession session2 = sqlSessionFactory.openSession();
        Seckill seckill1= session.selectOne("selectById", seckillId);
        System.out.println("-------------"+seckill1);

        //这里执行关闭操作,将sqlsession中的数据写到二级缓存区域
        session.close();
    }

我在test类里面调用这个方法两次,运行结果:

[DEBUG] 15:32:58.688 [main] o.m.s.t.SpringManagedTransaction - JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@cbb5ec0] will not be managed by Spring
[DEBUG] 15:32:58.698 [main] o.s.d.i.SeckillDao1Impl.selectById - ==>  Preparing: select seckill_id, name, number, start_time, end_time, create_time from seckill where seckill_id = ? 
[DEBUG] 15:32:58.760 [main] o.s.d.i.SeckillDao1Impl.selectById - ==> Parameters: 1000(Long)
[DEBUG] 15:32:58.820 [main] o.s.d.i.SeckillDao1Impl.selectById - <==      Total: 1
-------------Seckill{seckillId=1000, name='1000元秒杀iphone6', number=99, createTime=Thu Dec 29 14:20:31 CST 2016, startTime=Fri Dec 30 00:00:00 CST 2016, endTime=Sat Dec 31 00:00:00 CST 2016}
[DEBUG] 15:32:58.843 [main] org.seckill.dao.impl.SeckillDao1Impl - Cache Hit Ratio [org.seckill.dao.impl.SeckillDao1Impl]: 0.5
-------------Seckill{seckillId=1000, name='1000元秒杀iphone6', number=99, createTime=Thu Dec 29 14:20:31 CST 2016, startTime=Fri Dec 30 00:00:00 CST 2016, endTime=Sat Dec 31 00:00:00 CST 2016}
一月 03, 2017 3:32:58 下午 org.springframework.context.support.GenericApplicationContext doClose

如果没有在mapper里面加上运行结果是

[DEBUG] 15:34:17.304 [main] o.m.s.t.SpringManagedTransaction - JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@149c32b] will not be managed by Spring
[DEBUG] 15:34:17.322 [main] o.s.d.i.SeckillDao1Impl.selectById - ==>  Preparing: select seckill_id, name, number, start_time, end_time, create_time from seckill where seckill_id = ? 
[DEBUG] 15:34:17.407 [main] o.s.d.i.SeckillDao1Impl.selectById - ==> Parameters: 1000(Long)
[DEBUG] 15:34:17.455 [main] o.s.d.i.SeckillDao1Impl.selectById - <==      Total: 1
-------------Seckill{seckillId=1000, name='1000元秒杀iphone6', number=99, createTime=Thu Dec 29 14:20:31 CST 2016, startTime=Fri Dec 30 00:00:00 CST 2016, endTime=Sat Dec 31 00:00:00 CST 2016}
[DEBUG] 15:34:17.463 [main] o.m.s.t.SpringManagedTransaction - JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@5a3564a1] will not be managed by Spring
[DEBUG] 15:34:17.463 [main] o.s.d.i.SeckillDao1Impl.selectById - ==>  Preparing: select seckill_id, name, number, start_time, end_time, create_time from seckill where seckill_id = ? 
[DEBUG] 15:34:17.464 [main] o.s.d.i.SeckillDao1Impl.selectById - ==> Parameters: 1000(Long)
[DEBUG] 15:34:17.465 [main] o.s.d.i.SeckillDao1Impl.selectById - <==      Total: 1
-------------Seckill{seckillId=1000, name='1000元秒杀iphone6', number=99, createTime=Thu Dec 29 14:20:31 CST 2016, startTime=Fri Dec 30 00:00:00 CST 2016, endTime=Sat Dec 31 00:00:00 CST 2016}

执行了两遍sql.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2104次
    • 积分:164
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条