需要的jar包
spring-data-Redis-1.6.2.RELEASE.jar
jedis-2.7.2.jar(依赖 commons-pool2-2.3.jar)
commons-pool2-2.3.jar
spring-redis.xml 配置文件
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 1<beans xmlns="http://www.springframework.org/schema/beans"
2 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
3 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
5 xmlns:aop="http://www.springframework.org/schema/aop"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
8 http://www.springframework.org/schema/context
9 http://www.springframework.org/schema/context/spring-context-3.0.xsd
10 http://www.springframework.org/schema/mvc
11 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
12 http://www.springframework.org/schema/tx
13 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
14 http://www.springframework.org/schema/aop
15 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
16 http://www.springframework.org/schema/util
17 http://www.springframework.org/schema/util/spring-util-3.0.xsd">
18
19<!--[redis-JedisPoolConfig配置](http://blog.csdn.net/liang_love_java/article/details/50510753)-->
20<!-- jedis-2.7.2.jar 依赖jar包 commons-pool2-2.3.jar
21 jedis基于 commons-pool2-2.3.jar 自己实现了一个资源池。
22 配置参数 详见 http://blog.csdn.net/liang_love_java/article/details/50510753
23-->
24 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
25 <property name="maxIdle" value="1" />
26 <property name="maxTotal" value="5" />
27 <property name="blockWhenExhausted" value="true" />
28 <property name="maxWaitMillis" value="30000" />
29 <property name="testOnBorrow" value="true" />
30 </bean>
31
32 <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
33 <property name="hostName" value="10.1.8.200" />
34 <property name="port" value="6379"/>
35 <property name="poolConfig" ref="jedisPoolConfig" />
36 <property name="usePool" value="true"/>
37 </bean>
38
39 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
40 <property name="connectionFactory" ref="jedisConnectionFactory" />
41 <property name="keySerializer">
42 <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
43 </property>
44 <property name="valueSerializer">
45 <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
46 </property>
47 <property name="hashKeySerializer">
48 <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
49 </property>
50 <property name="hashValueSerializer">
51 <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
52 </property>
53 </bean>
54
55</beans>
56
57
58
-
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
-
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
测试代码
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 1import java.util.HashMap;
2import java.util.Map;
3
4import org.springframework.context.support.ClassPathXmlApplicationContext;
5import org.springframework.data.redis.core.HashOperations;
6import org.springframework.data.redis.core.ListOperations;
7import org.springframework.data.redis.core.RedisTemplate;
8import org.springframework.data.redis.core.ValueOperations;
9
10public static void main(String[] args) {
11 ClassPathXmlApplicationContext appCtx = new ClassPathXmlApplicationContext("spring-redis.xml");
12 final RedisTemplate<String, Object> redisTemplate = appCtx.getBean("redisTemplate",RedisTemplate.class);
13 //添加一个 key
14 ValueOperations<String, Object> value = redisTemplate.opsForValue();
15 value.set("lp", "hello word");
16 //获取 这个 key 的值
17 System.out.println(value.get("lp"));
18 //添加 一个 hash集合
19 HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
20 Map<String,Object> map = new HashMap<String,Object>();
21 map.put("name", "lp");
22 map.put("age", "26");
23 hash.putAll("lpMap", map);
24 //获取 map
25 System.out.println(hash.entries("lpMap"));
26 //添加 一个 list 列表
27 ListOperations<String, Object> list = redisTemplate.opsForList();
28 list.rightPush("lpList", "lp");
29 list.rightPush("lpList", "26");
30 //输出 list
31 System.out.println(list.range("lpList", 0, 1));
32 //添加 一个 set 集合
33 SetOperations<String, Object> set = redisTemplate.opsForSet();
34 set.add("lpSet", "lp");
35 set.add("lpSet", "26");
36 set.add("lpSet", "178cm");
37 //输出 set 集合
38 System.out.println(set.members("lpSet"));
39 //添加有序的 set 集合
40 ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
41 zset.add("lpZset", "lp", 0);
42 zset.add("lpZset", "26", 1);
43 zset.add("lpZset", "178cm", 2);
44 //输出有序 set 集合
45 System.out.println(zset.rangeByScore("lpZset", 0, 2));
46 }
47