MyBatis缓存

原创 2017年01月03日 15:37:21
  • 一级缓存
    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.

版权声明:本文为博主原创文章,未经博主允许不得转载。

mybatis一二级缓存

  • 2017年11月28日 16:02
  • 41KB
  • 下载

MyBatis中如何合理的使用EhCache缓存框架

缓存简介 缓存使用场景 在mapperxml中启用Ehcache 当为Select语句时 当为insertupdatedelete语句时 配置场景 使用举例关于详细配置方面已经在另一篇文章中说过:My...

mybatis+redis缓存配置

  • 2016年09月22日 14:52
  • 17KB
  • 下载

MyBatis学习 之 三、SQL语句映射文件-增删改查、参数、缓存

MyBatis学习 之 一、MyBatis简介与配置MyBatis+Spring+MySql MyBatis学习 之 二、SQL语句映射文件(1)resultMap MyBatis学习 之 三、S...

MyBatis_sql-resultMap-缓存

  • 2017年05月07日 12:50
  • 9.13MB
  • 下载

MyBatis学习 之 三、SQL语句映射文件(2)增删改查、参数、缓存

2.2 select 一个select 元素非常简单。例如: Xml代码      select id="getStudent" parameterType="Stri...

mybatis二级缓存学习

  • 2016年12月05日 18:02
  • 39KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:MyBatis缓存
举报原因:
原因补充:

(最多只允许输入30个字)