spring4+mybatis3+ehcache2.10.4使用缓存

本文介绍如何在Spring框架下整合MyBatis与EHCache进行缓存管理,包括添加依赖、配置EHCache、实现序列化及mapper缓存设置。适用于希望提升数据读取效率的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

**

spring4+mybatis3+ehcache2.10.4使用缓存

**
参考网址: ehcache官网
1.加入依赖

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.10.4</version>
</dependency>
<dependency>
    <groupId>org.mybatis.caches</groupId>
    <artifactId>mybatis-ehcache</artifactId>
    <version>1.1.0</version>
</dependency>

2、在classpath路径下加入Ehcache配置文件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">
    <defaultCache overflowToDisk="true" eternal="false" maxElementsInMemory="1"/>  
    <diskStore path="E:/cache" />
</ehcache>  

3、在需要使用缓存的*mapper.xml中加入<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="*Mapper">

    <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
    <select id="selectByCondition" 

4、需要缓存的对象必须实现序列化接口

详细配置参考 ehcache2.10.4官方文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值