mybatis13--2级缓存
验证内置的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();}@Overridepublic 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;@Beforepublic void before() {// 因为需要关闭session 需要把session提取出去session = SessionUtil.getSession();dao = session.getMapper(StudentDao.class);}@Afterpublic void after() {if (session != null) {session.close();}}/*** 验证2级缓存* * 开启内置2级缓存的步骤* 01.实体类对象 要实现serializable 序列化接口* 02.在mapper文件中 增加 <cache/>节点*/@Testpublic 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级缓存的影响*/@Testpublic 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>*/@Testpublic 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.不能在关联关系表上执行增删改操作*/
转载于:https://www.cnblogs.com/xtdxs/p/7096015.html
mybatis13--2级缓存相关推荐
- 【高并发】在高并发环境下该如何构建应用级缓存?
来自:冰河技术 写在前面 随着我们的系统负载越来越高,系统的性能就会有所下降,此时,我们可以很自然地想到使用缓存来解决数据读写性能低下的问题.但是,立志成为资深架构师的你,是否能够在高并发环境下合理并 ...
- nuxt渲染html文件,Nuxt页面级缓存
虽然 Vue 的服务器端渲染 (SSR) 相当快速,但是由于需要为每次请求为了避免交叉请求状态污染,都创建一个新的根Vue实例,创建组件实例和虚拟 DOM 节点的开销,无法与纯基于字符串拼接的模板的性 ...
- Hibernate缓存 - 第一级缓存
Hibernate缓存 - 第一级缓存 欢迎使用Hibernate缓存 - 一级缓存示例教程.最近我们研究了Hibernate架构,hibernate映射以及如何使用HQL以面向对象的方式触发SQL查 ...
- 在高并发环境下该如何构建应用级缓存
摘要:立志成为资深架构师的你,是否能够在高并发环境下合理并且高效的构建应用级缓存呢? 本文分享自华为云社区<[高并发]在高并发环境下该如何构建应用级缓存?>,作者:冰 河. 随着我们的系统 ...
- java l1 l2缓存,Java 两级缓存框架
概述介绍 J2Cache 是 OSChina 目前正在使用的两级缓存框架(要求至少 Java 8).第一级缓存使用内存(同时支持 Ehcache 2.x.Ehcache 3.x 和 Caffeine) ...
- SpringBoot 集成 layering-cache 实现两级缓存调研与实践
前言 对于系统查多改少的数据,可以通过缓存来提升系统的访问性能.一般情况下我们会采用 Redis ,但是如果仅仅依赖 Redis 很容易出现缓存雪崩的情况.为了防止缓存雪崩可以通过 Redis 高可用 ...
- j2cache两级缓存框架
j2cache介绍 j2cache是OSChina目前正在使用的两级缓存框架. j2cache的两级缓存结构: L1: 进程内缓存 caffeine/ehcache L2: 集中式缓存 Redis/M ...
- 高性能两级缓存J2Cache
今天给大家推荐一个开源项目,J2Cache,一个很完善的两级缓存项目,作者是-红薯(开源中国CTO) 介绍 开源项目地址:https://gitee.com/ld/J2Cache/tree/maste ...
- (一)大型电商详情页亿级缓存架构简介
大型电商详情页亿级缓存架构 上亿流量的商品详情页系统的多级缓存架构 架构图 采用三级缓存:**nginx本地缓存+redis分布式缓存+tomcat堆缓存的多级缓存架构**. 多级缓存架构中每一层的意 ...
- Springboot+caffeine 实现两级缓存
目录: 缓存.两级缓存 spring cache:主要包含spring cache定义的接口方法说明和注解中的属性说明 spring boot + spring cache caffeine简介 sp ...
最新文章
- keep-alive的深入理解与使用(配合router-view缓存整个路由页面)
- 报名 | 统计学概论和医疗临床大数据分析讲座
- Domino+Qucikplace+Sametime构建企业办公平台图文攻略(三)
- 信息系统项目管理师论文怎么准备?49分论文备考经验
- python实现货币转换
- java set方法赋值_java方面:private属性,没有set方法,只有get方法,如何给这个属性赋值?...
- php试题库,PHP试题库一
- 如何把hadoop源码关联到eclipse工程中
- reporting server 数据头部每页显示
- 简单的sql循环操作
- Java SE 正则表达式 API Pattern 与 Matcher.
- 如何定义适配器adapter类_【设计模式】第六篇:来康康适配器模式
- 2016-2017-2 《Java 程序设计》课堂实践项目
- 鲁大师2014 v3.75.14.1058 官方版
- Java 导出word和pdf_Java实现word导出与pdf导出
- 证明三角形中cosA+cosB+cosC=1+4sin(A/2)sin(B/2)sin(C/2)
- 比特大陆“众叛亲离”?
- htpp proxy
- “使用达芬奇软件实现Autosar架构:配置和注意事项“
- 解决 ImportError: cannot import name ‘_C‘ from ‘detectron2‘