#MyBatis中使用Ehcache缓存框架
来源:互联网 发布:mac. wps 编辑:程序博客网 时间:2024/04/28 00:42
- MyBatis简介
- Ehcache简介
- 使用Ehcache的目的
- MyBatis中使用Ehcache
- jar包
- ecache配置文件文件名必须为ehcachexml
- 在mybatis的mapper中启用
MyBatis简介
MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手工设置参数以及抽取结果集。MyBatis 使用简单的 XML 或注解来配置和映射基本体,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
Ehcache简介
Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特
点。
使用Ehcache的目的
为了提高MyBatis的性能,我们需要加入缓存的支持。
MyBatis中使用Ehcache
jar包
ehcache-core-2.6.10.jarmybatis-ehcache-1.0.3.jar
ecache配置文件,文件名必须为ehcache.xml
<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <diskStore path="java.io.tmpdir" /> <!-- 缓存存放目录(此目录为放入系统默认缓存目录),也可以是”D:/cache“ java.io.tmpdir --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" > </defaultCache></ehcache><!-- name:Cache的唯一标识 maxElementsInMemory:内存中最大缓存对象数 maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大 eternal:Element是否永久有效,一但设置了,timeout将不起作用 overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中 timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大 timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大 diskPersistent:是否缓存虚拟机重启期数据 diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒 diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区 memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)-->
在mybatis的mapper中启用
ehcache已经配置好了,之后我们只需要在需要缓存的mapper配置文件里面加入<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
,该查询语句得到的结果将会被缓存
有两种写法
<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/><cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
第一个可以输出日志,第二个不输出日志
<?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="UserXMapper"> <!-- ehcache已经配置好了,然后只需要在想要缓存的mapper配置文件里面加入以下内容,该查询语句得到的结果将会被缓存 以下两个<cache>标签都可以,第一个可以输出日志,第二个不输出日志 <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> --> <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> <!-- 用户列表(全部) --> <select id="listAllUser" parameterType="pd" resultType="pd" useCache="false"> select u.USER_ID, u.USERNAME, u.PASSWORD, u.LAST_LOGIN, u.NAME, u.IP, u.EMAIL, u.NUMBER, u.PHONE, r.ROLE_ID, r.ROLE_NAME from SYS_USER u, SYS_ROLE r where u.ROLE_ID = r.ROLE_ID and u.USERNAME != 'admin' <if test="USERNAME != null and USERNAME != ''"><!-- 关键词检索 --> and ( u.USERNAME LIKE CONCAT(CONCAT('%', #{USERNAME}),'%') or u.EMAIL LIKE CONCAT(CONCAT('%', #{USERNAME}),'%') or u.NUMBER LIKE CONCAT(CONCAT('%', #{USERNAME}),'%') or u.NAME LIKE CONCAT(CONCAT('%', #{USERNAME}),'%') or u.PHONE LIKE CONCAT(CONCAT('%', #{USERNAME}),'%') ) </if> <if test="ROLE_ID != null and ROLE_ID != ''"><!-- 角色检索 --> and u.ROLE_ID=#{ROLE_ID} </if> <if test="lastLoginStart!=null and lastLoginStart!=''"><!-- 登录时间检索 --> and u.LAST_LOGIN >= #{lastLoginStart} </if> <if test="lastLoginEnd!=null and lastLoginEnd!=''"><!-- 登录时间检索 --> and u.LAST_LOGIN <= #{lastLoginEnd} </if> order by u.LAST_LOGIN desc </select></mapper>
2 0
- #MyBatis中使用Ehcache缓存框架
- MyBatis中如何合理的使用EhCache缓存框架
- mybatis中整合ehcache缓存框架的使用
- hibernate中使用ehcache缓存框架
- Mybatis中使用ehcache
- 【MyBatis框架】查询缓存-二级缓存-整合ehcache
- 【MyBatis框架】查询缓存-二级缓存-整合ehcache
- 【MyBatis框架】查询缓存-二级缓存-整合ehcache
- mybatis和ehcache缓存框架整合
- mybatis 应用第三方缓存框架ehcache
- Jfinal中使用EhCache缓存
- mybatis框架缓存使用
- Ehcache缓存框架入门级使用
- 【EhCache】Java缓存框架使用EhCache结合Spring AOP
- 【EhCache】Java缓存框架使用EhCache结合Spring AOP
- 【EhCache】Java缓存框架使用EhCache结合Spring AOP
- EhCache缓存框架(1)-EhCache的简单使用
- mybatis配置ehcache缓存
- 相关FirDAC引用网址
- c——内置类型字面值
- 允许交换两个数(一对)的位置 求最大子数组和
- 大数据文本读取并保存到MSSQL的坑
- 小木棍_洛谷1120_搜索
- #MyBatis中使用Ehcache缓存框架
- Flask-Web 表单
- java.security.ProviderException: java.security.KeyException
- Eclipse 编辑HTML时卡顿假死
- java时间格式化----java.text.SimpleDateFormat
- break与continue--Java基础024
- Android工具类之创建快捷方式的方法详解
- 明明的随机数
- php 目录相关函数