1.添加jar包
<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.3.RELEASE</version>
</dependency>
2.配置文件
<!--资源池-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="1"/>
<property name="maxTotal" value="5"/>
<property name="blockWhenExhausted" value="true"/>
<property name="maxWaitMillis" value="30000"/>
<property name="testOnBorrow" value="true"/>
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="127.0.0.1"/>
<property name="port" value="6379"/>
<property name="poolConfig" ref="jedisPoolConfig"/>
<property name="usePool" value="true"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean>
3.测试
package token;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.*;
import static org.junit.Assert.*;
/**
* RedisTemplateTest
* Created by heqianqian on 2017/5/9.
*/
@ContextConfiguration(locations = "classpath:spring-redis.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class RedisTemplateTest {
@Resource
private RedisTemplate redisTemplate;
@Test
public void testString() throws Exception {
//添加一个key
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("csdn", "river");
//获取一个key
Assert.assertEquals("river", valueOperations.get("csdn"));
}
@Test
public void testMap() throws Exception {
//添加一个hash集合
HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
//hashOperations.put("article","java","java-io");
//hashOperations.put("article","mysql","procedure");
Map<String, Object> map = new HashMap<String, Object>();
map.put("java", "java-io");
map.put("mysql", "procedure");
hashOperations.putAll("article", map);
//获取map
Map<Object, Object> article = hashOperations.entries("article");
for (Map.Entry<Object, Object> entry : article.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
@Test
public void testList() throws Exception {
ListOperations listOperations = redisTemplate.opsForList();
listOperations.rightPush("queue", "i'm right");
listOperations.rightPush("queue", "i'm right2");
listOperations.leftPush("queue", "i'm left");
listOperations.leftPush("queue", "i'm left2");
List queue = listOperations.range("queue", 0, 4);
System.out.println(queue);
//[i'm left2, i'm left, i'm right, i'm right2]
}
@Test
public void testSet() throws Exception {
SetOperations setOperations = redisTemplate.opsForSet();
setOperations.add("set", "a");
setOperations.add("set", "b");
setOperations.add("set", "c");
setOperations.add("set", "c");
System.out.println(setOperations.members("set"));
}
@Test
public void testOrderedSet() throws Exception {
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add("zset", "a", 0);
zSetOperations.add("zset", "b", 2);
zSetOperations.add("zset", "c", 1);
System.out.println(zSetOperations.rangeByScore("zset", 0, 2));
}
}