验证内置的2级缓存

Ehcache缓存的配置

01.引入需要的ehcache 和mybatis-ehcache 两个jar包

02.在mapper文件中增加  <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> <!-- 配置Ehcache缓存 -->

org.mybatis.caches.ehcache.EhcacheCache就是在mybatis-ehcache这个jar包中

03.引入需要的ecache.xml文件   就在ecache.jar中

创建对应的dao

public interface StudentDao {
/**
* 验证mybatis2级缓存!
*/
Student selectStudentById(Integer sId); }

创建对应的mapper文件

<?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="cn.bdqn.dao.StudentDao">
<cache/> <!-- 配置2级缓存 -->
<select id="selectStudentById" resultType="Student">
select sid,sname from student where sid=#{xxx}
</select> </mapper>

实体类实现Serializable序列化接口

/**
*学生对应的实体类
*/
public class Student implements Serializable { private Integer sId;
private String sName; public Integer getsId() {
return sId;
}
public void setsId(Integer sId) {
this.sId = sId;
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public Student(Integer sId, String sName) {
super();
this.sId = sId;
this.sName = sName;
}
public Student() {
super();
}
@Override
public String toString() {
return "Student [sId=" + sId + ", sName=" + sName +"]";
} }

把log4j的配置文件中的显示改成

增加测试类代码

package cn.bdqn.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import cn.bdqn.bean.Student;
import cn.bdqn.dao.StudentDao;
import cn.bdqn.util.SessionUtil; public class TeacherTest {
StudentDao dao;
SqlSession session; @Before
public void before() {
// 因为需要关闭session 需要把session提取出去
session = SessionUtil.getSession();
dao = session.getMapper(StudentDao.class);
} @After
public void after() {
if (session != null) {
session.close();
}
} /**
* 验证2级缓存
*
* 开启内置2级缓存的步骤
* 01.实体类对象 要实现serializable 序列化接口
* 02.在mapper文件中 增加 <cache/>节点
*/
@Test
public void test1() {
Student student = dao.selectStudentById(1);
System.out.println(student); session.close(); //关闭了session 一级缓存中的数据肯定清空了 session = SessionUtil.getSession(); //再次获取session 查询数据
dao = session.getMapper(StudentDao.class);
//这时候不会再有sql语句了 因为2级缓存中存在相同的查询(mapper文件中sql的id)和相同的sql语句
Student student2 = dao.selectStudentById(1);
System.out.println(student2);
} }

查看运行的结果

验证增删改对2级缓存的影响

在dao中新增方法

public interface StudentDao {
/**
* 验证mybatis2级缓存!
*/
Student selectStudentById(Integer sId); /**
* 验证增删改查对2级缓存的影响!
*/
void addStudent(Student student);
}

在mapper文件中新增

<?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="cn.bdqn.dao.StudentDao">
<cache/> <!-- 配置2级缓存 -->
<select id="selectStudentById" resultType="Student">
select sid,sname from student where sid=#{xxx}
</select> <!-- 新增一个学生 验证对2级缓存的影响 标签中 增加 flushCache="false" 可以设置在新增数据的时候不刷新2级缓存
但是一级缓存不能配置 也就是 只要是一级缓存的增删改 都会刷新 -->

<insert id="addStudent"> insert into student values(#{sId},#{sName}) <!--#{sId},#{sName} 对应的是实体类中的属性 --></insert></mapper>

在测试类中新增

    /**
* 验证增删改对2级缓存的影响
*/
@Test
public void test2() {
Student student = dao.selectStudentById(1);
System.out.println(student); session.close(); //关闭了session 一级缓存中的数据肯定清空了 session = SessionUtil.getSession(); //再次获取session 查询数据
dao = session.getMapper(StudentDao.class);
//新增学生信息 看看对2级缓存的影响
dao.addStudent(new Student(66,"测试")); Student student2 = dao.selectStudentById(1);
System.out.println(student2);
}

得到的结果:

2级缓存的关闭

/**
* 2级缓存的关闭
* 01.局部关闭
* 在mapper文件中修改
* <select id="selectStudentById" useCache="false" resultType="Student">
* 增加了useCache="false" 相当于 局部关闭 2级缓存 useCache默认值为true===》把查询放入2级缓存
* 02.全局关闭
* 在mybatis.xml文件中增加
* <settings>
* <!--全局关闭2级缓存 -->
* <setting name="cacheEnabled" value="false"/>
* </settings>
*/ @Test
public void test3() {
Student student = dao.selectStudentById(1);
System.out.println(student); session.close(); //关闭了session 一级缓存中的数据肯定清空了 session = SessionUtil.getSession(); //再次获取session 查询数据
dao = session.getMapper(StudentDao.class);
//这时候不会再有sql语句了 因为2级缓存中存在相同的查询(mapper文件中sql的id)和相同的sql语句
Student student2 = dao.selectStudentById(1);
System.out.println(student2);
} /**
* 2级缓存的使用原则:
* 01. 很少被修改的数据
* 02. 不是很重要的数据,允许出现偶尔并发的数据
* 03. 不会被并发访问的数据
* 04.多个namespace不能操作同一张表
* 05.不能在关联关系表上执行增删改操作
*/

mybatis13--2级缓存的更多相关文章

  1. 缓存篇(Cache)~第三回 HttpModule实现网页的文件级缓存

    返回目录 再写完缓存篇第一回之后,得到了很多朋友的好评和来信,所以,决定加快步伐,尽快把剩下的文章写完,本篇是第三回,主要介绍使用HttpModule实现的文件级缓存,在看本文之前,大家需要限度Htt ...

  2. Hibernate-二级缓存策略

    二级缓存的策略 当多个并发的事务同时访问持久化层的缓存中的相同数据时,会引起并发问题,必须采用必要的事务隔离措施. 在进程范围或集群范围的缓存,即第二级缓存,会出现并发问题.因此可以设定以下4种类型的 ...

  3. Hibernate-一级缓存session

    hibernate提供的一级缓存 hibernate是一个线程对应一个session,一个线程可以看成一个用户.也就是说session级缓存(一级缓存)只能给一个线程用,别的线程用不了,一级缓存就是和 ...

  4. Spring+ehcache+redis两级缓存

    问题描述 场景:我们的应用系统是分布式集群的,可横向扩展的.应用中某个接口操作满足以下一个或多个条件: 1. 接口运行复杂代价大, 2. 接口返回数据量大, 3. 接口的数据基本不会更改, 4. 接口 ...

  5. mybatis学习笔记(14)-查询缓存之中的一个级缓存

    mybatis学习笔记(14)-查询缓存之中的一个级缓存 标签: mybatis mybatis学习笔记14-查询缓存之中的一个级缓存 查询缓存 一级缓存 一级缓存工作原理 一级缓存測试 一级缓存应用 ...

  6. 用guava快速打造两级缓存能力

    首先,咱们都有一共识,即可以使用缓存来提升系统的访问速度! 现如今,分布式缓存这么强大,所以,大部分时候,我们可能都不会去关注本地缓存了! 而在一起高并发的场景,如果我们一味使用nosql式的缓存,如 ...

  7. Redis+Caffeine两级缓存,让访问速度纵享丝滑

    原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 在高性能的服务架构设计中,缓存是一个不可或缺的环节.在实际的项目中,我们通常会将一些热点数据存储到Redis或MemCache这类缓存中间件中, ...

  8. 基于Spring接口,集成Caffeine+Redis两级缓存

    原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 在上一篇文章Redis+Caffeine两级缓存,让访问速度纵享丝滑中,我们介绍了3种整合Caffeine和Redis作为两级缓存使用的方法,虽 ...

  9. Spring的3级缓存和循环引用的理解

    此处是我自己的一个理解,防止以后忘记,如若那个地方理解不对,欢迎指出. 一.背景 在我们写代码的过程中一般会使用 @Autowired 来注入另外的一个对象,但有些时候发生了 循环依赖,但是我们的代码 ...

随机推荐

  1. Using async-await on .net 4

    I'm currently starting to create an application that would profit a lot from C# 5's async-await feat ...

  2. APB协议

    https://wenku.baidu.com/view/2663f629ef06eff9aef8941ea76e58fafab04592.html https://www.cnblogs.com/l ...

  3. Js实现Table动态添加一行的小例子

    <form id="form1" runat="server"> <div> <table id=" style=&qu ...

  4. python2.7升级3.5教程 可用

    1.查看Python版本: python -V 2.下载Python 3.5版本:wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.t ...

  5. Log4j/Log4j2自定义Appender来实现日志级别计数统计及监控

    一.简述 本文主要讲如何基于Log4j2来实现自定义的Appender.一般用途是用于Log4j2自带的Appender不足以满足我们的需求,或者需要我们对日志进行拦截统计等操作时,需要我们自定义Ap ...

  6. JAVA项目中引用Logback的方法

    一.简介 本文主要讲JAVA项目中引入Logback的方法. 二.解决 1.引入依赖. <!--Begin LogBack Log--> <!-- https://mvnreposi ...

  7. C# System.Threading.Timer

    提供以指定的时间间隔对线程池线程执行方法的机制 using System; using System.Threading; class TimerExample { static void Main( ...

  8. Python操作redis系列之 列表(list) (五)(转)

    # -*- coding: utf-8 -*- import redis r =redis.Redis(host=") 1. Lpush 命令将一个或多个值插入到列表头部. 如果 key 不 ...

  9. Ant之build.xml配置详解【转】

    原文:https://blog.csdn.net/mevicky/article/details/72828554 前言国内关于build.xml的配置资料太零散了,实在是受不了,故而将自己的笔记整理 ...

  10. 配置logback

    相关组件] Logback是由log4j创始人设计的又一个开源日志组件. logback当前分成三个模块:logback-core.logback- classic和logback-access. l ...