前幾天在spring整合Redis的時候使用了手動的方式,也就是可以手動的向redis添加緩存與清除緩存,參考:http://www.cnblogs.com/qlqwjy/p/8562703.html
今天想的將spring注解整合Redis緩存弄明白,於是通過查閱資料,先做記錄如下:
大致步驟如下:
0.spring的主配置中聲明注解緩存:<cache:annotation-driven cache-manager="redisCacheManager"/>
1.maven的pom.xml文件導入架包
2.配置文件添加配置
3.spring管理bean的生成,xml文件配置
4. RedisCacheConfig redis自定義的工具類,自定義redis的key生成規則
5.在你想要做緩存的地方,使用注解進行緩存
- 0.spring的主配置中聲明注解緩存:<cache:annotation-driven cache-manager="redisCacheManager"/>
注意:此步驟必須做,必須聲明采用的緩存管理器是自己配置的redisCacheManager,否則會報錯。
<cache:annotation-driven cache-manager="redisCacheManager"/>
- 1.maven的pom.xml文件導入架包
注意:
引入jackson是為了手動添加緩存
<!-- jedis依賴 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.4.RELEASE</version> </dependency> <!-- jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.1.0</version> </dependency>
- 2.配置文件添加配置redis.properties
#訪問地址
redis.host=127.0.0.1
#訪問端口
redis.port=6379
#注意,如果沒有password,此處不設置值,但這一項要保留
redis.password=
#最大空閑數,數據庫連接的最大空閑時間。超過空閑時間,數據庫連接將被標記為不可用,然后被釋放。設為0表示無限制。
redis.maxIdle=300
#連接池的最大數據庫連接數。設為0表示無限制
redis.maxActive=600
#最大建立連接等待時間。如果超過此時間將接到異常。設為-1表示無限制。
redis.maxWait=1000
#在borrow一個jedis實例時,是否提前進行alidate操作;如果為true,則得到的jedis實例均是可用的;
redis.testOnBorrow=true
- 3.spring管理bean的生成,xml文件配置 applicationContext-redis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 連接池基本參數配置,類似數據庫連接池 --> <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true" /> <!-- redis連接池 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.maxActive}" /> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <!-- 連接池配置,類似數據庫連接池 --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.host}"></property> <property name="port" value="${redis.port}"></property> <!-- <property name="password" value="${redis.pass}"></property> --> <property name="poolConfig" ref="poolConfig"></property> </bean> <!--redis操作模版,使用該對象可以操作redis --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" > <property name="connectionFactory" ref="jedisConnectionFactory" /> <!--如果不配置Serializer,那么存儲的時候缺省使用String,如果用User類型存儲,那么會提示錯誤User can't cast to String!! --> <property name="keySerializer" > <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer" > <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" /> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/> </property> <!--開啟事務 --> <property name="enableTransactionSupport" value="true"></property> </bean > <!-- 配置RedisCacheManager --> <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"> <constructor-arg name="redisOperations" ref="redisTemplate" /> <property name="defaultExpiration" value="${redis.expiration}" /> </bean> <!-- 配置RedisCacheConfig --> <bean id="redisCacheConfig" class="cn.qlq.util.RedisCacheConfig"> <constructor-arg ref="jedisConnectionFactory"/> <constructor-arg ref="redisTemplate"/> <constructor-arg ref="redisCacheManager"/> </bean> <!-- 下面這個是整合Mybatis的二級緩存使用的 --> <bean id="redisCacheTransfer" class="cn.qlq.jedis.RedisCacheTransfer"> <property name="jedisConnectionFactory" ref="jedisConnectionFactory" /> </bean> </beans>
JedisPoolConfig jedis連接池配置對象
JedisConnectionFactory jedis連接工廠,生成連接對象
RedisTemplate RedisTemplate 對 RedisConnection 進行了封裝。提供連接管理,序列化等功能,它對 Redis 的交互進行了更高層次的抽象,極大的方便和簡化了 Redis 的操作
RedisCacheManager 做為 redis 統一的調度和管理者
RedisCacheConfig RedisCacheConfig extends org.springframework.cache.annotation.CachingConfigurerSupport,自定義redis的key生成規則,如果不在注解參數中注明key=“”的話,就采用這個類中的key生成規則生成key
- 4. RedisCacheConfig redis自定義的工具類,自定義redis的key生成規則
采用Java配置的方式注入到spring
package cn.qlq.util; import java.lang.reflect.Method; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; @EnableCaching @Configuration public class RedisCacheConfig extends CachingConfigurerSupport { private volatile JedisConnectionFactory jedisConnectionFactory; private volatile RedisTemplate<String, String> redisTemplate; private volatile RedisCacheManager redisCacheManager; public RedisCacheConfig() { super(); } /** * 帶參數的構造方法 初始化所有的成員變量 * * @param jedisConnectionFactory * @param redisTemplate * @param redisCacheManager */ public RedisCacheConfig(JedisConnectionFactory jedisConnectionFactory, RedisTemplate<String, String> redisTemplate, RedisCacheManager redisCacheManager) { this.jedisConnectionFactory = jedisConnectionFactory; this.redisTemplate = redisTemplate; this.redisCacheManager = redisCacheManager; } public JedisConnectionFactory getJedisConnecionFactory() { return jedisConnectionFactory; } public RedisTemplate<String, String> getRedisTemplate() { return redisTemplate; } public RedisCacheManager getRedisCacheManager() { return redisCacheManager; } @Bean public KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Object generate(Object o, Method method, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(o.getClass().getName()); sb.append(method.getName()); for (Object param : params) { sb.append(param.toString()); } return sb.toString(); } }; } }
上面的自定義的key規則是類 本類名+方法名+參數名(中間沒有逗號區分),例如:
Array2List.TestClassAndMethodAndParamfun1{2=ssssssssssssssssss, 1=ssssssssssssssssss}
所以我們可以對上面的自定義規則進行改造,將方法名和參數名進行隔開之后進行區分:(修改過的key的生成規則)
public Object generate(Object o, Method method, Object... params) { //規定 本類名+方法名+參數名 為key StringBuilder sb = new StringBuilder(); sb.append(o.getClass().getName()); sb.append("-"); sb.append(method.getName()); sb.append("-"); for (Object param : params) { sb.append(param.toString()); } return sb.toString(); }
結果:
Array2List.TestClassAndMethodAndParam-fun1-{2=ssssssssssssssssss, 1=ssssssssssssssssss}
- 5.在你想要做緩存的地方,使用注解進行緩存
先介紹幾個注解
1》@CacheConfig 配置在類上,cacheNames即定義了本類中所有用到緩存的地方,都去找這個庫。只要使用了這個注解,在方法上@Cacheable @CachePut @CacheEvict就可以不用寫value去找具體庫名了。【一般不怎么用】
2》@Cacheable 配置在方法或類上,作用:本方法執行后,先去緩存看有沒有數據,如果沒有,從數據庫中查找出來,給緩存中存一份,返回結果,下次本方法執行,在緩存未過期情況下,先在緩存中查找,有的話直接返回,沒有的話從數據庫查找
3》@CachePut 類似於更新操作,即每次不管緩存中有沒有結果,都從數據庫查找結果,並將結果更新到緩存,並返回結果
4》@CacheEvict 用來清除用在本方法或者類上的緩存數據(用在哪里清除哪里)
例子:
最直觀的表現:首次登錄,會有一條數據庫的查詢語句在控制台。
退出再登錄,不會執行數據庫的查詢,直接從數據庫中取出緩存,登錄成功。
說明:
①使用了@Cacheable(value="myUser"),即表示緩存中有,直接從緩存取出,沒有的話先從數據庫中查出,然后再插入
②如果未在類上使用@CacheConfig注解規定數據要緩存到哪個庫中,就必須給value一個值,規定數據最后緩存到哪個redis庫中
③因為redis緩存數據實際就是鍵值對的形式存儲,因此必須給定key-value的key,這里沒有給key參數賦值,所以key的生成規則按照上面工具類中規定的key生成的
④key-value的value就是本方法的返回值,如果要緩存登錄用戶信息,本方法需要進行修改,返回user對象就可以緩存到key-value的value中
例如:
Action:
package cn.qlq.Action; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.Validate; import org.apache.struts2.ServletActionContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.github.pagehelper.PageHelper; import com.opensymphony.xwork2.ActionSupport; import cn.qlq.bean.User; import cn.qlq.service.UserService; @Controller @Scope("prototype") @SuppressWarnings("all") public class UserAction extends ActionSupport { private Map<String, Object> response; @Autowired private UserService userService; private int id; private String name; /** * 測試清除注解保存的緩存的同時手動添加單個緩存 * * @return */ public String add() { try { userService.addUser(id, name); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "add"; } /** * 測試查詢單個的時候注解添加單個緩存 * * @return * @throws Exception */ public String find() throws Exception { User user = userService.findUserById(id); HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("user", user); return "find"; } /** * 測試刪除單個的時候注解刪除單個緩存 * @return */ public String delete() { try { userService.deleteById(id); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "delete"; } /** * 測試注解緩存,將查出來的集合結果加入緩存 * * @return * @throws Exception */ public String findPage() throws Exception { response = new HashMap(); if (name == null) name = "111"; // 第三個參數代表排序方式 PageHelper.startPage(2, 2, "id desc"); List<User> users = userService.findUsersByPage(name); response.put("users", users); return "success"; } public Map getResponse() { return response; } public void setResponse(Map response) { this.response = response; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Service添加緩存與刪除緩存:
package cn.qlq.service.impl; import java.sql.SQLException; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import cn.qlq.bean.User; import cn.qlq.mapper.UserMapper; import cn.qlq.service.UserService; import cn.qlq.util.RedisUtil; @Service public class UserServiceImpl implements UserService { @Autowired private RedisUtil redisUtil; @Autowired private UserMapper userMapper; //查詢出來的時候添加單個緩存 @Cacheable(value="user",key="'user'+#id.toString()") @Override public User findUserById(int id) throws Exception { System.out.println("打印語句則沒有走緩存"); return userMapper.findUserById(id); } //刪除數據庫的時候刪除redis的緩存 @Override @CacheEvict(value="user",key="'user'+#id.toString()") public boolean deleteById(int id){ return true; } //添加緩存 @Cacheable(value="Alluser") @Override public List<User> findUsersByPage(String name) throws SQLException { System.out.println("打印語句則沒有走緩存"); return userMapper.findUsersByPage(); } //清除上面的緩存,同時手動的添加一個緩存看能否實現 @CacheEvict(value="Alluser") @Override public int addUser(int id, String name) throws SQLException { redisUtil.set("mykey", "myvalue"); return userMapper.addUser(id, name); } }
解釋:
findUserById()函數將單個用戶存入緩存中,例如訪問:http://localhost/SSM/user_find?id=1 后查看redis:
函數注解上面的user加上~value作為一個zset存入緩存,值為具體的緩存的鍵:
繼續訪問 http://localhost/SSM/user_find?id=2 http://localhost/SSM/user_find?id=3 http://localhost/SSM/user_find?id=4之后
繼續訪問http://localhost/SSM/user_delete?id=4 刪除一個user4緩存:
總結:
此版本的redisspring-data-redis在設置緩存的時候是將value的值加上~keys存為一個zset,值就是每個具體的緩存的key。例如上面
@Cacheable(value="user",key="'user'+#id.toString()")
就是將user~keys作為一個zset,然后其值為user1.......(緩存的鍵)
刪除緩存的時候刪除指定的鍵,然后從指定的value加上~keys的zset中刪除對應的值,完成刪除一個緩存。
如果刪除的時候只指定了其value,而沒有指定key值,則跟據value值加上~keys作為key找到對應的zset,根據zset值獲取所有的key后刪除,然后刪除此zset即完成刪除。
最后給出這幾個注解的具體參數以及使用相關配圖參考。
表 1. @Cacheable 作用和配置方法
@Cacheable 的作用 主要針對方法配置,能夠根據方法的請求參數對其結果進行緩存
表 2. @CachePut 作用和配置方法
@CachePut 的作用 主要針對方法配置,能夠根據方法的請求參數對其結果進行緩存,和 @Cacheable 不同的是,它每次都會觸發真實方法的調用
@CachePut 主要的參數 | ||
value | 緩存的名稱,在 spring 配置文件中定義,必須指定至少一個 | 例如: @Cacheable(value=”mycache”) 或者 @Cacheable(value={”cache1”,”cache2”} |
key | 緩存的 key,可以為空,如果指定要按照 SpEL 表達式編寫,如果不指定,則缺省按照方法的所有參數進行組合 | 例如: @Cacheable(value=”testcache”,key=”#userName”) |
condition | 緩存的條件,可以為空,使用 SpEL 編寫,返回 true 或者 false,只有為 true 才進行緩存 | 例如: @Cacheable(value=”testcache”,condition=”#userName.length()>2”) |
表 3. @CacheEvict 作用和配置方法
@CachEvict 的作用 主要針對方法配置,能夠根據一定的條件對緩存進行清空@CacheEvict 主要的參數 | ||
value | 緩存的名稱,在 spring 配置文件中定義,必須指定至少一個 | 例如: @CachEvict(value=”mycache”) 或者 @CachEvict(value={”cache1”,”cache2”} |
key | 緩存的 key,可以為空,如果指定要按照 SpEL 表達式編寫,如果不指定,則缺省按照方法的所有參數進行組合 | 例如: @CachEvict(value=”testcache”,key=”#userName”) |
condition | 緩存的條件,可以為空,使用 SpEL 編寫,返回 true 或者 false,只有為 true 才清空緩存 | 例如: @CachEvict(value=”testcache”, condition=”#userName.length()>2”) |
allEntries | 是否清空所有緩存內容,缺省為 false,如果指定為 true,則方法調用后將立即清空所有緩存 | 例如: @CachEvict(value=”testcache”,allEntries=true) |
beforeInvocation | 是否在方法執行前就清空,缺省為 false,如果指定為 true,則在方法還沒有執行的時候就清空緩存,缺省情況下,如果方法執行拋出異常,則不會清空緩存 | 例如: @CachEvict(value=”testcache”,beforeInvocation=true) |
*************************************************************************************************************************************************************************************
*************************************************************************************************************************************************************************************
spEL表達式的使用方法:http://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/expressions.html
關於注解實現Redis緩存的方法,只有將key設計的合理且強大,整個的緩存在項目中才能通用且高效。否則,就像我上面的簡單的例子一樣,真的是搞笑了。
總結:
在redis做緩存的時候最好是每個緩存的生命周期不固定,也就是分散的使緩存失效。可以設置有效期為3-9小時。具體的做法就是在Java中產生一個3-9小時的隨機數。
注意:
在IDEA中整合的時候發現用注解配置Bean報錯,因此將上面的KeyGenerator單獨抽成類,注入到Spring中並在cache標簽中指明key生成器:
KeyGenerator.java
package cn.xm.jwxt.utils; import org.springframework.stereotype.Component; import java.lang.reflect.Method; /** * @Author: qlq * @Description * @Date: 22:49 2018/3/25 */ public class KeyGenerator implements org.springframework.cache.interceptor.KeyGenerator { @Override public Object generate(Object o, Method method, Object... params) { //規定 本類名+方法名+參數名 為key StringBuilder sb = new StringBuilder(); sb.append(o.getClass().getName()); sb.append(method.getName()); for (Object param : params) { sb.append(param.toString()); } return sb.toString(); } }
key的生成規則也可以修改為:
public Object generate(Object o, Method method, Object... params) { //規定 本類名+方法名+參數名 為key StringBuilder sb = new StringBuilder(); sb.append(o.getClass().getName()); sb.append("-"); sb.append(method.getName()); sb.append("-"); for (Object param : params) { sb.append(param.toString()); } return sb.toString(); }
RedisCacheConfig.java
package cn.xm.jwxt.utils; import java.lang.reflect.Method; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; /** * 注解redis緩存集成spring需要使用的配置類 java配置的方式注入bean到spring * @author liqiang * */ public class RedisCacheConfig extends CachingConfigurerSupport { private volatile JedisConnectionFactory jedisConnectionFactory; private volatile RedisTemplate<String, String> redisTemplate; private volatile RedisCacheManager redisCacheManager; public RedisCacheConfig() { super(); } /** * 帶參數的構造方法 初始化所有的成員變量 * * @param jedisConnectionFactory * @param redisTemplate * @param redisCacheManager */ public RedisCacheConfig(JedisConnectionFactory jedisConnectionFactory, RedisTemplate<String, String> redisTemplate, RedisCacheManager redisCacheManager) { this.jedisConnectionFactory = jedisConnectionFactory; this.redisTemplate = redisTemplate; this.redisCacheManager = redisCacheManager; } public JedisConnectionFactory getJedisConnecionFactory() { return jedisConnectionFactory; } public RedisTemplate<String, String> getRedisTemplate() { return redisTemplate; } public RedisCacheManager getRedisCacheManager() { return redisCacheManager; } }
applicationContext-redis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd "> <cache:annotation-driven cache-manager="redisCacheManager" key-generator="keyGenerator"/> <!-- 連接池基本參數配置,類似數據庫連接池 --> <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true" /> <!-- redis連接池 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.maxActive}" /> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <!-- 連接池配置,類似數據庫連接池 --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.host}"></property> <property name="port" value="${redis.port}"></property> <!-- <property name="password" value="${redis.pass}"></property> --> <property name="poolConfig" ref="poolConfig"></property> </bean> <!--redis操作模版,使用該對象可以操作redis --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" > <property name="connectionFactory" ref="jedisConnectionFactory" /> <!--如果不配置Serializer,那么存儲的時候缺省使用String,如果用User類型存儲,那么會提示錯誤User can't cast to String!! --> <property name="keySerializer" > <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer" > <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" /> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/> </property> <!--開啟事務 --> <property name="enableTransactionSupport" value="true"></property> </bean > <!-- 配置RedisCacheManager --> <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"> <constructor-arg name="redisOperations" ref="redisTemplate" /> <property name="defaultExpiration" value="${redis.expiration}" /> </bean> <!-- 配置RedisCacheConfig --> <bean id="redisCacheConfig" class="cn.xm.jwxt.utils.RedisCacheConfig"> <constructor-arg ref="jedisConnectionFactory"/> <constructor-arg ref="redisTemplate"/> <constructor-arg ref="redisCacheManager"/> </bean> <!----> <bean id="keyGenerator" class="cn.xm.jwxt.utils.KeyGenerator"></bean> <!-- 下面這個是整合Mybatis的二級緩存使用的 --> <!-- <bean id="redisCacheTransfer" class="cn.qlq.jedis.RedisCacheTransfer"> <property name="jedisConnectionFactory" ref="jedisConnectionFactory" /> </bean>--> </beans>
參考:https://www.cnblogs.com/sxdcgaq8080/p/7228163.html
手動redis注解集成spring:http://www.cnblogs.com/qlqwjy/p/8562703.html
關於緩存注解的更詳細的使用方法參考:http://www.cnblogs.com/qlqwjy/p/8559119.html