一、整合ehcache
为什么需要缓存?提高程序的性能。
mybatis整合ehcache与hibernate的ehcache二级缓存相似 https://blog.csdn.net/oydl_1234/article/details/83625046
1、导入相关的依赖
<!--spring对ehcache的相关支持-->
<dependency>
<groupId>org.springframework</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.xml的cachemanagerFactory,得到cachemanager)
<!-- 使用ehcache缓存 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
<property name="shared" value="true"></property>
</bean>
<!-- 默认是cacheManager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean>
3、spring-mybatis.xml中的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.xml中配置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.springframework.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.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.3</version>
</dependency>
2、添加两个redis的配置文件,并将redis.properties和spring-redis.xml配置到applicationContext.xml文件中
redis.properties
spring-redis.xml(注册redis.properties、redis连接池、redisconnection、redistemplete)
注1:将redis.properties导入到applicationContext.xml文件中
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.xml的引入外部资源去掉,在总资源文件content加上
<!--1. 引入外部多文件方式 -->
<bean id="propertyConfigurer"
class="org.springframework.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.xml导入到applicationContext.xml文件中
<!--导入redis配置-->
<import resource="spring-redis.xml"/>
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.xml中添加自定义cache功能
<cache type="com.zking.ssm.util.RedisCache"/>
注意事项:
1、spring多配置文件的引入方式
2、pom中对properties文件的编译
3、加一个spring-xxx.xml一定记得在spring-Context.xml导入