mybatis3 添加ehcache支持
2016-06-08 16:21
281 查看
为了提高MyBatis的性能,有时候我们需要加入缓存支持,目前用的比较多的缓存莫过于ehcache缓存了,ehcache性能强大,而且位各种应用都提供了解决方案,在此我们主要是做查询缓存,提高查询的效率.
在Mybatis的官网上把集成ehcache的文档下载下来看了看,说的太简单了,对于新手很难理解,而且里面说的也不是很清楚,经过一番折腾,终于将ehcache加入了.
官网上提供了一个MyBatis-ehcache.jar的包用于整合ehcache缓存,文档中还说明需要一个ehcache-core.jar的包,除了这两个包之外有几个包也是必须的,官方并没有说明,以下是需要加入的所有和ehcache相关的包:
1.ehcache-core-2.4.4.jar
2.mybatis-ehcache-1.0.0.jar
3.slf4j-api-1.6.1.jar
4.slf4j-log4j12-1.6.2.jar
除此之外还有mybatis的jar包,log4j,MySQL驱动,这些大家应该都知道.
将上述包加入项目之后,新建一个文件名,该文件名必须为ehcache.xml,放在类路径下面,内容如下
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../bin/ehcache.xsd">
<!--
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(较少使用)
备注: 持久化到硬盘的路径由虚拟机参数"java.io.tmpdir"决定. 例如, 在windows中, 会在此路径下 C:\Documents and Settings\li\Local Settings\Temp 在linux中, 通常会在: /tmp 下
System.out.println(System.getProperty("java.io.tmpdir"));
-->
<defaultCache overflowToDisk="true" eternal="false"/>
<diskStore path="D:/cache" />
<!--
<cache name="zzugxy" overflowToDisk="true" eternal="false"
timeToIdleSeconds="300" timeToLiveSeconds="600" maxElementsInMemory="1000"
maxElementsOnDisk="10" diskPersistent="true" diskExpiryThreadIntervalSeconds="300"
diskSpoolBufferSizeMB="100" memoryStoreEvictionPolicy="LRU" />
-->
</ehcache>
[xml]
view plain
copy
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../bin/ehcache.xsd">
<!--
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(较少使用)
-->
<defaultCache overflowToDisk="true" eternal="false"/>
<diskStore path="D:/cache" />
<!--
<cache name="zzugxy" overflowToDisk="true" eternal="false"
timeToIdleSeconds="300" timeToLiveSeconds="600" maxElementsInMemory="1000"
maxElementsOnDisk="10" diskPersistent="true" diskExpiryThreadIntervalSeconds="300"
diskSpoolBufferSizeMB="100" memoryStoreEvictionPolicy="LRU" />
-->
</ehcache>
该文件是ehcache的配置文件,上面的注释已经说得很清楚了,这里我用的是默认的配置
至此ehcache已经配置好了,然后只需要在你想要缓存的mapper配置文件里面加入以下内容,该查询语句得到的结果将会被缓存
Xml代码
<?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="com.qiuqiu.dao.PersonDao">
<!-- 以下两个<cache>标签二选一,第一个可以输出日志,第二个不输出日志 -->
<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
<!-- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> -->
<select id="selectUserById" parameterType="int" resultType="org.qiuqiu.vo.Person">
select * from person where id=#{id}
</select>
</mapper>
[xml]
view plain
copy
<?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="com.qiuqiu.dao.PersonDao">
<!-- 以下两个<cache>标签二选一,第一个可以输出日志,第二个不输出日志 -->
<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
<!-- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> -->
<select id="selectUserById" parameterType="int" resultType="org.qiuqiu.vo.Person">
select * from person where id=#{id}
</select>
</mapper>
这样就对这个mapper里面的各种结果进行了缓存.程序中不需要修改任何地方.
这个过程不复杂,也没什么难度,不过Mybatis的官方说的也太含糊了.附件下面有,需要的各种jar包已经包含
在Mybatis的官网上把集成ehcache的文档下载下来看了看,说的太简单了,对于新手很难理解,而且里面说的也不是很清楚,经过一番折腾,终于将ehcache加入了.
官网上提供了一个MyBatis-ehcache.jar的包用于整合ehcache缓存,文档中还说明需要一个ehcache-core.jar的包,除了这两个包之外有几个包也是必须的,官方并没有说明,以下是需要加入的所有和ehcache相关的包:
1.ehcache-core-2.4.4.jar
2.mybatis-ehcache-1.0.0.jar
3.slf4j-api-1.6.1.jar
4.slf4j-log4j12-1.6.2.jar
除此之外还有mybatis的jar包,log4j,MySQL驱动,这些大家应该都知道.
将上述包加入项目之后,新建一个文件名,该文件名必须为ehcache.xml,放在类路径下面,内容如下
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../bin/ehcache.xsd">
<!--
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(较少使用)
备注: 持久化到硬盘的路径由虚拟机参数"java.io.tmpdir"决定. 例如, 在windows中, 会在此路径下 C:\Documents and Settings\li\Local Settings\Temp 在linux中, 通常会在: /tmp 下
System.out.println(System.getProperty("java.io.tmpdir"));
-->
<defaultCache overflowToDisk="true" eternal="false"/>
<diskStore path="D:/cache" />
<!--
<cache name="zzugxy" overflowToDisk="true" eternal="false"
timeToIdleSeconds="300" timeToLiveSeconds="600" maxElementsInMemory="1000"
maxElementsOnDisk="10" diskPersistent="true" diskExpiryThreadIntervalSeconds="300"
diskSpoolBufferSizeMB="100" memoryStoreEvictionPolicy="LRU" />
-->
</ehcache>
[xml]
view plain
copy
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../bin/ehcache.xsd">
<!--
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(较少使用)
-->
<defaultCache overflowToDisk="true" eternal="false"/>
<diskStore path="D:/cache" />
<!--
<cache name="zzugxy" overflowToDisk="true" eternal="false"
timeToIdleSeconds="300" timeToLiveSeconds="600" maxElementsInMemory="1000"
maxElementsOnDisk="10" diskPersistent="true" diskExpiryThreadIntervalSeconds="300"
diskSpoolBufferSizeMB="100" memoryStoreEvictionPolicy="LRU" />
-->
</ehcache>
该文件是ehcache的配置文件,上面的注释已经说得很清楚了,这里我用的是默认的配置
至此ehcache已经配置好了,然后只需要在你想要缓存的mapper配置文件里面加入以下内容,该查询语句得到的结果将会被缓存
Xml代码
<?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="com.qiuqiu.dao.PersonDao">
<!-- 以下两个<cache>标签二选一,第一个可以输出日志,第二个不输出日志 -->
<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
<!-- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> -->
<select id="selectUserById" parameterType="int" resultType="org.qiuqiu.vo.Person">
select * from person where id=#{id}
</select>
</mapper>
[xml]
view plain
copy
<?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="com.qiuqiu.dao.PersonDao">
<!-- 以下两个<cache>标签二选一,第一个可以输出日志,第二个不输出日志 -->
<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
<!-- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> -->
<select id="selectUserById" parameterType="int" resultType="org.qiuqiu.vo.Person">
select * from person where id=#{id}
</select>
</mapper>
这样就对这个mapper里面的各种结果进行了缓存.程序中不需要修改任何地方.
这个过程不复杂,也没什么难度,不过Mybatis的官方说的也太含糊了.附件下面有,需要的各种jar包已经包含
相关文章推荐
- Mybatis传递多个参数的解决办法(三种)
- 获取Java的MyBatis框架项目中的SqlSession的方法
- Mybatis逆工程jar包的修改和打包
- 深入浅析mybatis oracle BLOB类型字段保存与读取
- MyBatis MapperProvider MessageFormat拼接批量SQL语句执行报错的原因分析及解决办法
- 详解Java的MyBatis框架和Spring框架的整合运用
- Java的MyBatis框架项目搭建与hellow world示例
- SpringMVC整合mybatis实例代码
- oracle+mybatis 使用动态Sql当插入字段不确定的情况下实现批量insert
- MyBatis学习笔记(二)之关联关系
- 浅析Mybatis 在CS程序中的应用
- Java Mybatis框架入门基础教程
- Windows下Java+MyBatis框架+MySQL的开发环境搭建教程
- Mybatis与Ibatis的区别
- MyBatis学习教程(二)―如何使用MyBatis对users表执行CRUD操作
- Java+MyBatis+MySQL开发环境搭建流程详解
- MyBatis学习教程(四)-如何快速解决字段名与实体类属性名不相同的冲突问题
- Java环境中MyBatis与Spring或Spring MVC框架的集成方法
- spring、mybatis 配置方式详解(常用两种方式)
- Java简单实现SpringMVC+MyBatis分页插件