一、整合ehcache
为什么需要缓存?提高程序的性能。
mybatis整合ehcache与hibernate的ehcache二级缓存相似 https://blog.csdn.net/oydl_1234/article/details/83625046
1、导入相关的依赖
<!--spring对ehcache的相关支持-->
<dependency>
<groupId>org.spring work</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--mybatis与ehcache整合-->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.1.0</version>
</dependency>
<!--ehcache依赖-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.0</version>
</dependency>
2、在spring-ehcache的配置文件编写(配置读取ehcache. 的cachemanagerFactory,得到cachemanager)
<!-- 使用ehcache缓存 -->
<bean id=\"cacheManagerFactory\" class=\"org.spring work.cache.ehcache.EhCacheManagerFactoryBean\">
<property name=\"configLocation\" value=\"classpath:ehcache. \"/>
<property name=\"shared\" value=\"true\"></property>
</bean>
<!-- 默认是cacheManager -->
<bean id=\"cacheManager\" class=\"org.spring work.cache.ehcache.EhCacheCacheManager\">
<property name=\"cacheManager\" ref=\"cacheManagerFactory\"/>
</bean>
3、spring-mybatis. 中的sqlsessionfactory新增配置,开启二级缓存
<!--设置mybaits对缓存的支持-->
<property name=\"configurationProperties\">
<props>
<!-- 全局映射器启用缓存 *主要将此属性设置完成即可-->
<prop key=\"cacheEnabled\">true</prop>
<!-- 查询时,关闭关联对象即时加载以提高性能 -->
<prop key=\"lazyLoadingEnabled\">false</prop>
<!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->
<prop key=\"aggressiveLazyLoading\">true</prop>
</props>
</property>
4、对应的mapper接口操作数据使用二级缓存前,要在mapper. 中配置cache标签
<cache type=\"实现了ibatis的cache接口的工具类\">(这个类由mybatis-ehcache提供了)
注意:
hibernate与mybatis二级缓存的不同之处
1、mybatis的二级缓存针对当前的mapper接口的所有方法,但又可以控制到mapper代理接口中的一个方法,若对应的方法不想使用缓存,添加属性usecache改为false。
2、mybatis使用二级缓存默认可以缓存单条记录以及多条记录。
二、mybatis整合redis作为二级缓存
1、导入依赖
<!-- redis与spring的整合依赖 -->
<redis.version>2.9.0</redis.version>
<redis.spring.version>1.7.1.RELEASE</redis.spring.version>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${redis.version}</version>
</dependency>
<dependency>
<groupId>org.spring work.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${redis.spring.version}</version>
</dependency>
如果出现版本冲突的问题,可以替换版本
redis.client 2.9.0 ---- spring-data-redis 1.7.1.RELEASE
redis.client 2.9.0 ---- spring-data-redis 1.7.2.RELEASE
Jackson
<!-- jackson -->
<dependency>
<groupId>com.faster .jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.faster .jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.faster .jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.3</version>
</dependency>
2、添加两个redis的配置文件,并将redis.properties和spring-redis. 配置到applicationContext. 文件中
redis.properties
spring-redis. (注册redis.properties、redis连接池、redisconnection、redistemplete)
注1:将redis.properties导入到applicationContext. 文件中
spring中引入第二个属性文件会出现“找不到某个配置项”错误,这是因为spring只允许有一个<context:property-placeholder/>
会报错:java.lang.IllegalStateException: Failed to load ApplicationContext
<!--引入一个属性文件的写法-->
<context:property-placeholder ignore-unresolvable=\"true\" location=\"classpath:jdbc.properties\" />
<!--引入两个或多个属性文件的写法-->
<context:property-placeholder ignore-unresolvable=\"true\" location=\"classpath:jdbc.properties,classpath:redis.properties\" />
将mybatis和redis. 的引入外部资源去掉,在总资源文件content加上
<!--1. 引入外部多文件方式 -->
<bean id=\"propertyConfigurer\"
class=\"org.spring work.beans.factory.config.PropertyPlaceholderConfigurer\">
<property name=\"systemPropertiesModeName\" value=\"SYSTEM_PROPERTIES_MODE_OVERRIDE\" />
<property name=\"ignoreResourceNotFound\" value=\"true\" />
<property name=\"locations\">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:redis.properties</value>
</list>
</property>
</bean>
注2:通过import标签将spring-redis. 导入到applicationContext. 文件中
<!--导入redis配置-->
<import resource=\"spring-redis. \"/>
3、将redis缓存引入到mybatis中
3.1 创建mybatis的自定义缓存类“RedisCache”,必须实现org.apache.ibatis.cache.Cache接口
RedisCache.java
3.2 静态注入中间类“RedisCacheTransfer”,解决RedisCache中RedisTemplate的静态注入,从而使MyBatis实现第三方缓存
RedisCacheTransfer.java
3.3 在XxxMapper. 中添加自定义cache功能
<cache type=\"com.zking.ssm.util.RedisCache\"/>
注意事项:
1、spring多配置文件的引入方式
2、pom中对properties文件的编译
3、加一个spring-xxx. 一定记得在spring-Context. 导入
继续阅读与本文标签相同的文章
四、【排序】桶排序(bucketsort)
对话阿里云异构计算掌舵人:人工智能爆发三要素
-
网站渗透测试服务项目内容详情
2026-05-18栏目: 教程
-
来杭州云栖大会,全面了解企业如何实现云上IT治理
2026-05-18栏目: 教程
-
2019「边缘计算」大看点,点击解锁详细版云栖大会参会指南
2026-05-18栏目: 教程
-
生存还是毁灭?一文读懂挖矿木马的战略战术
2026-05-18栏目: 教程
-
数据库基础技术实践#网络安全基础技术实践课程
2026-05-18栏目: 教程
