Mybatis 缓存
1. 一级缓存:其存储作用域为 Session,当 Session flush 或 close 之后,该Session中的所有 Cache 就将清空。
2. 二级缓存与一级缓存其机制相同,不同在于其存储作用域为 Mapper(Namespace),并且可自定义存储源,如第三方 Ehcache。
一、一级缓存
实体类city
public class City implements Serializable{
private static final long serialVersionUID = 1L;
private int id;
private String cityName;
private String provinceId;
//getters、setters
}
CityMapper
<mapper namespace="com.area.dao.CityDao">
<select id="findByProvinceId" resultType="city">
select * from city where provinceId = #{pId}
</select>
</mapper>
测试;
@Test
public void testById() {
sqlSession = sqlSessionFactory.openSession();
CityDao cityDao = sqlSession.getMapper(CityDao.class);
cityDao.findByProvinceId("p1");
cityDao.findByProvinceId("p1");
sqlSession.commit();
}
测试结果
[com.area.dao.CityDao.findByProvinceId] - ==> Preparing: select * from city where provinceId = ?
[com.area.dao.CityDao.findByProvinceId] - ==> Parameters: p1(String)
[com.area.dao.CityDao.findByProvinceId] - <== Total: 1
从sql中可以看出,只有一次查询数据库的过程,这种现象产生的原因就是mybatis的一级缓存,并且一级缓存是默认开启的。
二、二级缓存
未开启二级缓存
@Test
public void test() {
sqlSession = sqlSessionFactory.openSession();
CityDao cityDao = sqlSession.getMapper(CityDao.class);
cityDao.findByProvinceId("p1");
sqlSession.commit();
sqlSession1 = sqlSessionFactory.openSession();
CityDao cityDao1 = sqlSession1.getMapper(CityDao.class);
cityDao1.findByProvinceId("p1");
sqlSession.commit();
}
2016-11-09 11:45:00 - ==> Preparing: select * from city where provinceId = ?
2016-11-09 11:45:00 - ==> Parameters: p1(String)
2016-11-09 11:45:00 - <== Total: 1
2016-11-09 11:45:00 - JDBC Connection
2016-11-09 11:45:00 - ==> Preparing: select * from city where provinceId = ?
2016-11-09 11:45:00 - ==> Parameters: p1(String)
2016-11-09 11:45:00 - <== Total: 1
2016-11-09 11:45:00 - Returning JDBC Connection to DataSource
两个session,分别查询provinceid为p1的city,与数据库交互了两次,这样说明mybatis当前并没有开启二级缓存。
配置如下:
<settings>
<setting name="cacheEnabled" value="true" />
</settings>
<mapper namespace="com.area.dao.CityDao">
<select id="findByProvinceId" resultType="city">
select * from city where provinceId = #{pId}
</select>
<cache/>
</mapper>
测试结果
2016-11-09 11:41:11 - ==> Preparing: select * from city where provinceId = ?
2016-11-09 11:41:11 - ==> Parameters: p1(String)
2016-11-09 11:41:12 - <== Total: 1
2016-11-09 11:41:12 - Cache Hit Ratio [com.area.dao.CityDao]: 0.5
2016-11-09 11:41:12 - Returning JDBC Connection to DataSource
只发出一条sql,第二条显示命中缓存,说明二级缓存起到缓存作用
总结:
mybatis
一级缓存:默认开启
二级缓存:
1. 映射语句文件中的所有select语句将会被缓存。
2. 映射语句文件中的所有insert,update和delete语句会刷新缓存。
3. 缓存会使用Least Recently Used(LRU,最近最少使用的)算法来收回。
4. 缓存会根据指定的时间间隔来刷新。
5. 缓存会存储1024个对象
Mybatis 缓存的更多相关文章
- mybatis缓存
mybatis缓存http://www.cnblogs.com/QQParadise/articles/5109633.htmlhttp://www.mamicode.com/info-detail- ...
- Mybatis缓存处理机制
一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...
- MyBatis缓存禁用失败
问题:MyBatis缓存无法禁用,同一个session的select查询结果一样,但是数据库其实已改变. 尝试达到想要的目的: 1.msgmapper.xml里的select标签加上 <sele ...
- MyBatis入门学习教程-MyBatis缓存
一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了 package me.gacl.test; 2 import me.gacl.domain.User; import ...
- MyBatis学习总结(七)——Mybatis缓存(转载)
孤傲苍狼 只为成功找方法,不为失败找借口! MyBatis学习总结(七)--Mybatis缓存 一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的 ...
- MyBatis学习总结(七)——Mybatis缓存
一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...
- MyBatis学习总结(七)——Mybatis缓存
一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...
- MyBatis——Mybatis缓存
原文:http://www.cnblogs.com/xdp-gacl/p/4270403.html MyBatis学习总结(七)--Mybatis缓存 一.MyBatis缓存介绍 正如大多数持久层框架 ...
- 【转】MyBatis学习总结(七)——Mybatis缓存
[转]MyBatis学习总结(七)——Mybatis缓存 一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualC ...
- 使用MyBatis缓存
(1).为什么需要使用缓存:: MyBatis是一个持久层(数据库层)映射框架,在所有访问数据库的操作中,无疑数据查询是最耗费数据库资源的操作了,因为你一次可能需要查询成千上百万条记录(如果你不加限制 ...
随机推荐
- Java日志系统及框架分析
最近在考虑将容器(Tomcat)内的应用日志统一成slf4j + logback,主要目的有: 快速定位应用日志输出路径,方便日志的采集: 能动态调整日志的级别,方便线上问题定位: 方便在容器层面做扩 ...
- rails日记1
assert_select "div" <div>foobar</div>assert_select "div", "foob ...
- To Use FTP Command in Linux
Yesterday I was asked to upload a file in Linux to the corresponding server. I said "oops" ...
- poj 1737 Connected Graph
// poj 1737 Connected Graph // // 题目大意: // // 带标号的连通分量计数 // // 解题思路: // // 设f(n)为连通图的数量,g(n)为非连通图的数量 ...
- OPC的理解Open Packaging Conventions
Open Packaging Conventions (OPC) 博客地址:www.cnblogs.com/icmzn OPC是一个文件容器技术.被微软创建,用来存储XML或者非XML文件结合起来的规 ...
- android wifi obtainmessage sendmessage解析
obtainmessage 从message pool获取一个对象 sendmessage 将message插入message queue java中wait和notify是一对,wait进入睡眠等待 ...
- hdu 5944 Fxx and string
\:nn,下标从1开始,第\:i\:i位的字母为\:s_isi,现在Fxx想知道有多少三元组\:(i,j,k)\:(i,j,k)满足下列条件 1.i,j,k\:i,j,k三个数成等比数列 2.s ...
- iOS关于定制某个控件四个角是否为圆角
UIView *myView=[[UIView alloc]initWithFrame:CGRectMake(50, 70, 200, 200)]; UIBezierPath * bezierPath ...
- On One Side Kolmogorov Type Inequalities
Let \(X_1,X_2,\ldots,X_n\) be independent random variables. Denote \[S_n=\sum_{i=1}^n X_i.\] The we ...
- java HashMap那点事
集合类的整体架构 比较重要的集合类图如下: 有序否 允许元素重复否 Collection 否 是 List 是 是 Set AbstractSet 否 否 HashSet TreeSet 是(用二 ...