MyBatis二级缓存配置
正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持
Mybatis二级缓存是SessionFactory,如果两次查询基于同一个SessionFactory,那么就从二级缓存中取数据,而不用到数据库里去取了。
1. 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Session,当 Session flush 或 close 之后,该Session中的所有 Cache 就将清空。
2. 二级缓存与一级缓存其机制相同,默认也是采用 PerpetualCache,HashMap存储,不同在于其存储作用域为 Mapper(Namespace),并且可自定义存储源,如 Ehcache。
启动二级缓存:
1.mybatis-config.xml添加
<setting name="cacheEnabled" value="true"/>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<setting name="cacheEnabled" value="true"/><!-- 二级缓存 -->
</settings>
<typeAliases>
<package name="pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/lol?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="pojo/Product.xml"/>
</mappers>
</configuration>
2.在Product.xml(数据库关系映射)中增加 <cache/>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="pojo">
<cache/>
<insert id="addProduct" parameterType="Product" >
insert into product (name, price) values (#{name}, #{price})
</insert>
<select id="listProduct" resultType="Product">
select * from product
</select>
<delete id="deleteProduct" parameterType="Product" >
delete from product where id= #{id}
</delete>
<select id="getProduct" parameterType="_int" resultType="Product">
select * from product where id=#{id}
</select>
<select id="listProductByIdAndName" resultType="Product">
<bind name="likename" value="'%' + name + '%'" />
select * from product
<where>
<if test="name!=null">
and name like #{likename}
<if test="price!=null">
and price > #{price}
</if>
</if>
</where> </select>
</mapper>
3.序列化Product.java(pojo类)
通常来说,二级缓存的机制里会有一个: 当缓存的个数达到某个数量的时候,把缓存对象保存在硬盘上。 如果要把对象保存在硬盘上,就必须实现序列化接口。
序列化相关知识:http://blog.csdn.net/wangloveall/article/details/7992448/
package pojo;
import java.io.Serializable;
public class Product implements Serializable{
private int id;
private String name;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
4.测试类
package controller; import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import pojo.Product; public class TestMybatis {
public static void main(String [] args) throws IOException{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession(); Product p = session.selectOne("getProduct", 1);
session.commit();
session.close(); SqlSession session1 = sqlSessionFactory.openSession();//二级缓存测试
Product p2 = session1.selectOne("getProduct", 1);
session1.commit();
session1.close();
}
}
5.结果

MyBatis二级缓存配置的更多相关文章
- Mybatis的二级缓存配置
一个项目中肯定会存在很多共用的查询数据,对于这一部分的数据,没必要每一个用户访问时都去查询数据库,因此配置二级缓存将是非常必要的. Mybatis的二级缓存配置相当容易,要开启二级缓存,只需要在你的 ...
- SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置
2016年03月03日 10:37:47 标签: mysql / redis / mybatis / spring mvc / spring 33805 项目环境: 在SpringMVC + MyBa ...
- SpringMVC +Spring + MyBatis + Mysql + Redis(作为二级缓存) 配置
转载:http://blog.csdn.net/xiadi934/article/details/50786293 项目环境: 在SpringMVC +Spring + MyBatis + MySQL ...
- 深入了解MyBatis二级缓存
深入了解MyBatis二级缓存 标签: mybatis二级缓存 2015-03-30 08:57 41446人阅读 评论(13) 收藏 举报 分类: Mybatis(51) 版权声明:版权归博主所 ...
- mybati缓存机制之二级缓存配置
二级缓存配置 在MyBatis的配置文件中开启二级缓存. <setting name="cacheEnabled" value="true"/> 在 ...
- mybatis二级缓存
二级缓存区域是根据mapper的namespace划分的,相同namespace的mapper查询数据放在同一个区域,如果使用mapper代理方法每个mapper的namespace都不同,此时可以理 ...
- 如何细粒度地控制你的MyBatis二级缓存(mybatis-enhanced-cache插件实现)
前几天网友chanfish 给我抛出了一个问题,笼统地讲就是如何能细粒度地控制MyBatis的二级缓存问题,酝酿了几天,觉得可以写个插件来实现这个这一功能.本文就是从问题入手,一步步分析现存的MyBa ...
- MyBatis 二级缓存全详解
目录 MyBatis 二级缓存介绍 二级缓存开启条件 探究二级缓存 二级缓存失效的条件 第一次SqlSession 未提交 更新对二级缓存影响 探究多表操作对二级缓存的影响 二级缓存源码解析 二级缓存 ...
- Springboot整合Ehcache 解决Mybatis二级缓存数据脏读 -详细
前面有写了一篇关于这个,但是这几天又改进了一点,就单独一篇在详细说明一下 配置 application.properties ,启用Ehcache # Ehcache缓存 spring.cache.t ...
随机推荐
- PAT天梯赛 L2-020. 功夫传人 【DFS】
题目链接 https://www.patest.cn/contests/gplt/L2-020 思路 从师父开始 一层一层往下搜 然后 搜到 得道者 就更新答案 AC代码 #include <c ...
- c#使用itextsharp输出pdf(动态填充表单内容,显示中文)
相关链接: iText的简单应用-字体 c#程序为PDF文件填写表单内容 示例代码: static void Main(string[] args) { BaseFont font = BaseFon ...
- 苹果AppStore如何申请加急审核
登录iTunesconnect,点击右上角的“?”图标,选择“联系我们”. iTunes Connect首页 依次选择“App Review”.“App Store Review” .” Reques ...
- kvm初体验之六:克隆
目标:克隆vm1到vm1-clone 1. virsh suspend vm1 2. virt-clone --connect qemu:///system --original vm1 --name ...
- SpringMVC框架<mvc:default-servlet-handler/>的作用
1.创建一个新工程 Eclipse下新建一个web项目,File>New>Dynamic Web Project 2.添加Jar包 3.配置Web.xml 4.配置 ...
- selenium中类名不能与方法名相同
不要将selenium中的类名命名成需要用到的方法名,不然会报错!
- 编写html页面时常见的问题(一)
说到写页面,肯定有很多人在刚接触编写页面这一块时遇到很多细节和兼容性的问题,那么在这里我总结一些经常遇到的小问题.希望能够帮助学习页面搭建的初学者! 虽然说ie6很多公司都已经抛弃,但是个人认为,初学 ...
- bzoj 4003: 城池攻占 左偏树
题目大意 http://www.lydsy.com/JudgeOnline/problem.php?id=4003 题解 一开始看漏条件了 题目保证当占领城池可以使攻击力乘上\(v_i\)时,一定有\ ...
- ActiveRecord 的类型初始值设定项引发异常
最近在研究ActiveRecord网上有很多贴子讲怎么用的.但自己照做就是出错. 最终定位在配置文件出错.应该是ActiveRecord有更新的原因.在国外的网站把配置复制了一份替换.问题解决了.我用 ...
- Silverlight 5 系列学习之一
最近公司要用Silverlight 开发并且使用了5 ,以前只学过WPF 没看过Silverlight ,不过在争光中国看了看其概念原来如此.它只不过是轻量级的WPF,且目标在于跨浏览器及平台.费话少 ...