SSM整合redis做Mybatis二级缓存
YeBin 2017-12-15 tag1
# 一、准备相关jar包
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.4.RELEASE</version>
<dependency/>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 二、开启Mybatis二级缓存
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.yebin.domain"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!--配置mybatis日志信息-->
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<property name="logImpl" value="org.apache.ibatis.logging.stdout.StdOutImpl"/>
<!--开启二级缓存-->
<property name="cacheEnabled" value="true"/>
<property name="aggressiveLazyLoading" value="true"/>
</bean>
</property>
</bean>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
我这里由于整合了Spring 所以去掉了Mybatis配置文件,直接在Spring-mybatsi中进行配置
# 三、redis相关配置
<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"/>
<property name="poolConfig" ref="poolConfig"/>
<property name="password" value="${redis.password}"/>
<property name="port" value="${redis.port}"/>
</bean>
<!-- redis模板配置 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
<!-- 对于中文的存储 需要进行序列化操作存储 序列化器 -->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean id="jsonRedisSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer">
<!-- 使用默认的 ObjectMapper,移除JSON格式化后的 “@Class”节点 -->
<constructor-arg name="mapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper"/>
</constructor-arg>
</bean>
</property>
</bean>
<bean class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg name="redisOperations" ref="redisTemplate"/>
<!--<property name="defaultExpiration" value="${redis.expiration}" />-->
</bean>
<bean class="com.yebin.config.RedisCacheTransfer">
</bean>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Spring-data-redis-1.x采用如下配置,但Spring-data-redis2.x就不能这样配置了,在Spring-data-redis2.x中我们分单机和分布式来配置,由于我在这里只演示单机的,所以只配置单机版的:
<bean id="redisPassword" class="org.springframework.data.redis.connection.RedisPassword">
<constructor-arg index="0" value="${redis.password}"></constructor-arg>
</bean>
Spring-data-redis 2.0以上配置
<bean id="redisStandaloneConfiguration" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="password" ref="redisPassword" />
<property name="database" value="${redis.index}"/>
</bean>
Spring-data-redis 2.0以上配置
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="standaloneConfig" ref="redisStandaloneConfiguration"/>
</bean>
Spring-data-redis 2.0以上配置
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg index="0" >
<bean class="org.springframework.data.redis.cache.DefaultRedisCacheWriter">
<constructor-arg ref="connectionFactory"/>
</bean>
</constructor-arg>
<constructor-arg index="1">
<bean class="org.springframework.data.redis.cache.RedisCacheConfiguration" factory-method="defaultCacheConfig" >
</bean>
</constructor-arg>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 四、实现Cache接口
Cache接口是Mybatis提供的一个接口,如果需要使用第三方来做mybatis的缓存就需要去实现这个接口
public class RedisCache implements Cache {
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private String id;
private static RedisTemplate redisTemplate;
public RedisCache(String id) {
if (id == null) {
throw new IllegalArgumentException("Cache instances require an ID");
}
this.id = id;
}
/**
* 清除所有数据
*/
@Override
public void clear() {
Set keys = redisTemplate.keys("*");
redisTemplate.delete(keys);
}
/**
* @return
*/
@Override
public String getId() {
return this.id;
}
/**
* 得到指定key的 value
*
* @param key
* @return object
*/
@Override
public Object getObject(Object key) {
Object result = null;
result = redisTemplate.opsForValue().get(key.toString());
return result;
}
/**
* 得到当前db的key值
*
* @return int
*/
@Override
public int getSize() {
Set keys = redisTemplate.keys("*");
return keys.size();
}
@Override
public void putObject (Object key, Object value){
System.out.print("key="+key);
redisTemplate.opsForValue().set(key.toString(),value);
}
@Override
public Object removeObject (Object key){
Object result = null;
redisTemplate.delete(key.toString());
return result;
}
@Override
public ReadWriteLock getReadWriteLock () {
return this.readWriteLock;
}
public static void setRedisTemplate(RedisTemplate template)
{
RedisCache.redisTemplate=template;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
我这里配置了连个序列化器,一个是对Key的序列化器StringRedisSerializer,还有一个是对value的序列化器GenericJackson2JsonRedisSerializer,所以缓存的数据以JSON格式保存在redis数据库中
最后对要缓存的Mapper进行设置
<mapper namespace="com.yebin.mapper.PhoneMapper">
<cache type="com.yebin.config.RedisCache" ></cache>
</mapper>
1
2
3
2
3